Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust the judgment logic of whether the current ordinal needs to be updated #1751

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)
}
})
}
}
Loading