@@ -616,3 +616,63 @@ func getCustomContainer() corev1.Container {
616
616
Image : "image-1" ,
617
617
}
618
618
}
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