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

Apply Helm chart global labels and annotations to all resources in the chart #662

Merged
merged 1 commit into from
Jul 17, 2024
Merged
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
5 changes: 5 additions & 0 deletions docs/other/041_global_labels.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,10 @@ helm install \
coherence/coherence-operator
----

[NOTE]
====
When setting the Helm chart values `globalLabels` or `globalAnnotations` any labels and annotations specified will also
be applied to all the resources installed by the Helm Chart too.
====


42 changes: 40 additions & 2 deletions helm-charts/coherence-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ kind: Secret
metadata:
name: {{ default "coherence-webhook-server-cert" .Values.webhookCertSecret }}
namespace: {{ .Release.Namespace }}
{{- if (.Values.globalLabels) }}
labels:
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
{{- end }}
---
apiVersion: v1
Expand All @@ -20,6 +28,13 @@ metadata:
app.kubernetes.io/component: webhook
app.kubernetes.io/part-of: coherence-operator
app.kubernetes.io/managed-by: helm
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
spec:
ports:
- name: webhook
Expand All @@ -44,6 +59,13 @@ metadata:
app.kubernetes.io/component: rest
app.kubernetes.io/part-of: coherence-operator
app.kubernetes.io/managed-by: helm
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
spec:
ports:
- name: http-rest
Expand All @@ -69,11 +91,19 @@ metadata:
app.kubernetes.io/part-of: coherence-operator
app.kubernetes.io/managed-by: helm
app.kubernetes.io/created-by: controller-manager
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if .Values.deploymentLabels }}
{{ toYaml .Values.deploymentLabels | indent 4 }}
{{- end }}
{{- if .Values.deploymentAnnotations }}
{{- if or (.Values.deploymentAnnotations) (.Values.globalAnnotations) }}
annotations:
{{- end }}
{{- if (.Values.globalAnnotations) }}
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
{{- if .Values.deploymentAnnotations }}
{{ toYaml .Values.deploymentAnnotations | indent 4 }}
{{- end }}
spec:
Expand All @@ -92,11 +122,19 @@ spec:
app.kubernetes.io/part-of: coherence-operator
app.kubernetes.io/managed-by: helm
app.kubernetes.io/created-by: controller-manager
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 8 }}
{{- end }}
{{- if .Values.labels }}
{{ toYaml .Values.labels | indent 8 }}
{{- end }}
{{- if .Values.annotations }}
{{- if or (.Values.annotations) (.Values.globalAnnotations) }}
annotations:
{{- end }}
{{- if (.Values.globalAnnotations) }}
{{ toYaml .Values.globalAnnotations | indent 8 }}
{{- end }}
{{- if .Values.annotations }}
{{ toYaml .Values.annotations | indent 8 }}
{{- end }}
spec:
Expand Down
63 changes: 63 additions & 0 deletions helm-charts/coherence-operator/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
---
{{- if .Values.clusterRoles }}
# -------------------------------------------------------------
Expand All @@ -21,6 +28,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
rules:
- apiGroups:
- apiextensions.k8s.io
Expand Down Expand Up @@ -54,6 +68,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
Expand All @@ -78,6 +99,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
rules:
- apiGroups:
- ""
Expand All @@ -99,6 +127,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
Expand All @@ -124,6 +159,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
rules:
- apiGroups:
- ""
Expand Down Expand Up @@ -225,6 +267,13 @@ metadata:
namespace: {{ .Release.Namespace }}
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
roleRef:
apiGroup: rbac.authorization.k8s.io
{{- if .Values.clusterRoles }}
Expand All @@ -248,6 +297,13 @@ metadata:
name: leader-election-role
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
rules:
- apiGroups:
- ""
Expand Down Expand Up @@ -291,6 +347,13 @@ metadata:
name: leader-election-rolebinding
labels:
control-plane: coherence
{{- if (.Values.globalLabels) }}
{{ toYaml .Values.globalLabels | indent 4 }}
{{- end }}
{{- if (.Values.globalAnnotations) }}
annotations:
{{ toYaml .Values.globalAnnotations | indent 4 }}
{{- end }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
Expand Down
164 changes: 131 additions & 33 deletions test/e2e/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,40 +583,138 @@ func TestGlobalLabelsAndAnnotations(t *testing.T) {
"--set", "globalAnnotations.four=annotation-four")
g.Expect(err).NotTo(HaveOccurred())
AssertHelmInstallWithStatefulSetSubTest(t, "basic", cmd, g, AssertLabelsAndAnnotations)
}

