From f080795b400dc7453d42bf0ae21b1aefd0ee33bc Mon Sep 17 00:00:00 2001 From: Wahab Ali Date: Tue, 17 Dec 2024 09:59:40 -0500 Subject: [PATCH] Add labels to Cluster when deploying clusterTemplate --- api/v1alpha1/managedcluster_types.go | 27 +++++++++++++++ .../controller/managedcluster_controller.go | 34 ++++++------------- .../cluster/aws-eks/templates/cluster.yaml | 4 +++ templates/cluster/aws-eks/values.yaml | 2 ++ .../aws-hosted-cp/templates/cluster.yaml | 4 +++ templates/cluster/aws-hosted-cp/values.yaml | 2 ++ .../aws-standalone-cp/templates/cluster.yaml | 4 +++ .../cluster/aws-standalone-cp/values.yaml | 4 +++ .../azure-hosted-cp/templates/cluster.yaml | 4 +++ templates/cluster/azure-hosted-cp/values.yaml | 2 ++ .../templates/cluster.yaml | 4 +++ .../cluster/azure-standalone-cp/values.yaml | 2 ++ .../vsphere-hosted-cp/templates/cluster.yaml | 4 +++ .../cluster/vsphere-hosted-cp/values.yaml | 2 ++ .../templates/cluster.yaml | 4 +++ .../cluster/vsphere-standalone-cp/values.yaml | 2 ++ 16 files changed, 82 insertions(+), 23 deletions(-) diff --git a/api/v1alpha1/managedcluster_types.go b/api/v1alpha1/managedcluster_types.go index 404a6de1..45eda7cb 100644 --- a/api/v1alpha1/managedcluster_types.go +++ b/api/v1alpha1/managedcluster_types.go @@ -15,6 +15,9 @@ package v1alpha1 import ( + "encoding/json" + "fmt" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -124,9 +127,33 @@ func (in *ManagedCluster) HelmValues() (values map[string]any, err error) { if in.Spec.Config != nil { err = yaml.Unmarshal(in.Spec.Config.Raw, &values) } + return values, err } +func (in *ManagedCluster) SetHelmValues(values map[string]any) error { + b, err := json.Marshal(values) + if err != nil { + return fmt.Errorf("error marshalling values: %w", err) + } + + in.Spec.Config = &apiextensionsv1.JSON{Raw: b} + return nil +} + +func (in *ManagedCluster) AddHelmValues(fn func(map[string]any) error) error { + values, err := in.HelmValues() + if err != nil { + return err + } + + if err := fn(values); err != nil { + return err + } + + return in.SetHelmValues(values) +} + func (in *ManagedCluster) GetConditions() *[]metav1.Condition { return &in.Status.Conditions } diff --git a/internal/controller/managedcluster_controller.go b/internal/controller/managedcluster_controller.go index 3dcc43ad..c1fcd2e3 100644 --- a/internal/controller/managedcluster_controller.go +++ b/internal/controller/managedcluster_controller.go @@ -16,7 +16,6 @@ package controller import ( "context" - "encoding/json" "errors" "fmt" "slices" @@ -31,7 +30,6 @@ import ( "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart" corev1 "k8s.io/api/core/v1" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -297,13 +295,19 @@ func (r *ManagedClusterReconciler) updateCluster(ctx context.Context, mc *hmc.Ma return ctrl.Result{}, nil } - helmValues, err := setIdentityHelmValues(mc.Spec.Config, cred.Spec.IdentityRef) - if err != nil { - return ctrl.Result{}, fmt.Errorf("error setting identity values: %w", err) - } + mc.AddHelmValues(func(values map[string]any) error { + values["clusterIdentity"] = cred.Spec.IdentityRef + + if _, ok := values["clusterLabels"]; !ok { + // Use the ManagedCluster's own labels if not defined. + values["clusterLabels"] = mc.GetObjectMeta().GetLabels() + } + + return nil + }) hrReconcileOpts := helm.ReconcileHelmReleaseOpts{ - Values: helmValues, + Values: mc.Spec.Config, OwnerReference: &metav1.OwnerReference{ APIVersion: hmc.GroupVersion.String(), Kind: hmc.ManagedClusterKind, @@ -779,22 +783,6 @@ func (r *ManagedClusterReconciler) reconcileCredentialPropagation(ctx context.Co return nil } -func setIdentityHelmValues(values *apiextensionsv1.JSON, idRef *corev1.ObjectReference) (*apiextensionsv1.JSON, error) { - var valuesJSON map[string]any - err := json.Unmarshal(values.Raw, &valuesJSON) - if err != nil { - return nil, fmt.Errorf("error unmarshalling values: %w", err) - } - - valuesJSON["clusterIdentity"] = idRef - valuesRaw, err := json.Marshal(valuesJSON) - if err != nil { - return nil, fmt.Errorf("error marshalling values: %w", err) - } - - return &apiextensionsv1.JSON{Raw: valuesRaw}, nil -} - func (r *ManagedClusterReconciler) setAvailableUpgrades(ctx context.Context, managedCluster *hmc.ManagedCluster, template *hmc.ClusterTemplate) error { if template == nil { return nil diff --git a/templates/cluster/aws-eks/templates/cluster.yaml b/templates/cluster/aws-eks/templates/cluster.yaml index dca896de..a4d01b97 100644 --- a/templates/cluster/aws-eks/templates/cluster.yaml +++ b/templates/cluster/aws-eks/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/aws-eks/values.yaml b/templates/cluster/aws-eks/values.yaml index 188b7818..48a35c02 100644 --- a/templates/cluster/aws-eks/values.yaml +++ b/templates/cluster/aws-eks/values.yaml @@ -9,6 +9,8 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: + # EKS cluster parameters region: "" sshKeyName: "" diff --git a/templates/cluster/aws-hosted-cp/templates/cluster.yaml b/templates/cluster/aws-hosted-cp/templates/cluster.yaml index eaef59c5..49380369 100644 --- a/templates/cluster/aws-hosted-cp/templates/cluster.yaml +++ b/templates/cluster/aws-hosted-cp/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/aws-hosted-cp/values.yaml b/templates/cluster/aws-hosted-cp/values.yaml index c018fe1f..58a807be 100644 --- a/templates/cluster/aws-hosted-cp/values.yaml +++ b/templates/cluster/aws-hosted-cp/values.yaml @@ -9,6 +9,8 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: + # AWS cluster parameters vpcID: "" region: "" diff --git a/templates/cluster/aws-standalone-cp/templates/cluster.yaml b/templates/cluster/aws-standalone-cp/templates/cluster.yaml index cb3425af..1edbf204 100644 --- a/templates/cluster/aws-standalone-cp/templates/cluster.yaml +++ b/templates/cluster/aws-standalone-cp/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/aws-standalone-cp/values.yaml b/templates/cluster/aws-standalone-cp/values.yaml index 6f0a019d..7716baed 100644 --- a/templates/cluster/aws-standalone-cp/values.yaml +++ b/templates/cluster/aws-standalone-cp/values.yaml @@ -10,6 +10,10 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: +# env: dev3 +# region: region1 + # AWS cluster parameters region: "" sshKeyName: "" diff --git a/templates/cluster/azure-hosted-cp/templates/cluster.yaml b/templates/cluster/azure-hosted-cp/templates/cluster.yaml index 74bd07a5..1bc60236 100644 --- a/templates/cluster/azure-hosted-cp/templates/cluster.yaml +++ b/templates/cluster/azure-hosted-cp/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/azure-hosted-cp/values.yaml b/templates/cluster/azure-hosted-cp/values.yaml index b4e1b81e..1e748cf9 100644 --- a/templates/cluster/azure-hosted-cp/values.yaml +++ b/templates/cluster/azure-hosted-cp/values.yaml @@ -10,6 +10,8 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: + # Azure cluster parameters location: "" subscriptionID: "" diff --git a/templates/cluster/azure-standalone-cp/templates/cluster.yaml b/templates/cluster/azure-standalone-cp/templates/cluster.yaml index 2ce7581f..6bb4fc95 100644 --- a/templates/cluster/azure-standalone-cp/templates/cluster.yaml +++ b/templates/cluster/azure-standalone-cp/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/azure-standalone-cp/values.yaml b/templates/cluster/azure-standalone-cp/values.yaml index bd7504aa..74b4eafc 100644 --- a/templates/cluster/azure-standalone-cp/values.yaml +++ b/templates/cluster/azure-standalone-cp/values.yaml @@ -10,6 +10,8 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: + # Azure cluster parameters location: "" subscriptionID: "" diff --git a/templates/cluster/vsphere-hosted-cp/templates/cluster.yaml b/templates/cluster/vsphere-hosted-cp/templates/cluster.yaml index 87c04260..b85007c1 100644 --- a/templates/cluster/vsphere-hosted-cp/templates/cluster.yaml +++ b/templates/cluster/vsphere-hosted-cp/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/vsphere-hosted-cp/values.yaml b/templates/cluster/vsphere-hosted-cp/values.yaml index 187774dc..2f20e08c 100644 --- a/templates/cluster/vsphere-hosted-cp/values.yaml +++ b/templates/cluster/vsphere-hosted-cp/values.yaml @@ -10,6 +10,8 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: + # vSphere cluster parameters clusterIdentity: name: "" diff --git a/templates/cluster/vsphere-standalone-cp/templates/cluster.yaml b/templates/cluster/vsphere-standalone-cp/templates/cluster.yaml index c2437c24..cd8ed32d 100644 --- a/templates/cluster/vsphere-standalone-cp/templates/cluster.yaml +++ b/templates/cluster/vsphere-standalone-cp/templates/cluster.yaml @@ -2,6 +2,10 @@ apiVersion: cluster.x-k8s.io/v1beta1 kind: Cluster metadata: name: {{ include "cluster.name" . }} + labels: + {{- with .Values.clusterLabels }} + {{- toYaml . | nindent 4 }} + {{- end }} spec: {{- with .Values.clusterNetwork }} clusterNetwork: diff --git a/templates/cluster/vsphere-standalone-cp/values.yaml b/templates/cluster/vsphere-standalone-cp/values.yaml index eb64d708..d92d63f0 100644 --- a/templates/cluster/vsphere-standalone-cp/values.yaml +++ b/templates/cluster/vsphere-standalone-cp/values.yaml @@ -10,6 +10,8 @@ clusterNetwork: cidrBlocks: - "10.96.0.0/12" +clusterLabels: + # vSphere cluster parameters clusterIdentity: name: ""