Skip to content

Commit

Permalink
implement StatefulSetStartOrdinal
Browse files Browse the repository at this point in the history
Signed-off-by: Abner-1 <yuanyuxing.yyx@alibaba-inc.com>
  • Loading branch information
ABNER-1 committed Jun 6, 2024
1 parent 0e69ed4 commit 113f941
Show file tree
Hide file tree
Showing 10 changed files with 736 additions and 252 deletions.
26 changes: 25 additions & 1 deletion apis/apps/v1beta1/statefulset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.
package v1beta1

import (
appspub "github.com/openkruise/kruise/apis/apps/pub"
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"

appspub "github.com/openkruise/kruise/apis/apps/pub"
)

const (
Expand Down Expand Up @@ -143,6 +144,21 @@ type StatefulSetPersistentVolumeClaimRetentionPolicy struct {
WhenScaled PersistentVolumeClaimRetentionPolicyType `json:"whenScaled,omitempty"`
}

// StatefulSetOrdinals describes the policy used for replica ordinal assignment
// in this StatefulSet.
type StatefulSetOrdinals struct {
// start is the number representing the first replica's index. It may be used
// to number replicas from an alternate index (eg: 1-indexed) over the default
// 0-indexed names, or to orchestrate progressive movement of replicas from
// one StatefulSet to another.
// If set, replica indices will be in the range:
// [.spec.ordinals.start, .spec.ordinals.start + .spec.replicas).
// If unset, defaults to 0. Replica indices will be in the range:
// [0, .spec.replicas).
// +optional
Start int32 `json:"start" protobuf:"varint,1,opt,name=start"`
}

// StatefulSetSpec defines the desired state of StatefulSet
type StatefulSetSpec struct {
// replicas is the desired number of replicas of the given Template.
Expand Down Expand Up @@ -228,6 +244,14 @@ type StatefulSetSpec struct {
// StatefulSetAutoDeletePVC feature gate to be enabled, which is alpha.
// +optional
PersistentVolumeClaimRetentionPolicy *StatefulSetPersistentVolumeClaimRetentionPolicy `json:"persistentVolumeClaimRetentionPolicy,omitempty"`

// ordinals controls the numbering of replica indices in a StatefulSet. The
// default ordinals behavior assigns a "0" index to the first replica and
// increments the index by one for each additional replica requested. Using
// the ordinals field requires the StatefulSetStartOrdinal feature gate to be
// enabled, which is beta.
// +optional
Ordinals *StatefulSetOrdinals `json:"ordinals,omitempty" protobuf:"bytes,11,opt,name=ordinals"`
}

// StatefulSetScaleStrategy defines strategies for pods scale.
Expand Down
20 changes: 20 additions & 0 deletions apis/apps/v1beta1/zz_generated.deepcopy.go

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

21 changes: 21 additions & 0 deletions config/crd/bases/apps.kruise.io_statefulsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,27 @@ spec:
type: boolean
type: object
type: object
ordinals:
description: |-
ordinals controls the numbering of replica indices in a StatefulSet. The
default ordinals behavior assigns a "0" index to the first replica and
increments the index by one for each additional replica requested. Using
the ordinals field requires the StatefulSetStartOrdinal feature gate to be
enabled, which is beta.
properties:
start:
description: |-
start is the number representing the first replica's index. It may be used
to number replicas from an alternate index (eg: 1-indexed) over the default
0-indexed names, or to orchestrate progressive movement of replicas from
one StatefulSet to another.
If set, replica indices will be in the range:
[.spec.ordinals.start, .spec.ordinals.start + .spec.replicas).
If unset, defaults to 0. Replica indices will be in the range:
[0, .spec.replicas).
format: int32
type: integer
type: object
persistentVolumeClaimRetentionPolicy:
description: |-
PersistentVolumeClaimRetentionPolicy describes the policy used for PVCs created from
Expand Down
21 changes: 21 additions & 0 deletions config/crd/bases/apps.kruise.io_uniteddeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ spec:
type: boolean
type: object
type: object
ordinals:
description: |-
ordinals controls the numbering of replica indices in a StatefulSet. The
default ordinals behavior assigns a "0" index to the first replica and
increments the index by one for each additional replica requested. Using
the ordinals field requires the StatefulSetStartOrdinal feature gate to be
enabled, which is beta.
properties:
start:
description: |-
start is the number representing the first replica's index. It may be used
to number replicas from an alternate index (eg: 1-indexed) over the default
0-indexed names, or to orchestrate progressive movement of replicas from
one StatefulSet to another.
If set, replica indices will be in the range:
[.spec.ordinals.start, .spec.ordinals.start + .spec.replicas).
If unset, defaults to 0. Replica indices will be in the range:
[0, .spec.replicas).
format: int32
type: integer
type: object
persistentVolumeClaimRetentionPolicy:
description: |-
PersistentVolumeClaimRetentionPolicy describes the policy used for PVCs created from
Expand Down
16 changes: 16 additions & 0 deletions pkg/controller/statefulset/stateful_pod_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,22 @@ func (spc *StatefulPodControl) recordClaimEvent(verb string, set *appsv1beta1.St
}
}

// createMissingPersistentVolumeClaims creates all of the required PersistentVolumeClaims for pod, and updates its retention policy
func (spc *StatefulPodControl) createMissingPersistentVolumeClaims(ctx context.Context, set *appsv1beta1.StatefulSet, pod *v1.Pod) error {
if err := spc.createPersistentVolumeClaims(set, pod); err != nil {
return err
}

if utilfeature.DefaultFeatureGate.Enabled(features.StatefulSetAutoDeletePVC) {
// Set PVC policy as much as is possible at this point.
if err := spc.UpdatePodClaimForRetentionPolicy(set, pod); err != nil {
spc.recordPodEvent("update", set, pod, err)
return err
}
}
return nil
}

// createPersistentVolumeClaims creates all of the required PersistentVolumeClaims for pod, which must be a member of
// set. If all of the claims for Pod are successfully created, the returned error is nil. If creation fails, this method
// may be called again until no error is returned, indicating the PersistentVolumeClaims for pod are consistent with
Expand Down
Loading

0 comments on commit 113f941

Please sign in to comment.