Skip to content

Commit 8408607

Browse files
committed
When specifying global labels or annotations values using the Helm chart, these labels and annotations will be applied to all the Operator resources installed by the chart
1 parent 0b8d884 commit 8408607

File tree

4 files changed

+239
-35
lines changed

4 files changed

+239
-35
lines changed

docs/other/041_global_labels.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,10 @@ helm install \
183183
coherence/coherence-operator
184184
----
185185
186+
[NOTE]
187+
====
188+
When setting the Helm chart values `globalLabels` or `globalAnnotations` any labels and annotations specified will also
189+
be applied to all the resources installed by the Helm Chart too.
190+
====
186191
187192

helm-charts/coherence-operator/templates/deployment.yaml

+40-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ kind: Secret
55
metadata:
66
name: {{ default "coherence-webhook-server-cert" .Values.webhookCertSecret }}
77
namespace: {{ .Release.Namespace }}
8+
{{- if (.Values.globalLabels) }}
9+
labels:
10+
{{ toYaml .Values.globalLabels | indent 4 }}
11+
{{- end }}
12+
{{- if (.Values.globalAnnotations) }}
13+
annotations:
14+
{{ toYaml .Values.globalAnnotations | indent 4 }}
15+
{{- end }}
816
{{- end }}
917
---
1018
apiVersion: v1
@@ -20,6 +28,13 @@ metadata:
2028
app.kubernetes.io/component: webhook
2129
app.kubernetes.io/part-of: coherence-operator
2230
app.kubernetes.io/managed-by: helm
31+
{{- if (.Values.globalLabels) }}
32+
{{ toYaml .Values.globalLabels | indent 4 }}
33+
{{- end }}
34+
{{- if (.Values.globalAnnotations) }}
35+
annotations:
36+
{{ toYaml .Values.globalAnnotations | indent 4 }}
37+
{{- end }}
2338
spec:
2439
ports:
2540
- name: webhook
@@ -44,6 +59,13 @@ metadata:
4459
app.kubernetes.io/component: rest
4560
app.kubernetes.io/part-of: coherence-operator
4661
app.kubernetes.io/managed-by: helm
62+
{{- if (.Values.globalLabels) }}
63+
{{ toYaml .Values.globalLabels | indent 4 }}
64+
{{- end }}
65+
{{- if (.Values.globalAnnotations) }}
66+
annotations:
67+
{{ toYaml .Values.globalAnnotations | indent 4 }}
68+
{{- end }}
4769
spec:
4870
ports:
4971
- name: http-rest
@@ -69,11 +91,19 @@ metadata:
6991
app.kubernetes.io/part-of: coherence-operator
7092
app.kubernetes.io/managed-by: helm
7193
app.kubernetes.io/created-by: controller-manager
94+
{{- if (.Values.globalLabels) }}
95+
{{ toYaml .Values.globalLabels | indent 4 }}
96+
{{- end }}
7297
{{- if .Values.deploymentLabels }}
7398
{{ toYaml .Values.deploymentLabels | indent 4 }}
7499
{{- end }}
75-
{{- if .Values.deploymentAnnotations }}
100+
{{- if or (.Values.deploymentAnnotations) (.Values.globalAnnotations) }}
76101
annotations:
102+
{{- end }}
103+
{{- if (.Values.globalAnnotations) }}
104+
{{ toYaml .Values.globalAnnotations | indent 4 }}
105+
{{- end }}
106+
{{- if .Values.deploymentAnnotations }}
77107
{{ toYaml .Values.deploymentAnnotations | indent 4 }}
78108
{{- end }}
79109
spec:
@@ -92,11 +122,19 @@ spec:
92122
app.kubernetes.io/part-of: coherence-operator
93123
app.kubernetes.io/managed-by: helm
94124
app.kubernetes.io/created-by: controller-manager
125+
{{- if (.Values.globalLabels) }}
126+
{{ toYaml .Values.globalLabels | indent 8 }}
127+
{{- end }}
95128
{{- if .Values.labels }}
96129
{{ toYaml .Values.labels | indent 8 }}
97130
{{- end }}
98-
{{- if .Values.annotations }}
131+
{{- if or (.Values.annotations) (.Values.globalAnnotations) }}
99132
annotations:
133+
{{- end }}
134+
{{- if (.Values.globalAnnotations) }}
135+
{{ toYaml .Values.globalAnnotations | indent 8 }}
136+
{{- end }}
137+
{{- if .Values.annotations }}
100138
{{ toYaml .Values.annotations | indent 8 }}
101139
{{- end }}
102140
spec:

