Skip to content

Commit 923d2b1

Browse files
authored
feat(sync): add labels to kubeconfig file (#27)
1 parent b3e06a7 commit 923d2b1

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

cmd/sync.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"crypto/sha256"
99
"encoding/hex"
10+
"encoding/json"
1011
"fmt"
1112
"log"
1213
"maps"
@@ -154,10 +155,24 @@ func buildIncomingKubeconfig(items []v1alpha1.ClusterKubeconfig) (*clientcmdapi.
154155

155156
if len(ckc.Spec.Kubeconfig.Clusters) > 0 {
156157
clusterItem := ckc.Spec.Kubeconfig.Clusters[0]
158+
157159
kubeconfig.Clusters[clusterItem.Name] = &clientcmdapi.Cluster{
158160
Server: clusterItem.Cluster.Server,
159161
CertificateAuthorityData: clusterItem.Cluster.CertificateAuthorityData,
160162
}
163+
164+
// Add/overwrite a "labels" named extension with the labels from the ClusterKubeconfig metadata.
165+
if len(ckc.Labels) > 0 {
166+
labelsJSON, err := json.Marshal(ckc.Labels)
167+
if err != nil {
168+
return nil, fmt.Errorf("failed to marshal labels for cluster %q: %w", clusterItem.Name, err)
169+
}
170+
if kubeconfig.Clusters[clusterItem.Name].Extensions == nil {
171+
kubeconfig.Clusters[clusterItem.Name].Extensions = map[string]runtime.Object{}
172+
}
173+
kubeconfig.Clusters[clusterItem.Name].Extensions["labels"] = &runtime.Unknown{Raw: labelsJSON}
174+
}
175+
161176
}
162177
}
163178

@@ -267,9 +282,10 @@ func mergeKubeconfig(localConfig *clientcmdapi.Config, serverConfig *clientcmdap
267282
// Add the managed cluster from serverConfig to localConfig
268283
localConfig.Clusters[managedName] = serverCluster
269284
} else {
270-
// Check if Server or CertificateAuthorityData has changed
285+
// Check if Server, CertificateAuthorityData or the labels extension has changed
271286
if localCluster.Server != serverCluster.Server ||
272-
!bytes.Equal(localCluster.CertificateAuthorityData, serverCluster.CertificateAuthorityData) {
287+
!bytes.Equal(localCluster.CertificateAuthorityData, serverCluster.CertificateAuthorityData) ||
288+
!labelsExtensionEqual(localCluster.Extensions, serverCluster.Extensions) {
273289
localConfig.Clusters[managedName] = serverCluster
274290
}
275291
}
@@ -468,6 +484,34 @@ func mergeAuthInfo(serverAuth, localAuth *clientcmdapi.AuthInfo) *clientcmdapi.A
468484
return mergedAuth
469485
}
470486

487+
// labelsExtensionEqual returns true if the \"labels\" named extension is equal in both maps.
488+
func labelsExtensionEqual(a, b map[string]runtime.Object) bool {
489+
ar := extensionRaw(a, "labels")
490+
br := extensionRaw(b, "labels")
491+
return bytes.Equal(ar, br)
492+
}
493+
494+
// extensionRaw extracts the raw JSON bytes for the given extension name, if present.
495+
func extensionRaw(m map[string]runtime.Object, name string) []byte {
496+
if m == nil {
497+
return nil
498+
}
499+
obj, ok := m[name]
500+
if !ok || obj == nil {
501+
return nil
502+
}
503+
switch t := obj.(type) {
504+
case *runtime.Unknown:
505+
return bytes.TrimSpace(t.Raw)
506+
default:
507+
b, err := json.Marshal(t)
508+
if err != nil {
509+
return nil
510+
}
511+
return bytes.TrimSpace(b)
512+
}
513+
}
514+
471515
func configWithContext(context, kubeconfigPath string) (*rest.Config, error) {
472516
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
473517
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},

0 commit comments

Comments
 (0)