Skip to content

Commit 6cab44e

Browse files
committed
move assignedOrdinals to status
1 parent 68c327b commit 6cab44e

File tree

12 files changed

+261
-447
lines changed

12 files changed

+261
-447
lines changed

apis/workloads/v1/instanceset_types.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,6 @@ type InstanceSetSpec struct {
9191
// +optional
9292
Ordinals Ordinals `json:"ordinals,omitempty"`
9393

94-
// AssignedOrdinals is the set of ordinals assigned to the current replicas.
95-
//
96-
// IMPORTANT: This field is managed automatically by the InstanceSet controller.
97-
// Users should NOT manually modify this field.
98-
//
99-
// This field represents the authoritative set of ordinal identifiers currently in use by the workload.
100-
// It enables support for non-contiguous ordinals, allowing any instance to be terminated without affecting others.
101-
//
102-
// The controller uses this list to maintain identity consistency and to decide which specific ordinal
103-
// to allocate next during scaling up, or which identity is preserved during a restart.
104-
//
105-
// +optional
106-
AssignedOrdinals Ordinals `json:"assignedOrdinals,omitempty"`
107-
10894
// Defines the minimum number of seconds a newly created pod should be ready
10995
// without any of its container crashing to be considered available.
11096
// Defaults to 0, meaning the pod will be considered available as soon as it is ready.
@@ -366,9 +352,21 @@ type InstanceSetStatus struct {
366352
// +optional
367353
UpdateRevisions map[string]string `json:"updateRevisions,omitempty"`
368354

369-
// TemplatesStatus represents status of each instance generated by InstanceTemplates
355+
// TemplatesStatus represents status of each instance generated by InstanceTemplates.
356+
//
370357
// +optional
371358
TemplatesStatus []InstanceTemplateStatus `json:"templatesStatus,omitempty"`
359+
360+
// AssignedOrdinals is the ordinals assigned to the workload instances.
361+
//
362+
// This field represents the authoritative set of ordinal identifiers currently in use by the workload.
363+
// It enables support for non-contiguous ordinals, allowing any instance to be terminated without affecting others.
364+
//
365+
// The controller uses this to maintain identity consistency and to decide which specific ordinal
366+
// to allocate next during scaling up, or which identity is preserved during a restart.
367+
//
368+
// +optional
369+
AssignedOrdinals map[string]Ordinals `json:"assignedOrdinals,omitempty"`
372370
}
373371

374372
// PersistentVolumeClaimRetentionPolicy describes the policy used for PVCs created from the VolumeClaimTemplates.
@@ -419,20 +417,6 @@ type InstanceTemplate struct {
419417
// +optional
420418
Ordinals Ordinals `json:"ordinals,omitempty"`
421419

422-
// AssignedOrdinals is the set of ordinals assigned to the current replicas of this InstanceTemplate.
423-
//
424-
// IMPORTANT: This field is managed automatically by the InstanceSet controller.
425-
// Users should NOT manually modify this field.
426-
//
427-
// This field represents the authoritative set of ordinal identifiers currently in use by the workload.
428-
// It enables support for non-contiguous ordinals, allowing any instance to be terminated without affecting others.
429-
//
430-
// The controller uses this list to maintain identity consistency and to decide which specific ordinal
431-
// to allocate next during scaling up, or which identity is preserved during a restart.
432-
//
433-
// +optional
434-
AssignedOrdinals Ordinals `json:"assignedOrdinals,omitempty"`
435-
436420
// Specifies a map of key-value pairs to be merged into the Pod's existing annotations.
437421
// Existing keys will have their values overwritten, while new keys will be added to the annotations.
438422
//

apis/workloads/v1/zz_generated.deepcopy.go

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/workloads.kubeblocks.io_instancesets.yaml

Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -65,45 +65,6 @@ spec:
6565
description: Defines the desired state of the state machine. It includes
6666
the configuration details for the state machine.
6767
properties:
68-
assignedOrdinals:
69-
description: |-
70-
AssignedOrdinals is the set of ordinals assigned to the current replicas.
71-
72-
73-
IMPORTANT: This field is managed automatically by the InstanceSet controller.
74-
Users should NOT manually modify this field.
75-
76-
77-
This field represents the authoritative set of ordinal identifiers currently in use by the workload.
78-
It enables support for non-contiguous ordinals, allowing any instance to be terminated without affecting others.
79-
80-
81-
The controller uses this list to maintain identity consistency and to decide which specific ordinal
82-
to allocate next during scaling up, or which identity is preserved during a restart.
83-
properties:
84-
discrete:
85-
items:
86-
format: int32
87-
type: integer
88-
type: array
89-
ranges:
90-
items:
91-
description: |-
92-
Range represents a range with a start and an end value. Both start and end are included.
93-
It is used to define a continuous segment.
94-
properties:
95-
end:
96-
format: int32
97-
type: integer
98-
start:
99-
format: int32
100-
type: integer
101-
required:
102-
- end
103-
- start
104-
type: object
105-
type: array
106-
type: object
10768
configs:
10869
description: Describe the configs to be reconfigured.
10970
items:
@@ -732,45 +693,6 @@ spec:
732693
Specifies a map of key-value pairs to be merged into the Pod's existing annotations.
733694
Existing keys will have their values overwritten, while new keys will be added to the annotations.
734695
type: object
735-
assignedOrdinals:
736-
description: |-
737-
AssignedOrdinals is the set of ordinals assigned to the current replicas of this InstanceTemplate.
738-
739-
740-
IMPORTANT: This field is managed automatically by the InstanceSet controller.
741-
Users should NOT manually modify this field.
742-
743-
744-
This field represents the authoritative set of ordinal identifiers currently in use by the workload.
745-
It enables support for non-contiguous ordinals, allowing any instance to be terminated without affecting others.
746-
747-
748-
The controller uses this list to maintain identity consistency and to decide which specific ordinal
749-
to allocate next during scaling up, or which identity is preserved during a restart.
750-
properties:
751-
discrete:
752-
items:
753-
format: int32
754-
type: integer
755-
type: array
756-
ranges:
757-
items:
758-
description: |-
759-
Range represents a range with a start and an end value. Both start and end are included.
760-
It is used to define a continuous segment.
761-
properties:
762-
end:
763-
format: int32
764-
type: integer
765-
start:
766-
format: int32
767-
type: integer
768-
required:
769-
- end
770-
- start
771-
type: object
772-
type: array
773-
type: object
774696
env:
775697
description: |-
776698
Defines Env to override.
@@ -11684,6 +11606,45 @@ spec:
1168411606
description: Represents the current information about the state machine.
1168511607
This data may be out of date.
1168611608
properties:
11609+
assignedOrdinals:
11610+
additionalProperties:
11611+
description: Ordinals represents a combination of continuous segments
11612+
and individual values.
11613+
properties:
11614+
discrete:
11615+
items:
11616+
format: int32
11617+
type: integer
11618+
type: array
11619+
ranges:
11620+
items:
11621+
description: |-
11622+
Range represents a range with a start and an end value. Both start and end are included.
11623+
It is used to define a continuous segment.
11624+
properties:
11625+
end:
11626+
format: int32
11627+
type: integer
11628+
start:
11629+
format: int32
11630+
type: integer
11631+
required:
11632+
- end
11633+
- start
11634+
type: object
11635+
type: array
11636+
type: object
11637+
description: |-
11638+
AssignedOrdinals is the ordinals assigned to the workload instances.
11639+
11640+
11641+
This field represents the authoritative set of ordinal identifiers currently in use by the workload.
11642+
It enables support for non-contiguous ordinals, allowing any instance to be terminated without affecting others.
11643+
11644+
11645+
The controller uses this to maintain identity consistency and to decide which specific ordinal
11646+
to allocate next during scaling up, or which identity is preserved during a restart.
11647+
type: object
1168711648
availableReplicas:
1168811649
description: Total number of available instances (ready for at least
1168911650
minReadySeconds) targeted by this InstanceSet.
@@ -11846,7 +11807,7 @@ spec:
1184611807
type: integer
1184711808
templatesStatus:
1184811809
description: TemplatesStatus represents status of each instance generated
11849-
by InstanceTemplates
11810+
by InstanceTemplates.
1185011811
items:
1185111812
description: InstanceTemplateStatus aggregates the status of replicas
1185211813
for each InstanceTemplate

controllers/apps/component/transformer_component_workload.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package component
2222
import (
2323
"context"
2424
"reflect"
25-
"slices"
2625
"strings"
2726

2827
"golang.org/x/exp/maps"
@@ -254,9 +253,7 @@ func copyAndMergeITS(oldITS, newITS *workloads.InstanceSet) *workloads.InstanceS
254253
itsObjCopy.Spec.Roles = itsProto.Spec.Roles
255254
itsObjCopy.Spec.LifecycleActions = itsProto.Spec.LifecycleActions
256255
itsObjCopy.Spec.Ordinals = itsProto.Spec.Ordinals
257-
// IMPORTANT: The assignedOrdinals fields are managed automatically by the InstanceSet controller.
258-
// itsObjCopy.Spec.AssignedOrdinals = itsProto.Spec.AssignedOrdinals
259-
itsObjCopy.Spec.Instances = copyNMergeITSInstances(itsObjCopy, itsProto)
256+
itsObjCopy.Spec.Instances = itsProto.Spec.Instances
260257
itsObjCopy.Spec.FlatInstanceOrdinal = itsProto.Spec.FlatInstanceOrdinal
261258
itsObjCopy.Spec.OfflineInstances = itsProto.Spec.OfflineInstances
262259
itsObjCopy.Spec.MinReadySeconds = itsProto.Spec.MinReadySeconds
@@ -300,34 +297,6 @@ func copyAndMergeITS(oldITS, newITS *workloads.InstanceSet) *workloads.InstanceS
300297
return itsObjCopy
301298
}
302299

303-
func copyNMergeITSInstances(oldITS, newITS *workloads.InstanceSet) []workloads.InstanceTemplate {
304-
for i, ntpl := range newITS.Spec.Instances {
305-
if idx := slices.IndexFunc(oldITS.Spec.Instances, func(otpl workloads.InstanceTemplate) bool {
306-
return ntpl.Name == otpl.Name
307-
}); idx >= 0 {
308-
newITS.Spec.Instances[i].AssignedOrdinals = oldITS.Spec.Instances[idx].AssignedOrdinals
309-
}
310-
}
311-
for _, otpl := range oldITS.Spec.Instances {
312-
if idx := slices.IndexFunc(newITS.Spec.Instances, func(ntpl workloads.InstanceTemplate) bool {
313-
return ntpl.Name == otpl.Name
314-
}); idx < 0 {
315-
newITS.Spec.Instances = append(newITS.Spec.Instances, workloads.InstanceTemplate{
316-
Name: otpl.Name,
317-
Replicas: ptr.To(int32(0)), // MUST set replicas to 0 explicitly
318-
AssignedOrdinals: otpl.AssignedOrdinals,
319-
})
320-
slices.SortFunc(newITS.Spec.Instances, func(a, b workloads.InstanceTemplate) int {
321-
return strings.Compare(a.Name, b.Name)
322-
})
323-
}
324-
}
325-
newITS.Spec.Instances = slices.DeleteFunc(newITS.Spec.Instances, func(tpl workloads.InstanceTemplate) bool {
326-
return ptr.Deref(tpl.Replicas, 0) == 0 && len(tpl.AssignedOrdinals.Discrete) == 0
327-
})
328-
return newITS.Spec.Instances
329-
}
330-
331300
func checkNRollbackProtoImages(itsObj, itsProto *workloads.InstanceSet) {
332301
if itsObj.Annotations == nil || itsProto.Annotations == nil {
333302
return

controllers/workloads/instanceset_controller_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
4343
"github.com/apecloud/kubeblocks/pkg/constant"
4444
"github.com/apecloud/kubeblocks/pkg/controller/builder"
45+
"github.com/apecloud/kubeblocks/pkg/controller/instancetemplate"
4546
"github.com/apecloud/kubeblocks/pkg/generics"
4647
kbacli "github.com/apecloud/kubeblocks/pkg/kbagent/client"
4748
kbaproto "github.com/apecloud/kubeblocks/pkg/kbagent/proto"
@@ -101,11 +102,7 @@ var _ = Describe("InstanceSet Controller", func() {
101102
itsKey = client.ObjectKeyFromObject(itsObj)
102103

103104
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, set *workloads.InstanceSet) {
104-
if !itsObj.Spec.FlatInstanceOrdinal {
105-
g.Expect(set.Status.ObservedGeneration).Should(BeEquivalentTo(1))
106-
} else {
107-
g.Expect(set.Status.ObservedGeneration).Should(BeEquivalentTo(2))
108-
}
105+
g.Expect(set.Status.ObservedGeneration).Should(BeEquivalentTo(1))
109106
}),
110107
).Should(Succeed())
111108
}
@@ -681,7 +678,8 @@ var _ = Describe("InstanceSet Controller", func() {
681678
mockPodReady(itsObj.Name+"-0", itsObj.Name+"-1", itsObj.Name+"-2")
682679
By("check its status")
683680
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
684-
g.Expect(its.Spec.AssignedOrdinals.Discrete).Should(HaveExactElements(int32(0), int32(1), int32(2)))
681+
g.Expect(its.Status.AssignedOrdinals).Should(HaveKey(instancetemplate.DefaultTemplateName))
682+
g.Expect(its.Status.AssignedOrdinals[instancetemplate.DefaultTemplateName].Discrete).Should(HaveExactElements(int32(0), int32(1), int32(2)))
685683
})).Should(Succeed())
686684

687685
// offline one instance
@@ -692,7 +690,8 @@ var _ = Describe("InstanceSet Controller", func() {
692690
checkPodOrdinal([]int{1}, eventuallyNotExist)
693691
By("check its status")
694692
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
695-
g.Expect(its.Spec.AssignedOrdinals.Discrete).Should(HaveExactElements(int32(0), int32(2)))
693+
g.Expect(its.Status.AssignedOrdinals).Should(HaveKey(instancetemplate.DefaultTemplateName))
694+
g.Expect(its.Status.AssignedOrdinals[instancetemplate.DefaultTemplateName].Discrete).Should(HaveExactElements(int32(0), int32(2)))
696695
})).Should(Succeed())
697696

