diff --git a/library/common-test/tests/service/metadata_test.yaml b/library/common-test/tests/service/metadata_test.yaml index ed5537dc..a77b7105 100644 --- a/library/common-test/tests/service/metadata_test.yaml +++ b/library/common-test/tests/service/metadata_test.yaml @@ -157,6 +157,99 @@ tests: metallb.universe.tf/allow-shared-ip: test-release-name-common-test traefik.ingress.kubernetes.io/service.serversscheme: https + - it: should add metallb shared ip annotation without externalTrafficPolicy + set: + global: + metallb: + addServiceAnnotations: true + service: + my-service1: + enabled: true + primary: true + type: LoadBalancer + ports: + port-name: + enabled: true + primary: true + port: 12345 + workload: + my-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *serviceDoc + isKind: + of: Service + - documentIndex: *serviceDoc + isSubset: + path: metadata.annotations + content: + metallb.universe.tf/allow-shared-ip: test-release-name-common-test + + - it: should add metallb shared ip annotation with externalTrafficPolicy set to Cluster + set: + global: + metallb: + addServiceAnnotations: true + service: + my-service1: + enabled: true + primary: true + type: LoadBalancer + externalTrafficPolicy: Cluster + ports: + port-name: + enabled: true + primary: true + port: 12345 + workload: + my-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *serviceDoc + isKind: + of: Service + - documentIndex: *serviceDoc + isSubset: + path: metadata.annotations + content: + metallb.universe.tf/allow-shared-ip: test-release-name-common-test + + - it: should NOT add metallb shared ip annotation with externalTrafficPolicy set to Local + set: + global: + metallb: + addServiceAnnotations: true + service: + my-service1: + enabled: true + primary: true + type: LoadBalancer + externalTrafficPolicy: Local + ports: + port-name: + enabled: true + primary: true + port: 12345 + workload: + my-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *serviceDoc + isKind: + of: Service + - documentIndex: *serviceDoc + notExists: + path: metadata.annotations.metallb\.universe\.tf/allow-shared-ip + - it: should pass with correct selector with targetSelector set: service: diff --git a/library/common/templates/lib/service/_additionalAnnotations.tpl b/library/common/templates/lib/service/_additionalAnnotations.tpl index 444f2818..19fec811 100644 --- a/library/common/templates/lib/service/_additionalAnnotations.tpl +++ b/library/common/templates/lib/service/_additionalAnnotations.tpl @@ -20,11 +20,8 @@ annotations: The annotations variable reference, to append the MetalLB annotatio {{- if (hasKey $rootCtx.Values.global "metallb") -}} {{- if $rootCtx.Values.global.metallb.addServiceAnnotations -}} - {{- if $objectData.externalTrafficPolicy -}} - {{- if ne $objectData.externalTrafficPolicy "Local" -}} - {{- $_ := set $annotations "metallb.universe.tf/allow-shared-ip" $sharedKey -}} - {{- end -}} - {{- else -}} + {{/* If externalTrafficPolicy is not set or is not Local, add the shared key as annotation */}} + {{- if ne $objectData.externalTrafficPolicy "Local" -}} {{- $_ := set $annotations "metallb.universe.tf/allow-shared-ip" $sharedKey -}} {{- end -}}