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

feature: range reserveOrdinals for AdvancedStatefulSet #1873

Open
wants to merge 4 commits 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
39 changes: 38 additions & 1 deletion apis/apps/v1beta1/statefulset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ limitations under the License.
package v1beta1

import (
"strconv"
"strings"

apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"

appspub "github.com/openkruise/kruise/apis/apps/pub"
)
Expand Down Expand Up @@ -275,7 +280,8 @@ type StatefulSetSpec struct {
// Then controller will delete Pod-1 and create Pod-3 (existing Pods will be [0, 2, 3])
// - If you just want to delete Pod-1, you should set spec.reserveOrdinal to [1] and spec.replicas to 2.
// Then controller will delete Pod-1 (existing Pods will be [0, 2])
ReserveOrdinals []int `json:"reserveOrdinals,omitempty"`
// You can also use ranges along with numbers, such as [1, 3-5], which is a shortcut for [1, 3, 4, 5].
ReserveOrdinals ReserveOrdinal `json:"reserveOrdinals,omitempty"`

// Lifecycle defines the lifecycle hooks for Pods pre-delete, in-place update.
Lifecycle *appspub.Lifecycle `json:"lifecycle,omitempty"`
Expand All @@ -299,6 +305,37 @@ type StatefulSetSpec struct {
Ordinals *StatefulSetOrdinals `json:"ordinals,omitempty"`
}

type ReserveOrdinal []intstr.IntOrString

func (r ReserveOrdinal) GetIntSet() sets.Set[int] {
values := sets.New[int]()
for _, elem := range r {
if elem.Type == intstr.Int {
values.Insert(int(elem.IntVal))
} else {
split := strings.Split(elem.StrVal, "-")
if len(split) != 2 {
klog.ErrorS(nil, "invalid range reserveOrdinal found, an empty slice will be returned", "reserveOrdinal", elem.StrVal)
return nil
}
start, err := strconv.Atoi(split[0])
if err != nil {
klog.ErrorS(err, "invalid range reserveOrdinal found, an empty slice will be returned", "reserveOrdinal", elem.StrVal)
return nil
}
end, err := strconv.Atoi(split[1])
if err != nil {
klog.ErrorS(err, "invalid range reserveOrdinal found, an empty slice will be returned", "reserveOrdinal", elem.StrVal)
return nil
}
for i := start; i <= end; i++ {
values.Insert(i)
}
}
}
return values
}

// StatefulSetScaleStrategy defines strategies for pods scale.
type StatefulSetScaleStrategy struct {
// The maximum number of pods that can be unavailable during scaling.
Expand Down
99 changes: 99 additions & 0 deletions apis/apps/v1beta1/statefulset_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2025 The Kruise Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"testing"

"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
)

func TestGetIntSet(t *testing.T) {
tests := []struct {
name string
input ReserveOrdinal
expected sets.Set[int]
wantErr bool
}{
{
name: "integer elements",
input: ReserveOrdinal{
intstr.FromInt32(1),
intstr.FromInt32(2),
intstr.FromInt32(3),
},
expected: sets.New(1, 2, 3),
},
{
name: "string range",
input: ReserveOrdinal{
intstr.FromString("1-3"),
},
expected: sets.New(1, 2, 3),
},
{
name: "invalid range end",
input: ReserveOrdinal{
intstr.FromString("1-%"),
},
wantErr: true,
},
{
name: "invalid range start",
input: ReserveOrdinal{
intstr.FromString("%-2"),
},
wantErr: true,
},
{
name: "invalid range split",
input: ReserveOrdinal{
intstr.FromString("1-2-3"),
},
wantErr: true,
},
{
name: "mixed input",
input: ReserveOrdinal{
intstr.FromInt32(1),
intstr.FromString("2-3"),
},
expected: sets.New(1, 2, 3),
},
{
name: "empty input",
input: ReserveOrdinal{},
expected: sets.New[int](),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.input.GetIntSet()
if tt.wantErr {
if actual != nil {
t.Errorf("Expected error (nil set), but got %v", actual)
}
return
}
if !actual.Equal(tt.expected) {
t.Errorf("For case %q, expected set %v, but got %v", tt.name, tt.expected, actual)
}
})
}
}
21 changes: 20 additions & 1 deletion apis/apps/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion config/crd/bases/apps.kruise.io_statefulsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,12 @@ spec:
Then controller will delete Pod-1 and create Pod-3 (existing Pods will be [0, 2, 3])
- If you just want to delete Pod-1, you should set spec.reserveOrdinal to [1] and spec.replicas to 2.
Then controller will delete Pod-1 (existing Pods will be [0, 2])
You can also use ranges along with numbers, such as [1, 3-5], which is a shortcut for [1, 3, 4, 5].
items:
type: integer
anyOf:
- type: integer
- type: string
x-kubernetes-int-or-string: true
type: array
revisionHistoryLimit:
description: |-
Expand Down
6 changes: 5 additions & 1 deletion config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,12 @@ spec:
Then controller will delete Pod-1 and create Pod-3 (existing Pods will be [0, 2, 3])
- If you just want to delete Pod-1, you should set spec.reserveOrdinal to [1] and spec.replicas to 2.
Then controller will delete Pod-1 (existing Pods will be [0, 2])
You can also use ranges along with numbers, such as [1, 3-5], which is a shortcut for [1, 3, 4, 5].
items:
type: integer
anyOf:
- type: integer
- type: string
x-kubernetes-int-or-string: true
type: array
revisionHistoryLimit:
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"strings"

