From a6e78d6e77e46bb6a831004bf3bc89e888f1570e Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Fri, 25 Nov 2022 14:48:50 +0200 Subject: [PATCH] expand service --- .../common/1.0.0/templates/class/_service.tpl | 93 ++++++++++++------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/library/common/1.0.0/templates/class/_service.tpl b/library/common/1.0.0/templates/class/_service.tpl index 0c98f445..dd235304 100644 --- a/library/common/1.0.0/templates/class/_service.tpl +++ b/library/common/1.0.0/templates/class/_service.tpl @@ -35,18 +35,16 @@ metadata: metallb.universe.tf/allow-shared-ip: {{ include "ix.v1.common.names.fullname" . }} {{- end -}} spec: +{{- if has $svcType (list "ClusterIP" "NodePort" "ExternalName" "LoadBalancer") }} + type: {{ $svcType }} {{/* Specify type only for the above types */}} +{{- end -}} {{- if eq $svcType "ClusterIP" }} {{/* ClusterIP */}} - type: {{ $svcType }} {{- with $svcValues.clusterIP }} clusterIP: {{ . }} {{- end }} -{{- else if eq $svcType "NodePort" -}} {{/* NodePort */}} - type: {{ $svcType }} {{- else if eq $svcType "ExternalName" -}} {{/* ExternalName */}} - type: {{ $svcType }} externalName: {{ $svcValues.externalName }} {{- else if eq $svcType "LoadBalancer" -}} {{/* LoadBalancer */}} - type: {{ $svcType }} {{- with $svcValues.loadBalancerIP }} loadBalancerIP: {{ . }} {{- end }} @@ -55,57 +53,86 @@ spec: {{- range . }} - {{ tpl . $root }} {{- end }} - {{- end }} - {{- if $svcValues.externalTrafficPolicy -}} - {{- if not (has $svcValues.externalTrafficPolicy (list "Cluster" "Local")) -}} - {{- fail (printf "Invalid option (%s) for . Valid options are Cluster and Local" $svcValues) -}} - {{- end }} - externalTrafficPolicy: {{ $svcValues.externalTrafficPolicy }} {{- end -}} - {{- with $svcValues.sessionAffinity }} +{{- end -}} +{{- if $svcValues.externalTrafficPolicy -}} + {{- if not (has $svcValues.externalTrafficPolicy (list "Cluster" "Local")) -}} + {{- fail (printf "Invalid option (%s) for . Valid options are Cluster and Local" $svcValues) -}} + {{- end }} + externalTrafficPolicy: {{ $svcValues.externalTrafficPolicy }} +{{- end -}} +{{- with $svcValues.sessionAffinity }} + {{- if not (has . (list "ClientIP" "None")) -}} + {{- fail (printf "Invalid option (%s). Valid options are ClusterIP and None" .) -}} + {{- end }} sessionAffinity: {{ . }} - {{- with $svcValues.sessionAffinityConfig -}} - {{- with .ClientIP -}} - {{- with .timeoutSecond }} + {{- with $svcValues.sessionAffinityConfig -}} + {{- with .ClientIP -}} + {{- if hasKey . "timeoutSecond" }} + {{- if or (lt . 0) (gt . 86400) -}} + {{- fail (printf "Invalid value (%s) for . Valid values must be with 0 and 86400" .) -}} + {{- end }} sessionAffinityConfig: ClientIP: timeoutSeconds: {{ tpl . $root }} - {{- end -}} {{- end -}} {{- end -}} {{- end -}} - {{- with $svcValues.externalIPs }} +{{- end -}} +{{- with $svcValues.externalIPs }} externalIPs: - {{- range . }} + {{- range . }} - {{ tpl . $root }} + {{- end }} +{{- end -}} +{{- with $svcValues.publishNotReadyAddresses }} + publishNotReadyAddresses: {{ . }} +{{- end -}} +{{- if has $svcType (list "ClusterIP" "NodePort" "LoadBalancer") -}} + {{- with $svcValues.ipFamilyPolicy }} + {{- if not (has . (list "SingleStack" "PreferDualStack" "RequireDualStack")) -}} + {{ fail (printf "Invalid option (%s) for . Valid options are SingleStack, PreferDualStack, RequireDualStack" .) -}} + {{- end }} + ipFamilyPolicy: {{ . }} + {{- end -}} + {{- with $svcValues.ipFamilies }} + ipFamilies: + {{- range . }} + {{- $ipFam := tpl . $root -}} + {{- if not has $ipFam (list "IPv4" "IPv6") -}} + {{- fail (printf "Invalid option (%s) for . Valid options are IPv4 and IPv6" $ipFam) -}} + {{- end }} + - {{ $ipFam }} {{- end }} {{- end -}} {{- end -}} ports: {{- range $name, $port := $svcValues.ports }} {{- if $port.enabled }} + {{- $protocol := "TCP" -}} {{/* Default to TCP if no protocol is specified */}} + {{- with $port.protocol }} + {{- if has . (list "HTTP" "HTTPS" "TCP") -}} + {{- $protocol = "TCP" -}} + {{- else -}} + {{- $protocol = . -}} + {{- end -}} + {{- end }} - port: {{ $port.port }} - targetPort: {{ $port.targetPort | default $name }} - {{- if $port.protocol -}} - {{- if has $port.protocol (list "HTTP" "HTTPS" "TCP") }} - protocol: TCP - {{- else }} - protocol: {{ $port.protocol }} - {{- end }} - {{- else }} - protocol: TCP - {{- end }} name: {{ $name }} - {{- if (and $port.nodePort (eq $svcType "NodePort")) }} + protocol: {{ $protocol }} + targetPort: {{ $port.targetPort | default $name }} + {{- if and (eq $svcType "NodePort") $port.nodePort }} nodePort: {{ $port.nodePort }} - {{- end }} + {{- end -}} {{- end -}} {{- end -}} {{- if not (has $svcType (list "ExternalName" "ExternalIP")) }} selector: - {{- with $svcValues.selector }} - {{/*TODO: */}} - {{- else }} + {{- with $svcValues.selector -}} {{/* If custom selector defined */}} + {{- range $k, $v := . }} + {{ $k }}: {{ tpl $v $root }} + {{- end -}} + {{- else }} {{/* else use the generated selectors */}} {{- include "ix.v1.common.labels.selectorLabels" $root | nindent 4 }} {{- end }} {{- end -}}