From baefb372890dfc6ae9505aaaaae691ea020414a0 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Mon, 6 Feb 2023 17:04:31 +0200 Subject: [PATCH] externalname can have no ports --- .../tests/service/external_name_test.yaml | 27 +++++++++++++ .../1.0.0/templates/classes/_service.tpl | 6 ++- .../templates/lib/service/_validation.tpl | 38 ++++++++++--------- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/library/common-test/tests/service/external_name_test.yaml b/library/common-test/tests/service/external_name_test.yaml index 6508650b8c..69cbab69e2 100644 --- a/library/common-test/tests/service/external_name_test.yaml +++ b/library/common-test/tests/service/external_name_test.yaml @@ -39,6 +39,33 @@ tests: protocol: TCP targetPort: 12345 + - it: should pass with type externalName, without ports + set: + service: + my-service: + enabled: true + primary: true + type: ExternalName + externalName: my-external-name + asserts: + - documentIndex: *serviceDoc + isKind: + of: Service + - documentIndex: *serviceDoc + isAPIVersion: + of: v1 + - documentIndex: *serviceDoc + equal: + path: metadata.name + value: release-name-common-test + - documentIndex: *serviceDoc + equal: + path: spec + value: + type: ExternalName + externalName: my-external-name + publishNotReadyAddresses: false + - it: should pass with type ExternalName and available options set set: some_ip: 172.16.20.35 diff --git a/library/common/1.0.0/templates/classes/_service.tpl b/library/common/1.0.0/templates/classes/_service.tpl index 024810feee..110d7cb938 100644 --- a/library/common/1.0.0/templates/classes/_service.tpl +++ b/library/common/1.0.0/templates/classes/_service.tpl @@ -83,9 +83,11 @@ spec: {{- include "ix.v1.common.lib.service.spec.externalName" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}} {{- else if eq $svcType "ExternalIP" -}} {{- include "ix.v1.common.lib.service.spec.externalIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}} - {{- end }} + {{- end -}} + {{- with (include "ix.v1.common.lib.service.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} ports: - {{- include "ix.v1.common.lib.service.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 4 }} + {{- . | nindent 4 }} + {{- end -}} {{- if not (mustHas $svcType $specialTypes) }} selector: {{- include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "podName" $podValues.shortName) | trim | nindent 4 -}} diff --git a/library/common/1.0.0/templates/lib/service/_validation.tpl b/library/common/1.0.0/templates/lib/service/_validation.tpl index 160c3f671f..ba41924242 100644 --- a/library/common/1.0.0/templates/lib/service/_validation.tpl +++ b/library/common/1.0.0/templates/lib/service/_validation.tpl @@ -28,28 +28,30 @@ objectData: {{- end -}} {{- $hasEnabledPort := false -}} - {{- range $name, $port := $objectData.ports -}} - {{- if $port.enabled -}} - {{- $hasEnabledPort = true -}} + {{- if ne $objectData.type "ExternalName" -}} + {{- range $name, $port := $objectData.ports -}} + {{- if $port.enabled -}} + {{- $hasEnabledPort = true -}} + + {{- if and $port.targetSelector (not (kindIs "string" $port.targetSelector)) -}} + {{- fail (printf "Service - Expected to be [string], but got [%s]" (kindOf $port.targetSelector)) -}} + {{- end -}} + + {{- if not $port.port -}} + {{- fail (printf "Service - Expected non-empty ") -}} + {{- end -}} + + {{- $protocolTypes := (list "TCP" "UDP" "HTTP" "HTTPS") -}} + {{- if and $port.protocol (not (mustHas (tpl $port.protocol $rootCtx) $protocolTypes)) -}} + {{- fail (printf "Service - Expected to be one of [%s] but got [%s]" (join ", " $protocolTypes) $port.protocol) -}} + {{- end -}} - {{- if and $port.targetSelector (not (kindIs "string" $port.targetSelector)) -}} - {{- fail (printf "Service - Expected to be [string], but got [%s]" (kindOf $port.targetSelector)) -}} {{- end -}} - - {{- if not $port.port -}} - {{- fail (printf "Service - Expected non-empty ") -}} - {{- end -}} - - {{- $protocolTypes := (list "TCP" "UDP" "HTTP" "HTTPS") -}} - {{- if and $port.protocol (not (mustHas (tpl $port.protocol $rootCtx) $protocolTypes)) -}} - {{- fail (printf "Service - Expected to be one of [%s] but got [%s]" (join ", " $protocolTypes) $port.protocol) -}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- if not $hasEnabledPort -}} - {{- fail "Service - Expected enabled service to have at least one port" -}} + {{- if not $hasEnabledPort -}} + {{- fail "Service - Expected enabled service to have at least one port" -}} + {{- end -}} {{- end -}} {{- end -}}