helm-charts/coherence-operator/templates/rbac.yaml

+63
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ metadata:
88
namespace: {{ .Release.Namespace }}
99
labels:
1010
control-plane: coherence
11+
{{- if (.Values.globalLabels) }}
12+
{{ toYaml .Values.globalLabels | indent 4 }}
13+
{{- end }}
14+
{{- if (.Values.globalAnnotations) }}
15+
annotations:
16+
{{ toYaml .Values.globalAnnotations | indent 4 }}
17+
{{- end }}
1118
---
1219
{{- if .Values.clusterRoles }}
1320
# -------------------------------------------------------------
@@ -21,6 +28,13 @@ metadata:
2128
namespace: {{ .Release.Namespace }}
2229
labels:
2330
control-plane: coherence
31+
{{- if (.Values.globalLabels) }}
32+
{{ toYaml .Values.globalLabels | indent 4 }}
33+
{{- end }}
34+
{{- if (.Values.globalAnnotations) }}
35+
annotations:
36+
{{ toYaml .Values.globalAnnotations | indent 4 }}
37+
{{- end }}
2438
rules:
2539
- apiGroups:
2640
- apiextensions.k8s.io
@@ -54,6 +68,13 @@ metadata:
5468
namespace: {{ .Release.Namespace }}
5569
labels:
5670
control-plane: coherence
71+
{{- if (.Values.globalLabels) }}
72+
{{ toYaml .Values.globalLabels | indent 4 }}
73+
{{- end }}
74+
{{- if (.Values.globalAnnotations) }}
75+
annotations:
76+
{{ toYaml .Values.globalAnnotations | indent 4 }}
77+
{{- end }}
5778
roleRef:
5879
apiGroup: rbac.authorization.k8s.io
5980
kind: ClusterRole
@@ -78,6 +99,13 @@ metadata:
7899
namespace: {{ .Release.Namespace }}
79100
labels:
80101
control-plane: coherence
102+
{{- if (.Values.globalLabels) }}
103+
{{ toYaml .Values.globalLabels | indent 4 }}
104+
{{- end }}
105+
{{- if (.Values.globalAnnotations) }}
106+
annotations:
107+
{{ toYaml .Values.globalAnnotations | indent 4 }}
108+
{{- end }}
81109
rules:
82110
- apiGroups:
83111
- ""
@@ -99,6 +127,13 @@ metadata:
99127
namespace: {{ .Release.Namespace }}
100128
labels:
101129
control-plane: coherence
130+
{{- if (.Values.globalLabels) }}
131+
{{ toYaml .Values.globalLabels | indent 4 }}
132+
{{- end }}
133+
{{- if (.Values.globalAnnotations) }}
134+
annotations:
135+
{{ toYaml .Values.globalAnnotations | indent 4 }}
136+
{{- end }}
102137
roleRef:
103138
apiGroup: rbac.authorization.k8s.io
104139
kind: ClusterRole
@@ -124,6 +159,13 @@ metadata:
124159
namespace: {{ .Release.Namespace }}
125160
labels:
126161
control-plane: coherence
162+
{{- if (.Values.globalLabels) }}
163+
{{ toYaml .Values.globalLabels | indent 4 }}
164+
{{- end }}
165+
{{- if (.Values.globalAnnotations) }}
166+
annotations:
167+
{{ toYaml .Values.globalAnnotations | indent 4 }}
168+
{{- end }}
127169
rules:
128170
- apiGroups:
129171
- ""
@@ -225,6 +267,13 @@ metadata:
225267
namespace: {{ .Release.Namespace }}
226268
labels:
227269
control-plane: coherence
270+
{{- if (.Values.globalLabels) }}
271+
{{ toYaml .Values.globalLabels | indent 4 }}
272+
{{- end }}
273+
{{- if (.Values.globalAnnotations) }}
274+
annotations:
275+
{{ toYaml .Values.globalAnnotations | indent 4 }}
276+
{{- end }}
228277
roleRef:
229278
apiGroup: rbac.authorization.k8s.io
230279
{{- if .Values.clusterRoles }}
@@ -248,6 +297,13 @@ metadata:
248297
name: leader-election-role
249298
labels:
250299
control-plane: coherence
300+
{{- if (.Values.globalLabels) }}
301+
{{ toYaml .Values.globalLabels | indent 4 }}
302+
{{- end }}
303+
{{- if (.Values.globalAnnotations) }}
304+
annotations:
305+
{{ toYaml .Values.globalAnnotations | indent 4 }}
306+
{{- end }}
251307
rules:
252308
- apiGroups:
253309
- ""
@@ -291,6 +347,13 @@ metadata:
291347
name: leader-election-rolebinding
292348
labels:
293349
control-plane: coherence
350+
{{- if (.Values.globalLabels) }}
351+
{{ toYaml .Values.globalLabels | indent 4 }}
352+
{{- end }}
353+
{{- if (.Values.globalAnnotations) }}
354+
annotations:
355+
{{ toYaml .Values.globalAnnotations | indent 4 }}
356+
{{- end }}
294357
roleRef:
295358
apiGroup: rbac.authorization.k8s.io
296359
kind: Role

