Skip to content

Commit df9aa3e

Browse files
committed
fix: Community-Operator - Fight with external Pod mutations
Signed-off-by: Mihai Galos <[email protected]>
1 parent 6b4107d commit df9aa3e

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

mongodb-community-operator/pkg/kube/podtemplatespec/podspec_template.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,20 @@ func WithInitContainer(name string, containerfunc func(*corev1.Container)) Modif
9393
}
9494
}
9595

96-
// WithInitContainerByIndex applies the modifications to the container with the provided index
97-
// if the index is out of range, a new container is added to accept these changes.
98-
func WithInitContainerByIndex(index int, funcs ...func(container *corev1.Container)) func(podTemplateSpec *corev1.PodTemplateSpec) {
96+
// WithPodLabels merges the provided labels with existing PodTemplateSpec labels.
97+
// This preserves labels added by external systems (like Kyverno policies) while
98+
// allowing the operator to add or override its own labels.
99+
func WithPodLabels(labels map[string]string) Modification {
100+
if labels == nil {
101+
labels = map[string]string{}
102+
}
99103
return func(podTemplateSpec *corev1.PodTemplateSpec) {
100-
if index >= len(podTemplateSpec.Spec.InitContainers) {
101-
podTemplateSpec.Spec.InitContainers = append(podTemplateSpec.Spec.InitContainers, corev1.Container{})
104+
if podTemplateSpec.ObjectMeta.Labels == nil {
105+
podTemplateSpec.ObjectMeta.Labels = map[string]string{}
102106
}
103-
c := &podTemplateSpec.Spec.InitContainers[index]
104-
for _, f := range funcs {
105-
f(c)
107+
108+
for k, v := range labels {
109+
podTemplateSpec.ObjectMeta.Labels[k] = v
106110
}
107111
}
108112
}

mongodb-community-operator/pkg/kube/podtemplatespec/podspec_template_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,63 @@ func getCustomContainer() corev1.Container {
616616
Image: "image-1",
617617
}
618618
}
619+
620+
func TestWithPodLabels_MergeWithExistingLabels(t *testing.T) {
621+
existingPodTemplate := &corev1.PodTemplateSpec{
622+
ObjectMeta: metav1.ObjectMeta{
623+
Labels: map[string]string{
624+
"foo.bar/customer": "acme-corp",
625+
"foo.bar/env": "prod",
626+
"external-label": "preserve-me",
627+
},
628+
},
629+
}
630+
631+
operatorLabels := map[string]string{
632+
"app": "database",
633+
"version": "6.0",
634+
}
635+
636+
modification := WithPodLabels(operatorLabels)
637+
modification(existingPodTemplate)
638+
639+
expectedLabels := map[string]string{
640+
"foo.bar/customer": "acme-corp", // Preserved from existing
641+
"foo.bar/env": "prod", // Preserved from existing
642+
"external-label": "preserve-me", // Preserved from existing
643+
"app": "database", // Added by operator
644+
"version": "6.0", // Added by operator
645+
}
646+
647+
assert.Equal(t, expectedLabels, existingPodTemplate.ObjectMeta.Labels)
648+
}
649+
650+
func TestWithPodLabels_OverrideExistingLabels(t *testing.T) {
651+
existingPodTemplate := &corev1.PodTemplateSpec{
652+
ObjectMeta: metav1.ObjectMeta{
653+
Labels: map[string]string{
654+
"app": "old-app",
655+
"version": "5.0",
656+
"external-label": "should-be-preserved",
657+
},
658+
},
659+
}
660+
661+
operatorLabels := map[string]string{
662+
"app": "database", // Should override existing
663+
"version": "6.0", // Should override existing
664+
"tier": "backend", // Should be added
665+
}
666+
667+
modification := WithPodLabels(operatorLabels)
668+
modification(existingPodTemplate)
669+
670+
expectedLabels := map[string]string{
671+
"external-label": "should-be-preserved", // Preserved
672+
"app": "database", // Overridden by operator
673+
"version": "6.0", // Overridden by operator
674+
"tier": "backend", // Added by operator
675+
}
676+
677+
assert.Equal(t, expectedLabels, existingPodTemplate.ObjectMeta.Labels)
678+
}

0 commit comments

Comments
 (0)