698697
// scale up
@@ -703,7 +702,8 @@ var _ = Describe("InstanceSet Controller", func() {
703702
mockPodReady(itsObj.Name+"-3", itsObj.Name+"-4")
704703
By("check its status")
705704
Eventually(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
706-
g.Expect(its.Spec.AssignedOrdinals.Discrete).Should(HaveExactElements(int32(0), int32(2), int32(3), int32(4)))
705+
g.Expect(its.Status.AssignedOrdinals).Should(HaveKey(instancetemplate.DefaultTemplateName))
706+
g.Expect(its.Status.AssignedOrdinals[instancetemplate.DefaultTemplateName].Discrete).Should(HaveExactElements(int32(0), int32(2), int32(3), int32(4)))
707707
})).Should(Succeed())
708708

709709
// delete OfflineInstances will not affect running instances
@@ -714,7 +714,8 @@ var _ = Describe("InstanceSet Controller", func() {
714714
checkPodOrdinal([]int{1}, consistentlyNotExist)
715715
By("check its status")
716716
Consistently(testapps.CheckObj(&testCtx, itsKey, func(g Gomega, its *workloads.InstanceSet) {
717-
g.Expect(its.Spec.AssignedOrdinals.Discrete).Should(HaveExactElements(int32(0), int32(2), int32(3), int32(4)))
717+
g.Expect(its.Status.AssignedOrdinals).Should(HaveKey(instancetemplate.DefaultTemplateName))
718+
g.Expect(its.Status.AssignedOrdinals[instancetemplate.DefaultTemplateName].Discrete).Should(HaveExactElements(int32(0), int32(2), int32(3), int32(4)))
718719
})).Should(Succeed())
719720
})
720721
})

0 commit comments

Comments
 (0)