Skip to content

Commit 4342fe3

Browse files
committed
refactor label managers
1 parent 50647a2 commit 4342fe3

28 files changed

Lines changed: 548 additions & 579 deletions

multicluster/clusterinfo/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
const (
25-
ClusterLabelKey = "kusionstack.io/cluster" // Label key for cluster name that will be attached when use Client or Cache to read
25+
ClusterLabelKey = "kusionstack.io/cluster" // Value key for cluster name that will be attached when use Client or Cache to read
2626

2727
EnvClusterAllowList = "CLUSTER_ALLOW_LIST" // Comma separated list of cluster names that are allowed to be accessed
2828
EnvClusterBlockList = "CLUSTER_BLOCK_LIST" // Comma separated list of cluster names that are blocked to be accessed

multicluster/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ var _ = Describe("multicluster", func() {
327327
err = clusterClient.Delete(clusterCtx, &configmap)
328328
Expect(err).NotTo(HaveOccurred())
329329

330-
// Label should be preserved after deleting
330+
// Value should be preserved after deleting
331331
val, ok := configmap.GetLabels()[clusterinfo.ClusterLabelKey]
332332
Expect(ok).To(BeTrue())
333333
Expect(val).To(Equal("cluster2"))
@@ -336,7 +336,7 @@ var _ = Describe("multicluster", func() {
336336
err = clusterClient.Update(clusterCtx, &configmap)
337337
Expect(err).To(HaveOccurred())
338338

339-
// Label should be preserved after updating
339+
// Value should be preserved after updating
340340
val, ok = configmap.GetLabels()[clusterinfo.ClusterLabelKey]
341341
Expect(ok).To(BeTrue())
342342
Expect(val).To(Equal("cluster2"))
Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package api
17+
package v1alpha1
1818

1919
import (
2020
"sigs.k8s.io/controller-runtime/pkg/client"
2121
)
2222

23-
type OperationLabelEnum int
24-
25-
const (
26-
// OperatingLabelPrefix indicates a target is under operation
27-
// set by xset controller
28-
OperatingLabelPrefix OperationLabelEnum = iota
29-
30-
// OperationTypeLabelPrefix indicates the type of operation
31-
// set by xset controller
32-
OperationTypeLabelPrefix
33-
34-
// OperateLabelPrefix indicates a target could start operation
35-
// set by related opsLifecycle controller.
36-
// xset controller will start operation only after this label is set
37-
OperateLabelPrefix
38-
39-
// UndoOperationTypeLabelPrefix indicates a type of operation has been canceled.
40-
// need to be handled by related opsLifecycle controller
41-
UndoOperationTypeLabelPrefix
42-
43-
// ServiceAvailableLabel indicates a target is available for service.
44-
// set by related opsLifecycle controller.
45-
ServiceAvailableLabel
46-
47-
// PreparingDeleteLabel indicates a target is preparing to be deleted.
48-
// set by xset controller,
49-
// handle by related opsLifecycle controller if needed.
50-
PreparingDeleteLabel
51-
)
52-
53-
type LifeCycleLabelManager interface {
54-
Get(labelType OperationLabelEnum) string
55-
}
56-
5723
type OperationType string
5824

5925
var (

xset/api/resourcecontext_types.go renamed to xset/api/v1alpha1/resourcecontext_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package api
17+
package v1alpha1
1818

1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

xset/api/validation/resourcecontext.go renamed to xset/api/v1alpha1/validation/resourcecontext.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121

2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323

24-
"kusionstack.io/kube-utils/xset/api"
24+
"kusionstack.io/kube-utils/xset/api/v1alpha1"
2525
)
2626

2727
// ValidateResourceContextAdapter validates the resource context adapter
28-
func ValidateResourceContextAdapter(adapter api.ResourceContextAdapter) error {
28+
func ValidateResourceContextAdapter(adapter v1alpha1.ResourceContextAdapter) error {
2929
if adapter == nil {
3030
return errors.New("resource context adapter is nil")
3131
}
@@ -43,13 +43,13 @@ func validateResourceContextMeta(t metav1.TypeMeta) error {
4343
return nil
4444
}
4545

46-
func validateResourceContextKey(m map[api.ResourceContextKeyEnum]string) error {
46+
func validateResourceContextKey(m map[v1alpha1.ResourceContextKeyEnum]string) error {
4747
if m == nil {
4848
return errors.New("resource context keys is nil")
4949
}
5050

51-
for i := range api.EnumContextKeyNum {
52-
if _, ok := m[api.ResourceContextKeyEnum(i)]; !ok {
51+
for i := range v1alpha1.EnumContextKeyNum {
52+
if _, ok := m[v1alpha1.ResourceContextKeyEnum(i)]; !ok {
5353
return errors.New("resource context keys is not valid, please add enough context keys")
5454
}
5555
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2024-2025 KusionStack Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package validation

xset/api/xset_controller_types.go renamed to xset/api/v1alpha1/xset_controller_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package api
17+
package v1alpha1
1818

1919
import (
2020
"context"
2121

2222
appsv1 "k8s.io/api/apps/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"sigs.k8s.io/controller-runtime/pkg/client"
25+
26+
"kusionstack.io/kube-utils/xset/api"
2527
)
2628

2729
type XSetController interface {
@@ -42,8 +44,7 @@ type XSetController interface {
4244
SetXSetStatus(object XSetObject, status *XSetStatus)
4345
GetReadyTime(object client.Object) *metav1.Time
4446

45-
GetLifeCycleLabelManager() LifeCycleLabelManager
46-
GetXSetControllerLabelManager() XSetLabelManager
47+
GetXSetControllerLabelManager() api.XSetLabelAnnotationManager
4748
GetScaleInOpsLifecycleAdapter() LifecycleAdapter
4849
GetUpdateOpsLifecycleAdapter() LifecycleAdapter
4950
GetResourceContextAdapter() ResourceContextAdapter
Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package api
17+
package v1alpha1
1818

1919
// +k8s:deepcopy-gen=file
2020

@@ -204,40 +204,3 @@ type OpsPriority struct {
204204
// DeletionCost is the deletion cost of the target
205205
DeletionCost int32
206206
}
207-
208-
type XSetControllerLabelEnum int
209-
210-
const (
211-
EnumXSetControlledLabel XSetControllerLabelEnum = iota
212-
213-
EnumXSetInstanceIdLabel
214-
215-
EnumXSetUpdateIndicationLabel
216-
217-
EnumXSetDeletionIndicationLabel
218-
219-
EnumXSetReplaceIndicationLabel
220-
221-
EnumXSetReplacePairNewIdLabel
222-
223-
EnumXSetReplacePairOriginNameLabel
224-
225-
EnumXSetReplaceByReplaceUpdateLabel
226-
227-
EnumXSetOrphanedLabel
228-
229-
EnumXSetTargetCreatingLabel
230-
231-
EnumXSetTargetCompletingLabel
232-
233-
EnumXSetTargetExcludeIndicationLabel
234-
235-
EnumXSetLastTargetStatusAnnotationKey
236-
)
237-
238-
type XSetLabelManager interface {
239-
Get(labels map[string]string, labelType XSetControllerLabelEnum) (string, bool)
240-
Set(labels map[string]string, labelType XSetControllerLabelEnum, value string)
241-
Delete(labels map[string]string, labelType XSetControllerLabelEnum)
242-
Label(labelType XSetControllerLabelEnum) string
243-
}

xset/api/zz_generated.deepcopy.go renamed to xset/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xset/api/well_knowns.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright 2024-2025 KusionStack Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import appsv1alpha1 "kusionstack.io/kube-api/apps/v1alpha1"
20+
21+
type XSetLabelAnnotationEnum int
22+
23+
const (
24+
// OperatingLabelPrefix indicates a target is under operation
25+
// set by xset controller
26+
OperatingLabelPrefix XSetLabelAnnotationEnum = iota
27+
28+
// OperationTypeLabelPrefix indicates the type of operation
29+
// set by xset controller
30+
OperationTypeLabelPrefix
31+
32+
// OperateLabelPrefix indicates a target could start operation
33+
// set by related opsLifecycle controller.
34+
// xset controller will start operation only after this label is set
35+
OperateLabelPrefix
36+
37+
// UndoOperationTypeLabelPrefix indicates a type of operation has been canceled.
38+
// need to be handled by related opsLifecycle controller
39+
UndoOperationTypeLabelPrefix
40+
41+
// ServiceAvailableLabel indicates a target is available for service.
42+
// set by related opsLifecycle controller.
43+
ServiceAvailableLabel
44+
45+
// PreparingDeleteLabel indicates a target is preparing to be deleted.
46+
// set by xset controller,
47+
// handle by related opsLifecycle controller if needed.
48+
PreparingDeleteLabel
49+
50+
// ControlledByXSetLabel indicates a target is controlled by xset.
51+
// set by xset controller
52+
ControlledByXSetLabel
53+
54+
// XInstanceIdLabelKey is used to attach instance ID on x
55+
XInstanceIdLabelKey
56+
57+
// XSetUpdateIndicationLabelKey is used to indicate a target is updated by xset
58+
XSetUpdateIndicationLabelKey
59+
60+
// XDeletionIndicationLabelKey is used to indicate a target is deleted by xset
61+
XDeletionIndicationLabelKey
62+
63+
// XReplaceIndicationLabelKey is used to indicate a target is replaced by xset
64+
XReplaceIndicationLabelKey
65+
66+
// XReplacePairNewId is used to indicate the new created target on replace origin target
67+
XReplacePairNewId
68+
69+
// XReplacePairOriginName is used to indicate replace origin target name on the new created target
70+
XReplacePairOriginName
71+
72+
// XReplaceByReplaceUpdateLabelKey indicates a target is replaced by update by xset
73+
XReplaceByReplaceUpdateLabelKey
74+
75+
// XOrphanedIndicationLabelKey is used to indicate a target is orphaned by xset
76+
XOrphanedIndicationLabelKey
77+
78+
// XCreatingLabel indicates a target is creating by xset
79+
XCreatingLabel
80+
81+
// XCompletingLabel indicates a target is completing by xset
82+
XCompletingLabel
83+
84+
// XExcludeIndicationLabelKey is used to indicate a target is excluded by xset
85+
XExcludeIndicationLabelKey
86+
87+
// LastXStatusAnnotationKey is used to record the last status of a target by xset
88+
LastXStatusAnnotationKey
89+
)
90+
91+
type XSetLabelAnnotationManager interface {
92+
Get(labels map[string]string, labelType XSetLabelAnnotationEnum) (string, bool)
93+
Set(labels map[string]string, labelType XSetLabelAnnotationEnum, value string)
94+
Delete(labels map[string]string, labelType XSetLabelAnnotationEnum)
95+
Value(labelType XSetLabelAnnotationEnum) string
96+
GetWellKnownLabelPrefixesWithID() []string
97+
}
98+
99+
var defaultXSetLabelAnnotationManager = map[XSetLabelAnnotationEnum]string{
100+
OperatingLabelPrefix: appsv1alpha1.PodOperatingLabelPrefix,
101+
OperationTypeLabelPrefix: appsv1alpha1.PodOperationTypeLabelPrefix,
102+
OperateLabelPrefix: appsv1alpha1.PodOperateLabelPrefix,
103+
UndoOperationTypeLabelPrefix: appsv1alpha1.PodUndoOperationTypeLabelPrefix,
104+
ServiceAvailableLabel: appsv1alpha1.PodServiceAvailableLabel,
105+
PreparingDeleteLabel: appsv1alpha1.PodPreparingDeleteLabel,
106+
107+
ControlledByXSetLabel: appsv1alpha1.ControlledByKusionStackLabelKey,
108+
XInstanceIdLabelKey: appsv1alpha1.PodInstanceIDLabelKey,
109+
XSetUpdateIndicationLabelKey: appsv1alpha1.CollaSetUpdateIndicateLabelKey,
110+
XDeletionIndicationLabelKey: appsv1alpha1.PodDeletionIndicationLabelKey,
111+
XReplaceIndicationLabelKey: appsv1alpha1.PodReplaceIndicationLabelKey,
112+
XReplacePairNewId: appsv1alpha1.PodReplacePairNewId,
113+
XReplacePairOriginName: appsv1alpha1.PodReplacePairOriginName,
114+
XReplaceByReplaceUpdateLabelKey: appsv1alpha1.PodReplaceByReplaceUpdateLabelKey,
115+
XOrphanedIndicationLabelKey: appsv1alpha1.PodOrphanedIndicateLabelKey,
116+
XCreatingLabel: appsv1alpha1.PodCreatingLabel,
117+
XCompletingLabel: appsv1alpha1.PodCompletingLabel,
118+
XExcludeIndicationLabelKey: appsv1alpha1.PodExcludeIndicationLabelKey,
119+
120+
LastXStatusAnnotationKey: appsv1alpha1.LastPodStatusAnnotationKey,
121+
}
122+
123+
func NewXSetLabelAnnotationManager() XSetLabelAnnotationManager {
124+
return &xSetLabelAnnotationManager{
125+
labelManager: defaultXSetLabelAnnotationManager,
126+
}
127+
}
128+
129+
type xSetLabelAnnotationManager struct {
130+
labelManager map[XSetLabelAnnotationEnum]string
131+
}
132+
133+
func (m *xSetLabelAnnotationManager) Get(labels map[string]string, key XSetLabelAnnotationEnum) (string, bool) {
134+
if labels == nil {
135+
return "", false
136+
}
137+
labelKey := m.labelManager[key]
138+
val, exist := labels[labelKey]
139+
return val, exist
140+
}
141+
142+
func (m *xSetLabelAnnotationManager) Set(labels map[string]string, key XSetLabelAnnotationEnum, val string) {
143+
if labels == nil {
144+
labels = make(map[string]string)
145+
}
146+
labelKey := m.labelManager[key]
147+
labels[labelKey] = val
148+
}
149+
150+
func (m *xSetLabelAnnotationManager) Delete(labels map[string]string, key XSetLabelAnnotationEnum) {
151+
if labels == nil {
152+
return
153+
}
154+
labelKey := m.labelManager[key]
155+
delete(labels, labelKey)
156+
}
157+
158+
func (m *xSetLabelAnnotationManager) GetWellKnownLabelPrefixesWithID() []string {
159+
return []string{
160+
m.labelManager[OperatingLabelPrefix],
161+
m.labelManager[OperationTypeLabelPrefix],
162+
m.labelManager[UndoOperationTypeLabelPrefix],
163+
m.labelManager[OperatingLabelPrefix],
164+
}
165+
}
166+
167+
func (m *xSetLabelAnnotationManager) Value(key XSetLabelAnnotationEnum) string {
168+
return m.labelManager[key]
169+
}

0 commit comments

Comments
 (0)