diff --git a/library/common-test/tests/service/external_ip_test.yaml b/library/common-test/tests/service/external_ip_test.yaml index cba5b9d8..549a126c 100644 --- a/library/common-test/tests/service/external_ip_test.yaml +++ b/library/common-test/tests/service/external_ip_test.yaml @@ -4,7 +4,7 @@ templates: chart: appVersion: &appVer v9.9.9 tests: - - it: should pass with type externalIP + - it: should pass with type externalIP and useSlice unset (default) set: service: my-service: @@ -144,13 +144,14 @@ tests: - addresses: - 1.1.1.1 - - it: should pass with type ExternalIP and https + - it: should pass with type ExternalIP and https and useSlice explicitly set to true set: service: my-service: enabled: true primary: true type: ExternalIP + useSlice: true externalIP: 1.1.1.1 ports: port-name: @@ -196,3 +197,124 @@ tests: path: metadata.annotations content: traefik.ingress.kubernetes.io/service.serversscheme: https + + - it: should pass with type ExternalIP and useSlice set to false + set: + service: + my-service: + enabled: true + primary: true + type: ExternalIP + useSlice: false + externalIP: 1.1.1.1 + ports: + port-name: + enabled: true + primary: true + port: 443 + protocol: https + asserts: + - documentIndex: *serviceDoc + equal: + path: spec + value: + publishNotReadyAddresses: false + ports: + - name: port-name + port: 443 + protocol: TCP + targetPort: 443 + - documentIndex: &endpointDoc 1 + isKind: + of: Endpoint + - documentIndex: *endpointDoc + isAPIVersion: + of: v1 + - documentIndex: *endpointDoc + equal: + path: subsets + value: + - addresses: + - ip: 1.1.1.1 + ports: + - name: port-name + port: 443 + protocol: TCP + - documentIndex: &serviceDoc 0 + isKind: + of: Service + - documentIndex: *serviceDoc + isSubset: + path: metadata.annotations + content: + traefik.ingress.kubernetes.io/service.serversscheme: https + + - it: should pass with type ExternalIP and useSlice set to false, multiple ports + set: + service: + my-service: + enabled: true + primary: true + type: ExternalIP + useSlice: false + externalIP: 1.1.1.1 + ports: + port-name: + enabled: true + primary: true + port: 12345 + port-name2: + enabled: true + port: 12344 + targetPort: 12346 + protocol: http + appProtocol: http + asserts: + - documentIndex: *serviceDoc + equal: + path: spec + value: + publishNotReadyAddresses: false + ports: + - name: port-name + port: 12345 + protocol: TCP + targetPort: 12345 + - name: port-name2 + port: 12344 + protocol: TCP + targetPort: 12346 + - documentIndex: &endpointDoc 1 + isKind: + of: Endpoint + - documentIndex: *endpointDoc + isAPIVersion: + of: v1 + - documentIndex: *endpointDoc + equal: + path: metadata.name + value: release-name-common-test + - documentIndex: *endpointDoc + equal: + path: metadata.labels + value: + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/version: *appVer + helm.sh/chart: common-test-1.0.0 + helm-revision: "0" + app: common-test-1.0.0 + release: RELEASE-NAME + - documentIndex: *endpointDoc + equal: + path: subsets + value: + - addresses: + - ip: 1.1.1.1 + ports: + - name: port-name + port: 12345 + protocol: TCP + - name: port-name2 + port: 12346 + protocol: TCP + appProtocol: http diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 94010122..8b48d081 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 12.4.24 +version: 12.5.0 diff --git a/library/common/templates/class/_endpoint.tpl b/library/common/templates/class/_endpoint.tpl new file mode 100644 index 00000000..bc1f1044 --- /dev/null +++ b/library/common/templates/class/_endpoint.tpl @@ -0,0 +1,32 @@ +{{/* Endpoint Class */}} +{{/* Call this template: +{{ include "tc.v1.common.class.endpoint" (dict "rootCtx" $ "objectData" $objectData) }} + +rootCtx: The root context of the chart. +objectData: The service data, that will be used to render the Service object. +*/}} + +{{- define "tc.v1.common.class.endpoint" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} +--- +apiVersion: v1 +kind: Endpoint +metadata: + name: {{ $objectData.name }} + {{- $labels := (mustMerge ($objectData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 4 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 4 }} + {{- end }} +subsets: + - addresses: + {{- include "tc.v1.common.lib.endpoint.addresses" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} + ports: + {{- include "tc.v1.common.lib.endpoint.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} +{{- end -}} diff --git a/library/common/templates/class/_service.tpl b/library/common/templates/class/_service.tpl index 02ebe44f..35c6084a 100644 --- a/library/common/templates/class/_service.tpl +++ b/library/common/templates/class/_service.tpl @@ -100,6 +100,15 @@ spec: {{- end }} {{- end -}} {{- if eq $objectData.type "ExternalIP" -}} - {{- include "tc.v1.common.class.endpointSlice" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- $useSlice := true -}} + {{- if kindIs "bool" $objectData.useSlice -}} + {{- $useSlice = $objectData.useSlice -}} + {{- end -}} + + {{- if $useSlice -}} + {{- include "tc.v1.common.class.endpointSlice" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- else -}} + {{- include "tc.v1.common.class.endpoint" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- end -}} {{- end -}} {{- end -}} diff --git a/library/common/templates/lib/endpoint/_addresses.tpl b/library/common/templates/lib/endpoint/_addresses.tpl new file mode 100644 index 00000000..381bb1f6 --- /dev/null +++ b/library/common/templates/lib/endpoint/_addresses.tpl @@ -0,0 +1,20 @@ +{{/* Endpoint - addresses */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.endpoint.addresses" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the chart. +objectData: The object data of the service +*/}} + +{{- define "tc.v1.common.lib.endpoint.addresses" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- if not $objectData.externalIP -}} + {{- fail "EndpointSlice - Expected non-empty " -}} + {{- end -}} + + {{- if not (kindIs "string" $objectData.externalIP) -}} {{/* Only single IP is supported currently on this lib */}} + {{- fail (printf "EndpointSlice - Expected to be a [string], but got [%s]" (kindOf $objectData.externalIP)) -}} + {{- end }} + - ip: {{ tpl $objectData.externalIP $rootCtx }} +{{- end -}} diff --git a/library/common/templates/lib/endpoint/_ports.tpl b/library/common/templates/lib/endpoint/_ports.tpl new file mode 100644 index 00000000..f0f9b937 --- /dev/null +++ b/library/common/templates/lib/endpoint/_ports.tpl @@ -0,0 +1,40 @@ +{{/* Endpoint - Ports */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.endpoint.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the chart. +objectData: The object data of the service +*/}} + +{{- define "tc.v1.common.lib.endpoint.ports" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $tcpProtocols := (list "tcp" "http" "https") -}} + {{- range $name, $portValues := $objectData.ports -}} + {{- if $portValues.enabled -}} + {{- $protocol := $rootCtx.Values.fallbackDefaults.serviceProtocol -}} {{/* Default to fallback protocol, if no protocol is defined */}} + {{- $port := $portValues.targetPort | default $portValues.port -}} + + {{/* Expand targetPort */}} + {{- if (kindIs "string" $port) -}} + {{- $port = (tpl $port $rootCtx) -}} + {{- end -}} + {{- $port = int $port -}} + + {{- with $portValues.protocol -}} + {{- $protocol = tpl . $rootCtx -}} + + {{- if mustHas $protocol $tcpProtocols -}} + {{- $protocol = "tcp" -}} + {{- end -}} + {{- end }} +- name: {{ $name }} + port: {{ $port }} + protocol: {{ $protocol | upper }} + {{- with $portValues.appProtocol }} + appProtocol: {{ tpl . $rootCtx | lower }} + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}}