func TestGlobalLabelsOnOperatorResources(t *testing.T) {
g := NewGomegaWithT(t)

result, err := helmInstall("--set", "globalLabels.one=label-one",
"--set", "globalLabels.two=label-two")

g.Expect(err).NotTo(HaveOccurred())
g.Expect(result).NotTo(BeNil())

dep := &appsv1.Deployment{}
err = result.Get("coherence-operator", dep)
g.Expect(err).NotTo(HaveOccurred())

labels := dep.Labels
actual, found := labels["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-one"))
actual, found = labels["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-two"))

labels = dep.Spec.Template.Labels
actual, found = labels["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-one"))
actual, found = labels["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-two"))

svc := &corev1.Service{}
err = result.Get("coherence-operator-webhook", svc)
g.Expect(err).NotTo(HaveOccurred())

labels = svc.Labels
actual, found = labels["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-one"))
actual, found = labels["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-two"))

svc = &corev1.Service{}
err = result.Get("coherence-operator-rest", svc)
g.Expect(err).NotTo(HaveOccurred())

labels = svc.Labels
actual, found = labels["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-one"))
actual, found = labels["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-two"))

sec := &corev1.Secret{}
err = result.Get("coherence-webhook-server-cert", sec)
g.Expect(err).NotTo(HaveOccurred())

labels = sec.Labels
actual, found = labels["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-one"))
actual, found = labels["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("label-two"))
}

func TestGlobalAnnotationsOnOperatorResources(t *testing.T) {
g := NewGomegaWithT(t)

result, err := helmInstall("--set", "globalAnnotations.one=annotation-one",
"--set", "globalAnnotations.two=annotation-two")

//result, err := helmInstall("--set", "globalLabels.one=label-one",
// "--set", "globalLabels.two=label-two",
// "--set", "globalAnnotations.three=annotation-three",
// "--set", "globalAnnotations.four=annotation-four")
//
//g.Expect(err).NotTo(HaveOccurred())
//g.Expect(result).NotTo(BeNil())
//
//dep := &appsv1.Deployment{}
//err = result.Get("coherence-operator", dep)
//g.Expect(err).NotTo(HaveOccurred())
//
//t.Logf("Asserting Helm install. Deploying Coherence resource")
//ns := helper.GetTestNamespace()
//deployment, err := helper.NewSingleCoherenceFromYaml(ns, "coherence.yaml")
//g.Expect(err).NotTo(HaveOccurred())
//
//defer deleteCoherence(t, &deployment)
//
//err = testContext.Client.Create(goctx.TODO(), &deployment)
//g.Expect(err).NotTo(HaveOccurred())
//
//var sts *appsv1.StatefulSet
//sts, err = helper.WaitForStatefulSetForDeployment(testContext, ns, &deployment, helper.RetryInterval, helper.Timeout)
//g.Expect(err).NotTo(HaveOccurred())
//
//g.Expect(sts.Labels).NotTo(BeNil())
//g.Expect(sts.Labels["one"]).To(Equal("label-one"))
//g.Expect(sts.Labels["two"]).To(Equal("label-two"))
//
//g.Expect(sts.Annotations).NotTo(BeNil())
//g.Expect(sts.Annotations["three"]).To(Equal("annotation-three"))
//g.Expect(sts.Annotations["four"]).To(Equal("annotation-four"))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(result).NotTo(BeNil())

dep := &appsv1.Deployment{}
err = result.Get("coherence-operator", dep)
g.Expect(err).NotTo(HaveOccurred())

annotations := dep.Annotations
actual, found := annotations["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-one"))
actual, found = annotations["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-two"))

annotations = dep.Spec.Template.Annotations
actual, found = annotations["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-one"))
actual, found = annotations["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-two"))

svc := &corev1.Service{}
err = result.Get("coherence-operator-webhook", svc)
g.Expect(err).NotTo(HaveOccurred())

annotations = svc.Annotations
actual, found = annotations["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-one"))
actual, found = annotations["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-two"))

svc = &corev1.Service{}
err = result.Get("coherence-operator-rest", svc)
g.Expect(err).NotTo(HaveOccurred())

annotations = svc.Annotations
actual, found = annotations["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-one"))
actual, found = annotations["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-two"))

sec := &corev1.Secret{}
err = result.Get("coherence-webhook-server-cert", sec)
g.Expect(err).NotTo(HaveOccurred())

annotations = sec.Annotations
actual, found = annotations["one"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-one"))
actual, found = annotations["two"]
g.Expect(found).To(BeTrue())
g.Expect(actual).To(Equal("annotation-two"))
}

func AssertLabelsAndAnnotations(t *testing.T, g *GomegaWithT, _ *coh.Coherence, sts *appsv1.StatefulSet) {
Expand Down
Loading