Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide ability to add labels to Cluster object #809

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions api/v1alpha1/clusterdeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -124,11 +127,39 @@ type ClusterDeployment struct {
Status ClusterDeploymentStatus `json:"status,omitempty"`
}

func (in *ClusterDeployment) HelmValues() (values map[string]any, err error) {
func (in *ClusterDeployment) HelmValues() (map[string]any, error) {
var values map[string]any

if in.Spec.Config != nil {
err = yaml.Unmarshal(in.Spec.Config.Raw, &values)
if err := yaml.Unmarshal(in.Spec.Config.Raw, &values); err != nil {
return nil, fmt.Errorf("error unmarshalling helm values for clusterTemplate %s: %w", in.Spec.Template, err)
}
}
return values, err

return values, nil
}

func (in *ClusterDeployment) SetHelmValues(values map[string]any) error {
b, err := json.Marshal(values)
if err != nil {
return fmt.Errorf("error marshalling helm values for clusterTemplate %s: %w", in.Spec.Template, err)
}

in.Spec.Config = &apiextensionsv1.JSON{Raw: b}
return nil
}

func (in *ClusterDeployment) 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 *ClusterDeployment) GetConditions() *[]metav1.Condition {
Expand Down
34 changes: 12 additions & 22 deletions internal/controller/clusterdeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
Expand All @@ -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"
Expand Down Expand Up @@ -297,13 +295,21 @@ func (r *ClusterDeploymentReconciler) updateCluster(ctx context.Context, mc *hmc
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)
if 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
}); err != nil {
return ctrl.Result{}, err
}

hrReconcileOpts := helm.ReconcileHelmReleaseOpts{
Values: helmValues,
Values: mc.Spec.Config,
OwnerReference: &metav1.OwnerReference{
APIVersion: hmc.GroupVersion.String(),
Kind: hmc.ClusterDeploymentKind,
Expand Down Expand Up @@ -798,22 +804,6 @@ func (r *ClusterDeploymentReconciler) reconcileCredentialPropagation(ctx context
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 *ClusterDeploymentReconciler) setAvailableUpgrades(ctx context.Context, clusterDeployment *hmc.ClusterDeployment, template *hmc.ClusterTemplate) error {
if template == nil {
return nil
Expand Down
3 changes: 3 additions & 0 deletions templates/cluster/aws-eks/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/aws-eks/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# EKS cluster parameters
region: ""
sshKeyName: ""
Expand Down
3 changes: 3 additions & 0 deletions templates/cluster/aws-hosted-cp/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/aws-hosted-cp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# AWS cluster parameters
vpcID: ""
region: ""
Expand Down
3 changes: 3 additions & 0 deletions templates/cluster/aws-standalone-cp/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/aws-standalone-cp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# AWS cluster parameters
region: ""
sshKeyName: ""
Expand Down
3 changes: 3 additions & 0 deletions templates/cluster/azure-hosted-cp/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/azure-hosted-cp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# Azure cluster parameters
location: ""
subscriptionID: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/azure-standalone-cp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# Azure cluster parameters
location: ""
subscriptionID: ""
Expand Down
3 changes: 3 additions & 0 deletions templates/cluster/vsphere-hosted-cp/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/vsphere-hosted-cp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# vSphere cluster parameters
clusterIdentity:
name: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: {{ include "cluster.name" . }}
{{- if .Values.clusterLabels }}
labels: {{- toYaml .Values.clusterLabels | nindent 4}}
{{- end }}
spec:
{{- with .Values.clusterNetwork }}
clusterNetwork:
Expand Down
2 changes: 2 additions & 0 deletions templates/cluster/vsphere-standalone-cp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clusterNetwork:
cidrBlocks:
- "10.96.0.0/12"

clusterLabels: {}

# vSphere cluster parameters
clusterIdentity:
name: ""
Expand Down
Loading