Skip to content

Commit 33c8b1e

Browse files
feat: support pod naming policy for collaset (#345)
* init * add collaset webhooks for naming policy * refactor sync pods * refactor sync pods * refactor sync pods * refactor calculate replicas * add webhook uts * add ut for pod naming policy scale * add ut * add ut * refactor naming policy api * refactor naming policy api * fix fmt vet * fix fmt vet * add uts * refactor fossa * refactor code * refactor code * refactor code
1 parent dea0beb commit 33c8b1e

File tree

20 files changed

+624
-49
lines changed

20 files changed

+624
-49
lines changed

apis/apps/v1alpha1/defaults.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
func SetDefaultCollaSet(cls *appsv1alpha1.CollaSet) {
2626
SetDefaultPodSpec(cls)
2727
SetDefaultCollaSetUpdateStrategy(cls)
28+
SetDefaultCollaSetNamingStrategy(cls)
2829
}
2930

3031
func SetDefaultPodSpec(in *appsv1alpha1.CollaSet) {
@@ -257,3 +258,12 @@ func SetDefaultCollaSetUpdateStrategy(cls *appsv1alpha1.CollaSet) {
257258
cls.Spec.UpdateStrategy.RollingUpdate.ByPartition = &appsv1alpha1.ByPartition{}
258259
}
259260
}
261+
262+
func SetDefaultCollaSetNamingStrategy(cls *appsv1alpha1.CollaSet) {
263+
if cls.Spec.NamingStrategy == nil {
264+
return
265+
}
266+
if len(cls.Spec.NamingStrategy.PodNamingSuffixPolicy) == 0 {
267+
cls.Spec.NamingStrategy.PodNamingSuffixPolicy = appsv1alpha1.PodNamingSuffixPolicyRandom
268+
}
269+
}

charts/templates/crd/apps.kusionstack.io_collasets.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ spec:
8383
If unspecified, defaults to 20
8484
format: int32
8585
type: integer
86+
namingStrategy:
87+
description: NamigPolicy indicates the strategy detail that will be
88+
used for replica naming
89+
properties:
90+
podNamingSuffixPolicy:
91+
description: |-
92+
PodNamingSuffixPolicy is a string enumeration that determaines how pod name suffix will be generated.
93+
A collaset pod name contains two parts to be placed in a string formation %s-%s; the prefix is collaset
94+
name, and the suffix is determined by PodNamingSuffixPolicy.
95+
type: string
96+
type: object
8697
paused:
8798
description: |-
8899
Indicates that the scaling and updating is paused and will not be processed by the

config/crd/bases/apps.kusionstack.io_collasets.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ spec:
8383
If unspecified, defaults to 20
8484
format: int32
8585
type: integer
86+
namingStrategy:
87+
description: NamigPolicy indicates the strategy detail that will be
88+
used for replica naming
89+
properties:
90+
podNamingSuffixPolicy:
91+
description: |-
92+
PodNamingSuffixPolicy is a string enumeration that determaines how pod name suffix will be generated.
93+
A collaset pod name contains two parts to be placed in a string formation %s-%s; the prefix is collaset
94+
name, and the suffix is determined by PodNamingSuffixPolicy.
95+
type: string
96+
type: object
8697
paused:
8798
description: |-
8899
Indicates that the scaling and updating is paused and will not be processed by the

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ require (
2222
k8s.io/klog/v2 v2.130.1
2323
k8s.io/kubernetes v0.0.0-00010101000000-000000000000
2424
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
25-
kusionstack.io/kube-api v0.7.4-0.20250728111735-11fa6b63ac0e
26-
kusionstack.io/kube-utils v0.2.1-0.20250815100310-6288d167061a
25+
kusionstack.io/kube-api v0.7.4-0.20250909095208-496f60eea9b5
26+
kusionstack.io/kube-utils v0.2.1-0.20250908100835-947051067cbe
2727
kusionstack.io/resourceconsist v0.0.1
2828
sigs.k8s.io/controller-runtime v0.17.3
2929
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,10 @@ k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
11511151
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
11521152
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro=
11531153
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
1154-
kusionstack.io/kube-api v0.7.4-0.20250728111735-11fa6b63ac0e h1:4+G3cHiUfDMh45xWGmZA+36UIZ8eBTSov4wxr3/gekE=
1155-
kusionstack.io/kube-api v0.7.4-0.20250728111735-11fa6b63ac0e/go.mod h1:e1jtrQH2LK5fD2nTyfIXG6nYrYbU8VXShRxTRwVPaLk=
1156-
kusionstack.io/kube-utils v0.2.1-0.20250815100310-6288d167061a h1:SsZ1ZSGRE9W/N1hC6ZW6D+iZr2WFt1ZSr1aPOzAXB4E=
1157-
kusionstack.io/kube-utils v0.2.1-0.20250815100310-6288d167061a/go.mod h1:pThYvlUgm58SXyax5nxHRln6EW7l6sqkNmlUqa/6OR0=
1154+
kusionstack.io/kube-api v0.7.4-0.20250909095208-496f60eea9b5 h1:/jbKYMeXiYnuxyJQs72MoL6vxVoY15FX/7tCKWljscY=
1155+
kusionstack.io/kube-api v0.7.4-0.20250909095208-496f60eea9b5/go.mod h1:e1jtrQH2LK5fD2nTyfIXG6nYrYbU8VXShRxTRwVPaLk=
1156+
kusionstack.io/kube-utils v0.2.1-0.20250908100835-947051067cbe h1:vLIXXJCL3ryDj4KZzUMlXUENhPcv0cfF77PNwjrttcU=
1157+
kusionstack.io/kube-utils v0.2.1-0.20250908100835-947051067cbe/go.mod h1:pThYvlUgm58SXyax5nxHRln6EW7l6sqkNmlUqa/6OR0=
11581158
kusionstack.io/resourceconsist v0.0.1 h1:+k/jriq5Ld7fQUYfWSMGynz/FesHtl3Rk2fmQPjBe0g=
11591159
kusionstack.io/resourceconsist v0.0.1/go.mod h1:816xS/fY6EOUbPFjXIWW/TGs8/YE46qP4ElKeIiwFdU=
11601160
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=

pkg/controllers/collaset/collaset_controller.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func calculateStatus(
279279

280280
activePods := synccontrol.FilterOutPlaceHolderPodWrappers(podWrappers)
281281
for _, podWrapper := range activePods {
282-
if podWrapper.DeletionTimestamp != nil {
282+
if podWrapper.DeletionTimestamp != nil && !collasetutils.IsPodNamingSuffixPolicyPersistentSequence(instance) {
283283
continue
284284
}
285285

@@ -391,25 +391,25 @@ func (r *CollaSetReconciler) ensureReclaimPvcs(ctx context.Context, cls *appsv1a
391391

392392
func (r *CollaSetReconciler) ensureReclaimPodOwnerReferences(cls *appsv1alpha1.CollaSet) error {
393393
podControl := podcontrol.NewRealPodControl(r.Client, r.Scheme)
394-
pods, err := podControl.GetFilteredPods(cls.Spec.Selector, cls)
394+
filteredPods, _, err := podControl.GetFilteredPods(cls.Spec.Selector, cls)
395395
if err != nil {
396396
return fmt.Errorf("fail to get filtered Pods: %w", err)
397397
}
398-
// reclaim podDecoration ownerReferences on pods
399-
for i := range pods {
400-
if len(pods[i].OwnerReferences) == 0 {
398+
// reclaim podDecoration ownerReferences on filteredPods
399+
for i := range filteredPods {
400+
if len(filteredPods[i].OwnerReferences) == 0 {
401401
continue
402402
}
403403
var newOwnerRefs []v1.OwnerReference
404-
for j := range pods[i].OwnerReferences {
405-
if pods[i].OwnerReferences[j].Kind == "PodDecoration" {
404+
for j := range filteredPods[i].OwnerReferences {
405+
if filteredPods[i].OwnerReferences[j].Kind == "PodDecoration" {
406406
continue
407407
}
408-
newOwnerRefs = append(newOwnerRefs, pods[i].OwnerReferences[j])
408+
newOwnerRefs = append(newOwnerRefs, filteredPods[i].OwnerReferences[j])
409409
}
410-
if len(newOwnerRefs) != len(pods[i].OwnerReferences) {
411-
pods[i].OwnerReferences = newOwnerRefs
412-
if err := podControl.UpdatePod(pods[i]); err != nil {
410+
if len(newOwnerRefs) != len(filteredPods[i].OwnerReferences) {
411+
filteredPods[i].OwnerReferences = newOwnerRefs
412+
if err := podControl.UpdatePod(filteredPods[i]); err != nil {
413413
return err
414414
}
415415
}
@@ -419,9 +419,9 @@ func (r *CollaSetReconciler) ensureReclaimPodOwnerReferences(cls *appsv1alpha1.C
419419

420420
func (r *CollaSetReconciler) ensureReclaimPodsDeletion(cls *appsv1alpha1.CollaSet) error {
421421
podControl := podcontrol.NewRealPodControl(r.Client, r.Scheme)
422-
pods, err := podControl.GetFilteredPods(cls.Spec.Selector, cls)
422+
filteredPods, _, err := podControl.GetFilteredPods(cls.Spec.Selector, cls)
423423
if err != nil {
424424
return fmt.Errorf("fail to get filtered Pods: %w", err)
425425
}
426-
return synccontrol.DeletePodsByLabel(podControl, pods)
426+
return synccontrol.DeletePodsByLabel(podControl, filteredPods)
427427
}

0 commit comments

Comments
 (0)