test/e2e/helm/helm_test.go

+131-33
Original file line numberDiff line numberDiff line change
@@ -583,40 +583,138 @@ func TestGlobalLabelsAndAnnotations(t *testing.T) {
583583
"--set", "globalAnnotations.four=annotation-four")
584584
g.Expect(err).NotTo(HaveOccurred())
585585
AssertHelmInstallWithStatefulSetSubTest(t, "basic", cmd, g, AssertLabelsAndAnnotations)
586+
}
587+
588+
func TestGlobalLabelsOnOperatorResources(t *testing.T) {
589+
g := NewGomegaWithT(t)
590+
591+
result, err := helmInstall("--set", "globalLabels.one=label-one",
592+
"--set", "globalLabels.two=label-two")
593+
594+
g.Expect(err).NotTo(HaveOccurred())
595+
g.Expect(result).NotTo(BeNil())
596+
597+
dep := &appsv1.Deployment{}
598+
err = result.Get("coherence-operator", dep)
599+
g.Expect(err).NotTo(HaveOccurred())
600+
601+
labels := dep.Labels
602+
actual, found := labels["one"]
603+
g.Expect(found).To(BeTrue())
604+
g.Expect(actual).To(Equal("label-one"))
605+
actual, found = labels["two"]
606+
g.Expect(found).To(BeTrue())
607+
g.Expect(actual).To(Equal("label-two"))
608+
609+
labels = dep.Spec.Template.Labels
610+
actual, found = labels["one"]
611+
g.Expect(found).To(BeTrue())
612+
g.Expect(actual).To(Equal("label-one"))
613+
actual, found = labels["two"]
614+
g.Expect(found).To(BeTrue())
615+
g.Expect(actual).To(Equal("label-two"))
616+
617+
svc := &corev1.Service{}
618+
err = result.Get("coherence-operator-webhook", svc)
619+
g.Expect(err).NotTo(HaveOccurred())
620+
621+
labels = svc.Labels
622+
actual, found = labels["one"]
623+
g.Expect(found).To(BeTrue())
624+
g.Expect(actual).To(Equal("label-one"))
625+
actual, found = labels["two"]
626+
g.Expect(found).To(BeTrue())
627+
g.Expect(actual).To(Equal("label-two"))
628+
629+
svc = &corev1.Service{}
630+
err = result.Get("coherence-operator-rest", svc)
631+
g.Expect(err).NotTo(HaveOccurred())
632+
633+
labels = svc.Labels
634+
actual, found = labels["one"]
635+
g.Expect(found).To(BeTrue())
636+
g.Expect(actual).To(Equal("label-one"))
637+
actual, found = labels["two"]
638+
g.Expect(found).To(BeTrue())
639+
g.Expect(actual).To(Equal("label-two"))
640+
641+
sec := &corev1.Secret{}
642+
err = result.Get("coherence-webhook-server-cert", sec)
643+
g.Expect(err).NotTo(HaveOccurred())
644+
645+
labels = sec.Labels
646+
actual, found = labels["one"]
647+
g.Expect(found).To(BeTrue())
648+
g.Expect(actual).To(Equal("label-one"))
649+
actual, found = labels["two"]
650+
g.Expect(found).To(BeTrue())
651+
g.Expect(actual).To(Equal("label-two"))
652+
}
653+
654+
func TestGlobalAnnotationsOnOperatorResources(t *testing.T) {
655+
g := NewGomegaWithT(t)
656+
657+
result, err := helmInstall("--set", "globalAnnotations.one=annotation-one",
658+
"--set", "globalAnnotations.two=annotation-two")
586659

587-
//result, err := helmInstall("--set", "globalLabels.one=label-one",
588-
// "--set", "globalLabels.two=label-two",
589-
// "--set", "globalAnnotations.three=annotation-three",
590-
// "--set", "globalAnnotations.four=annotation-four")
591-
//
592-
//g.Expect(err).NotTo(HaveOccurred())
593-
//g.Expect(result).NotTo(BeNil())
594-
//
595-
//dep := &appsv1.Deployment{}
596-
//err = result.Get("coherence-operator", dep)
597-
//g.Expect(err).NotTo(HaveOccurred())
598-
//
599-
//t.Logf("Asserting Helm install. Deploying Coherence resource")
600-
//ns := helper.GetTestNamespace()
601-
//deployment, err := helper.NewSingleCoherenceFromYaml(ns, "coherence.yaml")
602-
//g.Expect(err).NotTo(HaveOccurred())
603-
//
604-
//defer deleteCoherence(t, &deployment)
605-
//
606-
//err = testContext.Client.Create(goctx.TODO(), &deployment)
607-
//g.Expect(err).NotTo(HaveOccurred())
608-
//
609-
//var sts *appsv1.StatefulSet
610-
//sts, err = helper.WaitForStatefulSetForDeployment(testContext, ns, &deployment, helper.RetryInterval, helper.Timeout)
611-
//g.Expect(err).NotTo(HaveOccurred())
612-
//
613-
//g.Expect(sts.Labels).NotTo(BeNil())
614-
//g.Expect(sts.Labels["one"]).To(Equal("label-one"))
615-
//g.Expect(sts.Labels["two"]).To(Equal("label-two"))
616-
//
617-
//g.Expect(sts.Annotations).NotTo(BeNil())
618-
//g.Expect(sts.Annotations["three"]).To(Equal("annotation-three"))
619-
//g.Expect(sts.Annotations["four"]).To(Equal("annotation-four"))
660+
g.Expect(err).NotTo(HaveOccurred())
661+
g.Expect(result).NotTo(BeNil())
662+
663+
dep := &appsv1.Deployment{}
664+
err = result.Get("coherence-operator", dep)
665+
g.Expect(err).NotTo(HaveOccurred())
666+
667+
annotations := dep.Annotations
668+
actual, found := annotations["one"]
669+
g.Expect(found).To(BeTrue())
670+
g.Expect(actual).To(Equal("annotation-one"))
671+
actual, found = annotations["two"]
672+
g.Expect(found).To(BeTrue())
673+
g.Expect(actual).To(Equal("annotation-two"))
674+
675+
annotations = dep.Spec.Template.Annotations
676+
actual, found = annotations["one"]
677+
g.Expect(found).To(BeTrue())
678+
g.Expect(actual).To(Equal("annotation-one"))
679+
actual, found = annotations["two"]
680+
g.Expect(found).To(BeTrue())
681+
g.Expect(actual).To(Equal("annotation-two"))
682+
683+
svc := &corev1.Service{}
684+
err = result.Get("coherence-operator-webhook", svc)
685+
g.Expect(err).NotTo(HaveOccurred())
686+
687+
annotations = svc.Annotations
688+
actual, found = annotations["one"]
689+
g.Expect(found).To(BeTrue())
690+
g.Expect(actual).To(Equal("annotation-one"))
691+
actual, found = annotations["two"]
692+
g.Expect(found).To(BeTrue())
693+
g.Expect(actual).To(Equal("annotation-two"))
694+
695+
svc = &corev1.Service{}
696+
err = result.Get("coherence-operator-rest", svc)
697+
g.Expect(err).NotTo(HaveOccurred())
698+
699+
annotations = svc.Annotations
700+
actual, found = annotations["one"]
701+
g.Expect(found).To(BeTrue())
702+
g.Expect(actual).To(Equal("annotation-one"))
703+
actual, found = annotations["two"]
704+
g.Expect(found).To(BeTrue())
705+
g.Expect(actual).To(Equal("annotation-two"))
706+
707+
sec := &corev1.Secret{}
708+
err = result.Get("coherence-webhook-server-cert", sec)
709+
g.Expect(err).NotTo(HaveOccurred())
710+
711+
annotations = sec.Annotations
712+
actual, found = annotations["one"]
713+
g.Expect(found).To(BeTrue())
714+
g.Expect(actual).To(Equal("annotation-one"))
715+
actual, found = annotations["two"]
716+
g.Expect(found).To(BeTrue())
717+
g.Expect(actual).To(Equal("annotation-two"))
620718
}
621719

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

0 commit comments

Comments
 (0)