Skip to content

Commit

Permalink
ISSUE-257 Propagate labels to ZookeeperCluster underlying resources
Browse files Browse the repository at this point in the history
  • Loading branch information
amuraru committed Oct 13, 2020
1 parent 0c54143 commit 4dbe889
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
38 changes: 30 additions & 8 deletions pkg/zk/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -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),
},
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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{
Expand All @@ -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,
Expand All @@ -318,3 +328,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
}
74 changes: 73 additions & 1 deletion pkg/zk/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -190,6 +236,9 @@ var _ = Describe("Generators Spec", func() {
},
Spec: v1beta1.ZookeeperClusterSpec{
DomainName: domainName,
Labels: map[string]string{
"exampleLabel": "exampleValue",
},
},
}
z.WithDefaults()
Expand All @@ -214,6 +263,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() {
Expand All @@ -229,6 +284,9 @@ var _ = Describe("Generators Spec", func() {
},
Spec: v1beta1.ZookeeperClusterSpec{
DomainName: domainName,
Labels: map[string]string{
"exampleLabel": "exampleValue",
},
},
}
z.WithDefaults()
Expand Down Expand Up @@ -272,6 +330,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
Expand Down Expand Up @@ -313,6 +377,9 @@ var _ = Describe("Generators Spec", func() {
},
Spec: v1beta1.ZookeeperClusterSpec{
DomainName: domainName,
Labels: map[string]string{
"exampleLabel": "exampleValue",
},
},
}
z.WithDefaults()
Expand All @@ -324,9 +391,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"))
})
})
})

0 comments on commit 4dbe889

Please sign in to comment.