"github.com/openkruise/kruise/pkg/util/configuration"
"k8s.io/utils/ptr"

ctrlUtil "github.com/openkruise/kruise/pkg/controller/util"

Expand All @@ -36,7 +37,6 @@
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
utilpointer "k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -146,7 +146,7 @@
// replicas
Replicas int32
// kruise statefulset filed
ReserveOrdinals []int
ReserveOrdinals sets.Set[int]
DeletionTimestamp *metav1.Time
}

Expand Down Expand Up @@ -354,11 +354,10 @@
}

func isInStatefulSetReplicas(index int, sts *innerStatefulset) bool {
reserveOrdinals := sets.NewInt(sts.ReserveOrdinals...)
replicas := sets.NewInt()
replicaIndex := 0
for realReplicaCount := 0; realReplicaCount < int(sts.Replicas); replicaIndex++ {
if reserveOrdinals.Has(replicaIndex) {
if sts.ReserveOrdinals.Has(replicaIndex) {
continue
}
realReplicaCount++
Expand Down Expand Up @@ -457,7 +456,7 @@
APIVersion: workload.APIVersion,
Kind: workload.Kind,
Name: workload.Name,
Controller: utilpointer.BoolPtr(true),
Controller: ptr.To(true),

Check warning on line 459 in pkg/controller/persistentpodstate/persistent_pod_state_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/persistentpodstate/persistent_pod_state_controller.go#L459

Added line #L459 was not covered by tests
UID: workload.UID,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"testing"
"time"

"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/utils/ptr"

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
appsv1beta1 "github.com/openkruise/kruise/apis/apps/v1beta1"
Expand All @@ -36,7 +38,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -56,7 +57,7 @@ var (
UID: "012d18d5-5eb9-449d-b670-3da8fec8852f",
},
Spec: appsv1beta1.StatefulSetSpec{
Replicas: pointer.Int32Ptr(10),
Replicas: ptr.To[int32](10),
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Expand All @@ -70,7 +71,7 @@ var (
Namespace: "ns-test",
OwnerReferences: []metav1.OwnerReference{
{
Controller: pointer.BoolPtr(true),
Controller: ptr.To(true),
},
},
Annotations: map[string]string{
Expand Down Expand Up @@ -222,7 +223,7 @@ func TestReconcilePersistentPodState(t *testing.T) {
name: "kruise statefulset, scale down replicas 10->8, 1 pod deleted, 1 pod running",
getSts: func() (*apps.StatefulSet, *appsv1beta1.StatefulSet) {
kruise := kruiseStsDemo.DeepCopy()
kruise.Spec.Replicas = pointer.Int32Ptr(8)
kruise.Spec.Replicas = ptr.To[int32](8)
return nil, kruise
},
getPods: func() []*corev1.Pod {
Expand Down Expand Up @@ -316,8 +317,12 @@ func TestReconcilePersistentPodState(t *testing.T) {
name: "kruise reserveOrigin statefulset, scale down replicas 10->8, 1 pod deleted, 1 pod running",
getSts: func() (*apps.StatefulSet, *appsv1beta1.StatefulSet) {
kruise := kruiseStsDemo.DeepCopy()
kruise.Spec.Replicas = pointer.Int32Ptr(8)
kruise.Spec.ReserveOrdinals = []int{0, 3, 7}
kruise.Spec.Replicas = ptr.To[int32](8)
kruise.Spec.ReserveOrdinals = appsv1beta1.ReserveOrdinal{
intstr.FromInt32(0),
intstr.FromInt32(3),
intstr.FromInt32(7),
}
return nil, kruise
},
getPods: func() []*corev1.Pod {
Expand Down
Loading
Loading