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

define partition as number of non-updated pods should be reversed #1819

Merged
merged 1 commit into from
Nov 11, 2024
Merged
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
4 changes: 1 addition & 3 deletions apis/apps/v1beta1/statefulset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ type VolumeClaimUpdateStrategy struct {

// RollingUpdateStatefulSetStrategy is used to communicate parameter for RollingUpdateStatefulSetStrategyType.
type RollingUpdateStatefulSetStrategy struct {
// Partition indicates the ordinal at which the StatefulSet should be partitioned by default.
// But if unorderedUpdate has been set:
// - Partition indicates the number of pods with non-updated revisions when rolling update.
// Partition indicates the number of pods the StatefulSet should be partitioned by default.
// - It means controller will update $(replicas - partition) number of pod.
// Default value is 0.
// +optional
Expand Down
4 changes: 1 addition & 3 deletions config/crd/bases/apps.kruise.io_statefulsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,7 @@ spec:
type: integer
partition:
description: |-
Partition indicates the ordinal at which the StatefulSet should be partitioned by default.
But if unorderedUpdate has been set:
- Partition indicates the number of pods with non-updated revisions when rolling update.
Partition indicates the number of pods the StatefulSet should be partitioned by default.
- It means controller will update $(replicas - partition) number of pod.
Default value is 0.
format: int32
Expand Down
4 changes: 1 addition & 3 deletions config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,7 @@ spec:
type: integer
partition:
description: |-
Partition indicates the ordinal at which the StatefulSet should be partitioned by default.
But if unorderedUpdate has been set:
- Partition indicates the number of pods with non-updated revisions when rolling update.
Partition indicates the number of pods the StatefulSet should be partitioned by default.
- It means controller will update $(replicas - partition) number of pod.
Default value is 0.
format: int32
Expand Down
11 changes: 10 additions & 1 deletion pkg/controller/statefulset/stateful_set_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,16 @@ func isCurrentRevisionNeeded(set *appsv1beta1.StatefulSet, updateRevision string
return ordinal < getStartOrdinal(set)+int(set.Status.CurrentReplicas)
}
if set.Spec.UpdateStrategy.RollingUpdate.UnorderedUpdate == nil {
return ordinal < getStartOrdinal(set)+int(*set.Spec.UpdateStrategy.RollingUpdate.Partition)
unreservedPodsNum := 0
// assume all pods [0, idx) are created and only reserved pods are nil
idx := ordinal - getStartOrdinal(set)
for i := 0; i < idx; i++ {
if replicas[i] != nil {
unreservedPodsNum++
}
}
// if all pods [0, idx] are current revision
return unreservedPodsNum+1 <= int(*set.Spec.UpdateStrategy.RollingUpdate.Partition)
}

var noUpdatedReplicas int
Expand Down
15 changes: 9 additions & 6 deletions pkg/controller/statefulset/stateful_set_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,8 @@ func TestIsCurrentRevisionNeeded(t *testing.T) {
updateRevision: updatedRevisionHash,
ordinal: 0,
replicas: func() []*corev1.Pod {
pods := newReplicas(2, 2, currentRevisionHash)
pods := []*corev1.Pod{nil, nil}
pods = append(pods, newReplicas(2, 2, currentRevisionHash)...)
return pods
}(),
expectedRes: true,
Expand Down Expand Up @@ -1701,12 +1702,12 @@ func TestIsCurrentRevisionNeeded(t *testing.T) {
},

// with startOrdinal and reservedIds
// TODO Abner-1: These cases will be discussed and may be changed
// fixes https://github.com/openkruise/kruise/issues/1813
{
// reservedId 1, replicas 3, partition 2
// 3: updated revision
// 0: current revision
// => 2: should be updated revision
// => 2: should be current revision
name: "Ordinals start 0, reservedId 1, partition 1, create pod2",
statefulSet: &appsv1beta1.StatefulSet{
Spec: appsv1beta1.StatefulSetSpec{
Expand All @@ -1731,16 +1732,17 @@ func TestIsCurrentRevisionNeeded(t *testing.T) {
ordinal: 2,
replicas: func() []*corev1.Pod {
pods := newReplicas(0, 1, currentRevisionHash)
pods = append(pods, nil, nil)
pods = append(pods, newReplicas(3, 1, updatedRevisionHash)...)
return pods
}(),
expectedRes: false,
expectedRes: true,
},
{
// start ordinal 2, reservedId 3, replicas 3, partition 2
// 5: updated revision
// 2: current revision
// => 4: should be updated revision
// => 4: should be current revision
name: "Ordinals start 2, reservedId 1, partition 1, create pod2",
statefulSet: &appsv1beta1.StatefulSet{
Spec: appsv1beta1.StatefulSetSpec{
Expand All @@ -1765,10 +1767,11 @@ func TestIsCurrentRevisionNeeded(t *testing.T) {
ordinal: 4,
replicas: func() []*corev1.Pod {
pods := newReplicas(2, 1, currentRevisionHash)
pods = append(pods, nil, nil)
pods = append(pods, newReplicas(5, 1, updatedRevisionHash)...)
return pods
}(),
expectedRes: false,
expectedRes: true,
},
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/statefulset/stateful_update_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ func sortPodsToUpdate(rollingUpdateStrategy *appsv1beta1.RollingUpdateStatefulSe
updateMin = int(*rollingUpdateStrategy.Partition)
}

maxUpdate := int(totalReplicas) - updateMin
if maxUpdate <= 0 {
return []int{}
}

if rollingUpdateStrategy == nil || rollingUpdateStrategy.UnorderedUpdate == nil {
var indexes []int
for target := len(replicas) - 1; target >= updateMin; target-- {
for target := len(replicas) - 1; target >= updateMin && len(indexes) < maxUpdate; target-- {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary to add the maxUpdate comparison here , since maxUpdate will be checked in L72

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In L41, we return indexes and will not go to the L72.

if replicas[target] == nil {
continue
}
Expand All @@ -42,10 +47,6 @@ func sortPodsToUpdate(rollingUpdateStrategy *appsv1beta1.RollingUpdateStatefulSe
}

priorityStrategy := rollingUpdateStrategy.UnorderedUpdate.PriorityStrategy
maxUpdate := int(totalReplicas) - updateMin
if maxUpdate <= 0 {
return []int{}
}

var updatedIdxs []int
var waitUpdateIdxs []int
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/statefulset/stateful_update_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestSortPodsToUpdate(t *testing.T) {
expected: []int{2, 1, 0},
},
{
// change the case because we define partition as number of pods with non-updated revision
strategy: &appsv1beta1.RollingUpdateStatefulSetStrategy{Partition: func() *int32 { var i int32 = 2; return &i }()},
updateRevision: "r1",
totalReplicas: 3,
Expand All @@ -97,7 +98,7 @@ func TestSortPodsToUpdate(t *testing.T) {
nil,
{ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{apps.ControllerRevisionHashLabelKey: "r0"}}},
},
expected: []int{4, 2},
expected: []int{4},
},
{
strategy: &appsv1beta1.RollingUpdateStatefulSetStrategy{
Expand Down
Loading