Skip to content

Commit

Permalink
Stop updating labels/annotations for plugins configMap
Browse files Browse the repository at this point in the history
- this is to avoid running `rabbitmq-plugins set` everytime
the CR annotations and labels are updated.
- CR annotations and labels are still applied to plugins
configmap at creation.
  • Loading branch information
ChunyiLyu committed Nov 9, 2020
1 parent b127e59 commit 06a5c83
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 27 deletions.
8 changes: 0 additions & 8 deletions controllers/rabbitmqcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,6 @@ var _ = Describe("RabbitmqClusterController", func() {
return roleBinding.Annotations
}, 3).Should(HaveKeyWithValue(annotationKey, annotationValue))
})

It("updates annotations for plugins config map", func() {
Eventually(func() map[string]string {
roleBinding, err := clientSet.CoreV1().ConfigMaps(cluster.Namespace).Get(ctx, cluster.ChildResourceName("plugins-conf"), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
return roleBinding.Annotations
}, 3).Should(HaveKeyWithValue(annotationKey, annotationValue))
})
})

It("service type is updated", func() {
Expand Down
13 changes: 4 additions & 9 deletions internal/resource/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ var _ = Describe("GenerateServerConfigMap", func() {
var configMap *corev1.ConfigMap

BeforeEach(func() {
instance = rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbit-example",
},
}
instance.Labels = map[string]string{
"app.kubernetes.io/foo": "bar",
"foo": "bar",
Expand Down Expand Up @@ -98,8 +93,8 @@ var _ = Describe("GenerateServerConfigMap", func() {
})

It("adds labels from the instance and default labels", func() {
Expect(len(configMap.Labels)).To(Equal(6))
Expect(configMap.Labels).To(SatisfyAll(
HaveLen(6),
HaveKeyWithValue("foo", "bar"),
HaveKeyWithValue("rabbitmq", "is-great"),
HaveKeyWithValue("foo/app.kubernetes.io", "edgecase"),
Expand All @@ -111,8 +106,8 @@ var _ = Describe("GenerateServerConfigMap", func() {
})

It("adds annotations from the instance", func() {
Expect(len(configMap.Annotations)).To(Equal(1))
Expect(configMap.Annotations).To(SatisfyAll(
HaveLen(1),
HaveKeyWithValue("my-annotation", "i-like-this"),
Not(HaveKey("kubernetes.io/name")),
Not(HaveKey("kubectl.kubernetes.io/name")),
Expand Down Expand Up @@ -342,8 +337,8 @@ ssl_options.verify = verify_peer
"new-label": "test",
}
Expect(configMapBuilder.Update(configMap)).To(Succeed())
Expect(len(configMap.Labels)).To(Equal(3))
Expect(configMap.Labels).To(SatisfyAll(
HaveLen(3),
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Expand All @@ -352,7 +347,7 @@ ssl_options.verify = verify_peer
})

// this is to ensure that pods are not restarted when instance annotations are updated
It("does not update labels on the config map", func() {
It("does not update annotations on the config map", func() {
instance.Annotations = map[string]string{
"new-annotation": "test",
}
Expand Down
8 changes: 4 additions & 4 deletions internal/resource/rabbitmq_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func (builder *RabbitmqResourceBuilder) RabbitmqPluginsConfigMap() *RabbitmqPlug
func (builder *RabbitmqPluginsConfigMapBuilder) Build() (runtime.Object, error) {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: builder.Instance.ChildResourceName(PluginsConfigName),
Namespace: builder.Instance.Namespace,
Name: builder.Instance.ChildResourceName(PluginsConfigName),
Namespace: builder.Instance.Namespace,
Labels: metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels),
Annotations: metadata.ReconcileAndFilterAnnotations(nil, builder.Instance.Annotations),
},
Data: map[string]string{
"enabled_plugins": desiredPluginsAsString([]rabbitmqv1beta1.Plugin{}),
Expand All @@ -47,8 +49,6 @@ func (builder *RabbitmqPluginsConfigMapBuilder) Build() (runtime.Object, error)

func (builder *RabbitmqPluginsConfigMapBuilder) Update(object runtime.Object) error {
configMap := object.(*corev1.ConfigMap)
configMap.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
configMap.Annotations = metadata.ReconcileAndFilterAnnotations(configMap.GetAnnotations(), builder.Instance.Annotations)

if configMap.Data == nil {
configMap.Data = make(map[string]string)
Expand Down
70 changes: 70 additions & 0 deletions internal/resource/rabbitmq_plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ var _ = Describe("RabbitMQPlugins", func() {
var configMap *corev1.ConfigMap

BeforeEach(func() {
instance.Labels = map[string]string{
"app.kubernetes.io/foo": "bar",
"foo": "bar",
"rabbitmq": "is-great",
"foo/app.kubernetes.io": "edgecase",
}
instance.Annotations = map[string]string{
"my-annotation": "i-like-this",
"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",
}

obj, err := configMapBuilder.Build()
configMap = obj.(*corev1.ConfigMap)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -112,6 +128,32 @@ var _ = Describe("RabbitMQPlugins", func() {
configMap = obj.(*corev1.ConfigMap)
Expect(configMap.Data).To(HaveKeyWithValue("enabled_plugins", expectedEnabledPlugins))
})

It("adds labels from the instance and default labels", func() {
Expect(configMap.Labels).To(SatisfyAll(
HaveLen(6),
HaveKeyWithValue("foo", "bar"),
HaveKeyWithValue("rabbitmq", "is-great"),
HaveKeyWithValue("foo/app.kubernetes.io", "edgecase"),
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Not(HaveKey("app.kubernetes.io/foo")),
))
})

It("adds annotations from the instance", func() {
Expect(configMap.Annotations).To(SatisfyAll(
HaveLen(1),
HaveKeyWithValue("my-annotation", "i-like-this"),
Not(HaveKey("kubernetes.io/name")),
Not(HaveKey("kubectl.kubernetes.io/name")),
Not(HaveKey("k8s.io/name")),
Not(HaveKey("kubernetes.io/other")),
Not(HaveKey("kubectl.kubernetes.io/other")),
Not(HaveKey("k8s.io/other")),
))
})
})

Context("Update", func() {
Expand Down Expand Up @@ -177,6 +219,34 @@ var _ = Describe("RabbitMQPlugins", func() {
})
})
})

// ensures that we are not unnecessarily running `rabbitmq-plugins set` when CR labels are updated
It("does not update labels on the config map", func() {
configMap.Labels = map[string]string{
"app.kubernetes.io/name": instance.Name,
"app.kubernetes.io/component": "rabbitmq",
"app.kubernetes.io/part-of": "rabbitmq",
}
instance.Labels = map[string]string{
"new-label": "test",
}
Expect(configMapBuilder.Update(configMap)).To(Succeed())
Expect(configMap.Labels).To(SatisfyAll(
HaveLen(3),
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Not(HaveKey("new-label")),
))
})
// ensures that we are not unnecessarily running `rabbitmq-plugins set` when CR annotations are updated
It("does not update annotations on the config map", func() {
instance.Annotations = map[string]string{
"new-annotation": "test",
}
Expect(configMapBuilder.Update(configMap)).To(Succeed())
Expect(configMap.Annotations).To(BeEmpty())
})
})
})
})
10 changes: 4 additions & 6 deletions internal/resource/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,8 @@ var _ = Describe("StatefulSet", func() {
stsBuilder := builder.StatefulSet()
Expect(stsBuilder.Update(statefulSet)).To(Succeed())

labels := statefulSet.Spec.Template.ObjectMeta.Labels
Expect(len(labels)).To(Equal(3))
Expect(labels).To(SatisfyAll(
Expect(statefulSet.Spec.Template.ObjectMeta.Labels).To(SatisfyAll(
HaveLen(3),
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Expand Down Expand Up @@ -528,9 +527,8 @@ var _ = Describe("StatefulSet", func() {
}

Expect(stsBuilder.Update(statefulSet)).To(Succeed())
annotations := statefulSet.Spec.Template.Annotations
Expect(len(annotations)).To(Equal(3))
Expect(annotations).To(SatisfyAll(
Expect(statefulSet.Spec.Template.Annotations).To(SatisfyAll(
HaveLen(3),
HaveKeyWithValue("prometheus.io/scrape", "true"),
HaveKeyWithValue("prometheus.io/port", "15692"),
HaveKeyWithValue("this-was-the-previous-pod-anno", "should-be-preserved"),
Expand Down

0 comments on commit 06a5c83

Please sign in to comment.