diff --git a/pkg/zk/generators.go b/pkg/zk/generators.go index 2a8ef2d79..e6d7b1761 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), }, @@ -190,6 +196,7 @@ func MakeConfigMap(z *v1beta1.ZookeeperCluster) *v1.ConfigMap { ObjectMeta: metav1.ObjectMeta{ Name: z.ConfigMapName(), Namespace: z.Namespace, + Labels: z.Spec.Labels, }, Data: map[string]string{ "zoo.cfg": makeZkConfigString(z.Spec), @@ -281,8 +288,10 @@ func makeService(name string, ports []v1.ServicePort, clusterIP bool, z *v1beta1 ObjectMeta: metav1.ObjectMeta{ 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{ @@ -307,6 +316,7 @@ func MakePodDisruptionBudget(z *v1beta1.ZookeeperCluster) *policyv1beta1.PodDisr ObjectMeta: metav1.ObjectMeta{ Name: z.GetName(), Namespace: z.Namespace, + Labels: z.Spec.Labels, }, Spec: policyv1beta1.PodDisruptionBudgetSpec{ MaxUnavailable: &pdbCount, @@ -328,3 +338,15 @@ func MakeServiceAccount(z *v1beta1.ZookeeperCluster) *v1.ServiceAccount { }, } } + +// 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 +} diff --git a/pkg/zk/generators_test.go b/pkg/zk/generators_test.go index a19f932e7..379b37f98 100644 --- a/pkg/zk/generators_test.go +++ b/pkg/zk/generators_test.go @@ -46,11 +46,22 @@ var _ = Describe("Generators Spec", func() { Name: "example", Namespace: "default", }, + Spec: v1beta1.ZookeeperClusterSpec{ + Labels: map[string]string{ + "exampleLabel": "exampleValue", + }, + }, } z.WithDefaults() cm = zk.MakeConfigMap(z) }) + It("should have custom labels set", func() { + Ω(cm.GetLabels()).To(HaveKeyWithValue( + "exampleLabel", + "exampleValue")) + }) + Context("zoo.cfg", func() { BeforeEach(func() { cfg = cm.Data["zoo.cfg"] @@ -152,6 +163,41 @@ var _ = Describe("Generators Spec", func() { }) }) + Context("#MakeStatefulSet", func() { + var sts *appsv1.StatefulSet + + Context("with defaults", func() { + + BeforeEach(func() { + z := &v1beta1.ZookeeperCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example", + Namespace: "default", + }, + Spec: v1beta1.ZookeeperClusterSpec{ + Labels: map[string]string{ + "exampleLabel": "exampleValue", + }, + }, + } + z.WithDefaults() + sts = zk.MakeStatefulSet(z) + }) + + It("should have custom labels set", func() { + Ω(sts.GetLabels()).To(HaveKeyWithValue( + "exampleLabel", + "exampleValue")) + }) + + It("should have custom labels set on pods", func() { + Ω(sts.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue( + "exampleLabel", + "exampleValue")) + }) + }) + }) + Context("#MakeStatefulSet with Ephemeral storage", func() { var sts *appsv1.StatefulSet @@ -213,6 +259,9 @@ var _ = Describe("Generators Spec", func() { }, Spec: v1beta1.ZookeeperClusterSpec{ DomainName: domainName, + Labels: map[string]string{ + "exampleLabel": "exampleValue", + }, }, } z.WithDefaults() @@ -237,6 +286,12 @@ var _ = Describe("Generators Spec", func() { mapLength := len(s.GetAnnotations()) Ω(mapLength).To(Equal(0)) }) + + It("should have custom labels set", func() { + Ω(s.GetLabels()).To(HaveKeyWithValue( + "exampleLabel", + "exampleValue")) + }) }) Context("#MakeHeadlessService", func() { @@ -252,6 +307,9 @@ var _ = Describe("Generators Spec", func() { }, Spec: v1beta1.ZookeeperClusterSpec{ DomainName: domainName, + Labels: map[string]string{ + "exampleLabel": "exampleValue", + }, }, } z.WithDefaults() @@ -295,6 +353,12 @@ var _ = Describe("Generators Spec", func() { "external-dns.alpha.kubernetes.io/hostname", "example-headless.zk.com.")) }) + + It("should have custom labels set", func() { + Ω(s.GetLabels()).To(HaveKeyWithValue( + "exampleLabel", + "exampleValue")) + }) }) Context("#MakeHeadlessService dnsname without dot", func() { var s *v1.Service @@ -336,6 +400,9 @@ var _ = Describe("Generators Spec", func() { }, Spec: v1beta1.ZookeeperClusterSpec{ DomainName: domainName, + Labels: map[string]string{ + "exampleLabel": "exampleValue", + }, }, } z.WithDefaults() @@ -347,9 +414,14 @@ var _ = Describe("Generators Spec", func() { Ω(pdb.GetObjectKind().GroupVersionKind().Kind).To(Equal("PodDisruptionBudget")) }) - It("should have slector is zookeeper cluster name", func() { + It("should have selector is zookeeper cluster name", func() { Ω(pdb.Spec.Selector.MatchLabels["app"]).To(BeEquivalentTo(zkClusterName)) }) + It("should have custom labels set", func() { + Ω(pdb.GetLabels()).To(HaveKeyWithValue( + "exampleLabel", + "exampleValue")) + }) }) })