Skip to content

Commit

Permalink
adjust the judgment logic of whether the current ordinal needs to be …
Browse files Browse the repository at this point in the history
…updated
  • Loading branch information
zhangyubin authored and zhangyubin committed Sep 19, 2024
1 parent 6d57029 commit 5ae5f63
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/statefulset/stateful_set_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,15 @@ func isCurrentRevisionNeeded(set *appsv1beta1.StatefulSet, updateRevision string
return false
}
if set.Spec.UpdateStrategy.RollingUpdate == nil {
return ordinal < int(set.Status.CurrentReplicas)
return ordinal < getStartOrdinal(set)+int(set.Status.CurrentReplicas)
}
if set.Spec.UpdateStrategy.RollingUpdate.UnorderedUpdate == nil {
return ordinal < int(*set.Spec.UpdateStrategy.RollingUpdate.Partition)
return ordinal < getStartOrdinal(set)+int(*set.Spec.UpdateStrategy.RollingUpdate.Partition)
}

var noUpdatedReplicas int
for i, pod := range replicas {
if pod == nil || i == ordinal {
if pod == nil || i+getStartOrdinal(set) == ordinal {

Check warning on line 522 in pkg/controller/statefulset/stateful_set_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/statefulset/stateful_set_utils.go#L522

Added line #L522 was not covered by tests
continue
}
if !revision.IsPodUpdate(pod, updateRevision) {
Expand Down
84 changes: 84 additions & 0 deletions pkg/controller/statefulset/stateful_set_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,87 @@ func newStorageClass(name string, canExpand bool) storagev1.StorageClass {
AllowVolumeExpansion: &canExpand,
}
}

func TestIsCurrentRevisionNeeded(t *testing.T) {
currentRevisionHash := "1"
updatedRevisionHash := "2"
int32Ptr := func(i int32) *int32 {
return &i
}

newRelicas := func(size int, revisionHash string) []*corev1.Pod {
pods := make([]*corev1.Pod, size)
for i := 0; i < size; i++ {
pods[i] = &corev1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
apps.ControllerRevisionHashLabelKey: revisionHash,
},
},
}
}
return pods
}

utilfeature.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.StatefulSetStartOrdinal, true)

tests := []struct {
name string
statefulSet *appsv1beta1.StatefulSet
updateRevision string
ordinal int
replicas []*corev1.Pod
expectedRes bool
}{
{
name: "Ordinals start 0",
statefulSet: &appsv1beta1.StatefulSet{
Spec: appsv1beta1.StatefulSetSpec{
Replicas: int32Ptr(3),
ReserveOrdinals: []int{},
Ordinals: &appsv1beta1.StatefulSetOrdinals{
Start: 0,
},
UpdateStrategy: appsv1beta1.StatefulSetUpdateStrategy{
Type: apps.RollingUpdateStatefulSetStrategyType,
},
},
},
updateRevision: updatedRevisionHash,
ordinal: 1,
replicas: newRelicas(2, currentRevisionHash),
expectedRes: false,
},
{
name: "Ordinals start 2",
statefulSet: &appsv1beta1.StatefulSet{
Spec: appsv1beta1.StatefulSetSpec{
Replicas: int32Ptr(4),
ReserveOrdinals: []int{},
Ordinals: &appsv1beta1.StatefulSetOrdinals{
Start: 2,
},
UpdateStrategy: appsv1beta1.StatefulSetUpdateStrategy{
Type: apps.RollingUpdateStatefulSetStrategyType,
},
},
},
updateRevision: updatedRevisionHash,
ordinal: 1,
replicas: newRelicas(2, currentRevisionHash),
expectedRes: true,
},
// ... other test cases
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := isCurrentRevisionNeeded(tt.statefulSet, tt.updateRevision, tt.ordinal, tt.replicas)
if res != tt.expectedRes {
t.Errorf("checkIsCurrentRevisionNeeded(%v) got (%v), want (%v)",
tt.name, res, tt.expectedRes)
}
})
}
}

0 comments on commit 5ae5f63

Please sign in to comment.