Skip to content

Commit f32f857

Browse files
committed
feat: add labels to managedcluster resource if present and have the ability to pass it through helm chart.
Signed-off-by: ramekris3163 <[email protected]>
1 parent 3df894d commit f32f857

File tree

8 files changed

+502
-5
lines changed

8 files changed

+502
-5
lines changed

deploy/klusterlet/chart/klusterlet/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ klusterlet:
127127
# - feature: ""
128128
# mode: ""
129129
# clientCertExpirationSeconds: 600
130+
# clusterLabels:
131+
# environment: "production"
132+
# region: "us-west-2"
130133
workConfiguration: {}
131134
# featureGates:
132135
# - feature: ""

manifests/klusterlet/management/klusterlet-agent-deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ spec:
103103
{{if .ClusterAnnotationsString}}
104104
- "--cluster-annotations={{ .ClusterAnnotationsString }}"
105105
{{end}}
106+
{{if .ClusterLabelsString}}
107+
- "--cluster-labels={{ .ClusterLabelsString }}"
108+
{{end}}
106109
{{if eq .InstallMode "SingletonHosted"}}
107110
- "--spoke-kubeconfig=/spoke/config/kubeconfig"
108111
- "--terminate-on-files=/spoke/config/kubeconfig"

manifests/klusterlet/management/klusterlet-registration-deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ spec:
8888
{{if .ClusterAnnotationsString}}
8989
- "--cluster-annotations={{ .ClusterAnnotationsString }}"
9090
{{end}}
91+
{{if .ClusterLabelsString}}
92+
- "--cluster-labels={{ .ClusterLabelsString }}"
93+
{{end}}
9194
{{if gt .RegistrationKubeAPIQPS 0.0}}
9295
- "--kube-api-qps={{ .RegistrationKubeAPIQPS }}"
9396
{{end}}

pkg/operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ type klusterletConfig struct {
167167
Replica int32
168168
ClientCertExpirationSeconds int32
169169
ClusterAnnotationsString string
170+
ClusterLabelsString string
170171
RegistrationKubeAPIQPS float32
171172
RegistrationKubeAPIBurst int32
172173
WorkKubeAPIQPS float32
@@ -389,6 +390,13 @@ func (n *klusterletController) sync(ctx context.Context, controllerContext facto
389390
annotationsArray = append(annotationsArray, fmt.Sprintf("%s=%s", k, v))
390391
}
391392
config.ClusterAnnotationsString = strings.Join(annotationsArray, ",")
393+
394+
// construct cluster labels string, the final format is "key1=value1,key2=value2"
395+
var labelsArray []string
396+
for k, v := range klusterlet.Spec.RegistrationConfiguration.ClusterLabels {
397+
labelsArray = append(labelsArray, fmt.Sprintf("%s=%s", k, v))
398+
}
399+
config.ClusterLabelsString = strings.Join(labelsArray, ",")
392400
}
393401

394402
config.AboutAPIEnabled = helpers.FeatureGateEnabled(

pkg/registration/spoke/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type SpokeAgentOptions struct {
3636
MaxCustomClusterClaims int
3737
ReservedClusterClaimSuffixes []string
3838
ClusterAnnotations map[string]string
39+
ClusterLabels map[string]string
3940

4041
RegisterDriverOption *registerfactory.Options
4142
}
@@ -76,6 +77,7 @@ func (o *SpokeAgentOptions) AddFlags(fs *pflag.FlagSet) {
7677
"A list of suffixes for reserved cluster claims.")
7778
fs.StringToStringVar(&o.ClusterAnnotations, "cluster-annotations", o.ClusterAnnotations, `the annotations with the reserve
7879
prefix "agent.open-cluster-management.io" set on ManagedCluster when creating only, other actors can update it afterwards.`)
80+
fs.StringToStringVar(&o.ClusterLabels, "cluster-labels", o.ClusterLabels, `the labels set on ManagedCluster when creating only, other actors can update it afterwards.`)
7981

8082
o.RegisterDriverOption.AddFlags(fs)
8183
}

