From 5f382bde0631cddeeedba26bb3bec93baea144cd Mon Sep 17 00:00:00 2001 From: Adi Muraru Date: Tue, 13 Oct 2020 00:20:31 +0300 Subject: [PATCH] ISSUE-257 Propagate labels to ZookeeperCluster underlying resources Fixes: #257 --- pkg/zk/generators.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/pkg/zk/generators.go b/pkg/zk/generators.go index bda87031f..656020e99 100644 --- a/pkg/zk/generators.go +++ b/pkg/zk/generators.go @@ -54,8 +54,11 @@ func MakeStatefulSet(z *v1beta1.ZookeeperCluster) *appsv1.StatefulSet { } else { pvcs = append(pvcs, v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ - Name: zkDataVolume, - Labels: map[string]string{"app": z.GetName()}, + Name: zkDataVolume, + Labels: mergeLabels( + z.Spec.Labels, + map[string]string{"app": z.GetName()}, + ), }, Spec: persistence.PersistentVolumeClaimSpec, }) @@ -85,10 +88,13 @@ func MakeStatefulSet(z *v1beta1.ZookeeperCluster) *appsv1.StatefulSet { Template: v1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ GenerateName: z.GetName(), - Labels: map[string]string{ - "app": z.GetName(), - "kind": "ZookeeperMember", - }, + Labels: mergeLabels( + z.Spec.Labels, + map[string]string{ + "app": z.GetName(), + "kind": "ZookeeperMember", + }, + ), }, Spec: makeZkPodSpec(z, extraVolumes), }, @@ -282,7 +288,10 @@ func makeService(name string, ports []v1.ServicePort, clusterIP bool, z *v1beta1 Name: name, Namespace: z.Namespace, - Labels: map[string]string{"app": z.GetName(), "headless": strconv.FormatBool(!clusterIP)}, + Labels: mergeLabels( + z.Spec.Labels, + map[string]string{"app": z.GetName(), "headless": strconv.FormatBool(!clusterIP)}, + ), Annotations: annotationMap, }, Spec: v1.ServiceSpec{ @@ -318,3 +327,15 @@ func MakePodDisruptionBudget(z *v1beta1.ZookeeperCluster) *policyv1beta1.PodDisr }, } } + +// MergeLabels merges label maps +func mergeLabels(l ...map[string]string) map[string]string { + res := make(map[string]string) + + for _, v := range l { + for lKey, lValue := range v { + res[lKey] = lValue + } + } + return res +}