Skip to content

Commit

Permalink
Stop populate CR labels/annotions to pods
Browse files Browse the repository at this point in the history
- we don't want to trigger pod restarts for
CR labels/annotations updates
  • Loading branch information
ChunyiLyu committed Nov 9, 2020
1 parent 4a06999 commit 660aa43
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
23 changes: 12 additions & 11 deletions internal/resource/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,12 @@ func (builder *StatefulSetBuilder) Update(object runtime.Object) error {

//Annotations
sts.Annotations = metadata.ReconcileAndFilterAnnotations(sts.Annotations, builder.Instance.Annotations)
defaultPodAnnotations := map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "15692",
}
podAnnotations := metadata.ReconcileAnnotations(defaultPodAnnotations, metadata.ReconcileAndFilterAnnotations(sts.Spec.Template.Annotations, builder.Instance.Annotations))

//Labels
updatedLabels := metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
sts.Labels = updatedLabels
sts.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)

sts.Spec.Template = builder.podTemplateSpec(podAnnotations, updatedLabels)
// pod template
sts.Spec.Template = builder.podTemplateSpec(sts.Spec.Template.Annotations)

if !sts.Spec.Template.Spec.Containers[0].Resources.Limits.Memory().Equal(*sts.Spec.Template.Spec.Containers[0].Resources.Requests.Memory()) {
logger := ctrl.Log.WithName("statefulset").WithName("RabbitmqCluster")
Expand Down Expand Up @@ -261,7 +256,13 @@ func sortEnvVar(envVar []corev1.EnvVar) {
}
}

func (builder *StatefulSetBuilder) podTemplateSpec(annotations, labels map[string]string) corev1.PodTemplateSpec {
func (builder *StatefulSetBuilder) podTemplateSpec(previousPodAnnotations map[string]string) corev1.PodTemplateSpec {
// default pod annotations used for prometheus metrics
defaultPodAnnotations := map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "15692",
}

//Init Container resources
cpuRequest := k8sresource.MustParse(initContainerCPU)
memoryRequest := k8sresource.MustParse(initContainerMemory)
Expand Down Expand Up @@ -510,8 +511,8 @@ func (builder *StatefulSetBuilder) podTemplateSpec(annotations, labels map[strin

return corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: annotations,
Labels: labels,
Annotations: metadata.ReconcileAnnotations(defaultPodAnnotations, previousPodAnnotations),
Labels: metadata.Label(builder.Instance.Name),
},
Spec: corev1.PodSpec{
TopologySpreadConstraints: []corev1.TopologySpreadConstraint{
Expand Down
62 changes: 28 additions & 34 deletions internal/resource/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,21 @@ var _ = Describe("StatefulSet", func() {
testLabels(statefulSet.Labels)
})

It("has the labels from the instance on the pod", func() {
It("adds default labels to pods but does not populate labels from the instance onto pods", func() {
stsBuilder := builder.StatefulSet()
Expect(stsBuilder.Update(statefulSet)).To(Succeed())
podTemplate := statefulSet.Spec.Template
testLabels(podTemplate.Labels)

labels := statefulSet.Spec.Template.ObjectMeta.Labels
Expect(len(labels)).To(Equal(3))
Expect(labels).To(SatisfyAll(
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Not(HaveKey("foo")),
Not(HaveKey("rabbitmq")),
Not(HaveKey("foo/app.kubernetes.io")),
Not(HaveKey("app.kubernetes.io/foo")),
))
})

It("adds the correct labels on the statefulset", func() {
Expand Down Expand Up @@ -438,16 +448,6 @@ var _ = Describe("StatefulSet", func() {
Expect(stsBuilder.Update(statefulSet)).To(Succeed())
Expect(statefulSet.Labels).NotTo(HaveKey("this-was-the-previous-label"))
})

It("adds the correct labels on the rabbitmq pods", func() {
stsBuilder := builder.StatefulSet()
Expect(stsBuilder.Update(statefulSet)).To(Succeed())

labels := statefulSet.Spec.Template.ObjectMeta.Labels
Expect(labels["app.kubernetes.io/name"]).To(Equal(instance.Name))
Expect(labels["app.kubernetes.io/component"]).To(Equal("rabbitmq"))
Expect(labels["app.kubernetes.io/part-of"]).To(Equal("rabbitmq"))
})
})

Context("annotations", func() {
Expand Down Expand Up @@ -483,8 +483,6 @@ var _ = Describe("StatefulSet", func() {
"prometheus.io/scrape": "true",
"prometheus.io/port": "15692",
"this-was-the-previous-pod-anno": "should-be-preserved",
"app.kubernetes.io/part-of": "rabbitmq-pod",
"app.k8s.io/something": "something-amazing-on-pod",
}

existingPvcTemplateAnnotations = map[string]string{
Expand All @@ -501,7 +499,7 @@ var _ = Describe("StatefulSet", func() {
statefulSet.Spec.VolumeClaimTemplates[0].Annotations = existingPvcTemplateAnnotations
})

It("updates annotations", func() {
It("updates sts annotations", func() {
stsBuilder.Instance.Annotations = map[string]string{
"kubernetes.io/name": "i-do-not-like-this",
"kubectl.kubernetes.io/name": "i-do-not-like-this",
Expand All @@ -523,28 +521,24 @@ var _ = Describe("StatefulSet", func() {
Expect(statefulSet.Annotations).To(Equal(expectedAnnotations))
})

It("update annotations from the instance to the pod", func() {
It("adds default annotations but does not populate annotations from the instance to the pod", func() {
stsBuilder.Instance.Annotations = map[string]string{
"kubernetes.io/name": "i-do-not-like-this",
"kubectl.kubernetes.io/name": "i-do-not-like-this",
"k8s.io/name": "i-do-not-like-this",
"kubernetes.io/other": "i-do-not-like-this",
"kubectl.kubernetes.io/other": "i-do-not-like-this",
"k8s.io/other": "i-do-not-like-this",
"my-annotation": "i-like-this",
"my-annotation": "my-annotation",
"k8s.io/other": "i-do-not-like-this",
}

Expect(stsBuilder.Update(statefulSet)).To(Succeed())
expectedAnnotations := map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": "15692",
"my-annotation": "i-like-this",
"app.kubernetes.io/part-of": "rabbitmq-pod",
"this-was-the-previous-pod-anno": "should-be-preserved",
"app.k8s.io/something": "something-amazing-on-pod",
}

Expect(statefulSet.Spec.Template.Annotations).To(Equal(expectedAnnotations))
annotations := statefulSet.Spec.Template.Annotations
Expect(len(annotations)).To(Equal(3))
Expect(annotations).To(SatisfyAll(
HaveKeyWithValue("prometheus.io/scrape", "true"),
HaveKeyWithValue("prometheus.io/port", "15692"),
HaveKeyWithValue("this-was-the-previous-pod-anno", "should-be-preserved"),
Not(HaveKey("app.kubernetes.io/part-of")),
Not(HaveKey("app.k8s.io/something")),
Not(HaveKey("my-annotation")),
Not(HaveKey("k8s.io/other")),
))
})

It("does not update annotations from the instance to the pvc template", func() {
Expand Down

0 comments on commit 660aa43

Please sign in to comment.