pkg/registration/spoke/registration/creating_controller.go

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ func (c *managedClusterCreatingController) sync(ctx context.Context, syncCtx fac
8989
managedCluster = decorator(managedCluster)
9090
}
9191

92-
if len(existingCluster.Spec.ManagedClusterClientConfigs) == len(managedCluster.Spec.ManagedClusterClientConfigs) {
92+
// Check if any updates are needed (ClientConfigs, Labels, or Annotations)
93+
if equalClientConfigs(existingCluster.Spec.ManagedClusterClientConfigs, managedCluster.Spec.ManagedClusterClientConfigs) &&
94+
equalLabels(existingCluster.Labels, managedCluster.Labels) &&
95+
equalAnnotations(existingCluster.Annotations, managedCluster.Annotations) {
9396
return nil
9497
}
9598

96-
// update ManagedClusterClientConfigs in ManagedCluster
99+
// update ManagedCluster (ClientConfigs, Labels, and Annotations)
97100
_, err = c.hubClusterClient.ClusterV1().ManagedClusters().Update(ctx, managedCluster, metav1.UpdateOptions{})
98101
// ManagedClusterClientConfigs in ManagedCluster is only allowed updated during bootstrap.
99102
// After bootstrap secret expired, an unauthorized error will be got, skip it
@@ -112,15 +115,33 @@ func skipUnauthorizedError(err error) error {
112115
return err
113116
}
114117

118+
// AnnotationDecorator set annotations from annotation map to ManagedCluster
115119
func AnnotationDecorator(annotations map[string]string) ManagedClusterDecorator {
116120
return func(cluster *clusterv1.ManagedCluster) *clusterv1.ManagedCluster {
117-
filteredAnnotations := commonhelpers.FilterClusterAnnotations(annotations)
118121
if cluster.Annotations == nil {
119122
cluster.Annotations = make(map[string]string)
120123
}
121-
for key, value := range filteredAnnotations {
122-
cluster.Annotations[key] = value
124+
125+
filteredAnnotations := commonhelpers.FilterClusterAnnotations(annotations)
126+
for k, v := range filteredAnnotations {
127+
cluster.Annotations[k] = v
123128
}
129+
130+
return cluster
131+
}
132+
}
133+
134+
// LabelDecorator set labels from label map to ManagedCluster
135+
func LabelDecorator(labels map[string]string) ManagedClusterDecorator {
136+
return func(cluster *clusterv1.ManagedCluster) *clusterv1.ManagedCluster {
137+
if cluster.Labels == nil {
138+
cluster.Labels = make(map[string]string)
139+
}
140+
141+
for k, v := range labels {
142+
cluster.Labels[k] = v
143+
}
144+
124145
return cluster
125146
}
126147
}
@@ -148,3 +169,46 @@ func ClientConfigDecorator(externalServerURLs []string, caBundle []byte) Managed
148169
return cluster
149170
}
150171
}
172+
173+
// equalClientConfigs compares two ClientConfig slices for equality
174+
func equalClientConfigs(configs1, configs2 []clusterv1.ClientConfig) bool {
175+
if len(configs1) != len(configs2) {
176+
return false
177+
}
178+
for i, config1 := range configs1 {
179+
config2 := configs2[i]
180+
if config1.URL != config2.URL {
181+
return false
182+
}
183+
if string(config1.CABundle) != string(config2.CABundle) {
184+
return false
185+
}
186+
}
187+
return true
188+
}
189+
190+
// equalLabels compares two label maps for equality
191+
func equalLabels(labels1, labels2 map[string]string) bool {
192+
if len(labels1) != len(labels2) {
193+
return false
194+
}
195+
for k, v := range labels1 {
196+
if labels2[k] != v {
197+
return false
198+
}
199+
}
200+
return true
201+
}
202+
203+
// equalAnnotations compares two annotation maps for equality
204+
func equalAnnotations(annotations1, annotations2 map[string]string) bool {
205+
if len(annotations1) != len(annotations2) {
206+
return false
207+
}
208+
for k, v := range annotations1 {
209+
if annotations2[k] != v {
210+
return false
211+
}
212+
}
213+
return true
214+
}

0 commit comments

Comments
 (0)