From c8aee19d5babf9d5f5efd5fc44de0cb3fb042ecf Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Mon, 5 Dec 2022 14:58:50 +0200 Subject: [PATCH] cleanup service --- .../common/1.0.0/templates/class/_service.tpl | 45 ++++++++----------- .../templates/lib/service/_clusterIP.tpl | 7 +++ .../templates/lib/service/_clusterIPSpec.tpl | 7 +++ .../templates/lib/service/_externalIPs.tpl | 4 +- .../lib/service/_externalNameSpec.tpl | 8 ++++ .../lib/service/_externalTrafficPolicy.tpl | 6 +-- .../1.0.0/templates/lib/service/_ipFamily.tpl | 29 ++++++------ .../templates/lib/service/_loadBalancer.tpl | 18 -------- .../lib/service/_loadBalancerSpec.tpl | 17 +++++++ .../templates/lib/service/_nodePortSpec.tpl | 8 ++++ 10 files changed, 84 insertions(+), 65 deletions(-) create mode 100644 library/common/1.0.0/templates/lib/service/_clusterIP.tpl create mode 100644 library/common/1.0.0/templates/lib/service/_clusterIPSpec.tpl create mode 100644 library/common/1.0.0/templates/lib/service/_externalNameSpec.tpl delete mode 100644 library/common/1.0.0/templates/lib/service/_loadBalancer.tpl create mode 100644 library/common/1.0.0/templates/lib/service/_loadBalancerSpec.tpl create mode 100644 library/common/1.0.0/templates/lib/service/_nodePortSpec.tpl diff --git a/library/common/1.0.0/templates/class/_service.tpl b/library/common/1.0.0/templates/class/_service.tpl index 077fceb73e..2a01cb2e5d 100644 --- a/library/common/1.0.0/templates/class/_service.tpl +++ b/library/common/1.0.0/templates/class/_service.tpl @@ -14,10 +14,7 @@ {{- if $root.Values.hostNetwork -}} {{- $svcType = "ClusterIP" -}} {{/* When hostNetwork is enabled, force ClusterIP as service type */}} {{- end -}} - {{- $primaryPort := get $svcValues.ports (include "ix.v1.common.lib.util.service.ports.primary" (dict "values" $svcValues "svcName" $svcName)) -}} - {{/* Prepare a dict to pass into includes */}} - {{- $tmpSVC := dict -}} - {{- $_ := set $tmpSVC "name" $svcName }} + {{- $primaryPort := get $svcValues.ports (include "ix.v1.common.lib.util.service.ports.primary" (dict "values" $svcValues "svcName" $svcName)) }} --- apiVersion: {{ include "ix.v1.common.capabilities.service.apiVersion" $root }} kind: Service @@ -41,22 +38,18 @@ metadata: {{- . | nindent 4 }} {{- end }} spec: -{{- if has $svcType (list "ClusterIP" "NodePort" "ExternalName") }} - type: {{ $svcType }} {{/* Specify type only for the above types */}} -{{- end -}} -{{- include "ix.v1.common.class.serivce.loadBalancer" (dict "svc" $svcValues "svcType" $svcType "root" $root) | trim | nindent 2 -}} -{{- if has $svcType (list "ClusterIP" "NodePort" "LoadBalancer") -}} {{/* ClusterIP */}} - {{- with $svcValues.clusterIP }} - clusterIP: {{ . }} + {{- if eq $svcType "ClusterIP" -}} + {{- include "ix.v1.common.class.serivce.clusterIP.spec" (dict "svc" $svcValues "root" $root) | indent 2 -}} + {{- else if eq $svcType "LoadBalancer" -}} + {{- include "ix.v1.common.class.serivce.loadBalancer.spec" (dict "svc" $svcValues "root" $root)| indent 2 -}} + {{- else if eq $svcType "NodePort" -}} + {{- include "ix.v1.common.class.serivce.nodePort.spec" (dict "svc" $svcValues "root" $root) | indent 2 -}} + {{- else if eq $svcType "ExternalName" -}} + {{- include "ix.v1.common.class.serivce.externalName.spec" (dict "svc" $svcValues "root" $root) | indent 2 -}} {{- end -}} -{{- else if eq $svcType "ExternalName" }} {{/* ExternalName */}} - externalName: {{ required " is required when service type is set to ExternalName" $svcValues.externalName }} -{{- end -}} -{{- include "ix.v1.common.class.serivce.externalTrafficPolicy" (dict "svc" $svcValues "svcType" $svcType "root" $root) | trim | nindent 2 -}} -{{- include "ix.v1.common.class.serivce.sessionAffinity" (dict "svc" $svcValues "root" $root) | trim | nindent 2 -}} -{{- include "ix.v1.common.class.serivce.externalIPs" (dict "svc" $svcValues "root" $root) | trim | nindent 2 -}} -{{- include "ix.v1.common.class.serivce.publishNotReadyAddresses" (dict "publishNotReadyAddresses" $svcValues.publishNotReadyAddresses) | trim | nindent 2 -}} -{{- include "ix.v1.common.class.serivce.ipFamily" (dict "svcType" $svcType "svc" $svcValues "root" $root) | trim | nindent 2 }} + {{- include "ix.v1.common.class.serivce.sessionAffinity" (dict "svc" $svcValues "root" $root) | indent 2 -}} + {{- include "ix.v1.common.class.serivce.externalIPs" (dict "svc" $svcValues "root" $root) | indent 2 -}} + {{- include "ix.v1.common.class.serivce.publishNotReadyAddresses" (dict "publishNotReadyAddresses" $svcValues.publishNotReadyAddresses) | indent 2 }} ports: {{- range $name, $port := $svcValues.ports }} {{- if $port.enabled }} @@ -83,12 +76,12 @@ spec: {{- 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 -}} - {{- if eq $svcType "ExternalIP" -}} - {{- $_ := set $tmpSVC "values" $svcValues -}} - {{- include "ix.v1.common.class.serivce.endpoints" (dict "svc" $svcValues "svcName" $svcName "root" $root) }} + {{- else -}} {{/* else use the generated selectors */}} + {{- include "ix.v1.common.labels.selectorLabels" $root | nindent 4 -}} + {{- end -}} +{{- end -}} + {{- if eq $svcType "ExternalIP" -}} + {{- include "ix.v1.common.class.serivce.externalTrafficPolicy" (dict "svc" $svcValues "root" $root) | nindent 2 -}} + {{- include "ix.v1.common.class.serivce.endpoints" (dict "svc" $svcValues "svcName" $svcName "root" $root) | nindent 0 -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_clusterIP.tpl b/library/common/1.0.0/templates/lib/service/_clusterIP.tpl new file mode 100644 index 0000000000..54bda60dec --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/_clusterIP.tpl @@ -0,0 +1,7 @@ +{{- define "ix.v1.common.class.serivce.clusterIP" -}} + {{- $svcValues := .svc -}} + + {{- with $svcValues.clusterIP }} +clusterIP: {{ . }} + {{- end -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_clusterIPSpec.tpl b/library/common/1.0.0/templates/lib/service/_clusterIPSpec.tpl new file mode 100644 index 0000000000..3e0829b5f8 --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/_clusterIPSpec.tpl @@ -0,0 +1,7 @@ +{{- define "ix.v1.common.class.serivce.clusterIP.spec" -}} + {{- $svcValues := .svc -}} + {{- $root := .root }} +type: ClusterIP + {{- include "ix.v1.common.class.serivce.clusterIP" (dict "svc" $svcValues) | trim | nindent 0 -}} + {{- include "ix.v1.common.class.serivce.ipFamily" (dict "svc" $svcValues "root" $root) | trim | nindent 0 -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_externalIPs.tpl b/library/common/1.0.0/templates/lib/service/_externalIPs.tpl index 83b08b8345..eb19ff21db 100644 --- a/library/common/1.0.0/templates/lib/service/_externalIPs.tpl +++ b/library/common/1.0.0/templates/lib/service/_externalIPs.tpl @@ -2,10 +2,10 @@ {{- $svcValues := .svc -}} {{- $root := .root -}} - {{- with $svcValues.externalIPs -}} + {{- with $svcValues.externalIPs }} externalIPs: {{- range . }} - {{ tpl . $root }} - {{- end }} + {{- end -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_externalNameSpec.tpl b/library/common/1.0.0/templates/lib/service/_externalNameSpec.tpl new file mode 100644 index 0000000000..87091f445d --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/_externalNameSpec.tpl @@ -0,0 +1,8 @@ +{{- define "ix.v1.common.class.serivce.externalName.spec" -}} + {{- $svcValues := .svc -}} + {{- $root := .root }} +type: ExternalName +externalName: {{ required " is required when service type is set to ExternalName" $svcValues.externalName }} + {{- include "ix.v1.common.class.serivce.clusterIP" (dict "svc" $svcValues) | trim | nindent 0 -}} + {{- include "ix.v1.common.class.serivce.externalTrafficPolicy" (dict "svc" $svcValues "root" $root) | trim | nindent 0 -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_externalTrafficPolicy.tpl b/library/common/1.0.0/templates/lib/service/_externalTrafficPolicy.tpl index 8877c7c14f..2a61b374cd 100644 --- a/library/common/1.0.0/templates/lib/service/_externalTrafficPolicy.tpl +++ b/library/common/1.0.0/templates/lib/service/_externalTrafficPolicy.tpl @@ -1,13 +1,13 @@ {{- define "ix.v1.common.class.serivce.externalTrafficPolicy" -}} -{{- $svcType := .svcType -}} -{{- $svcValues := .svc -}} + {{- $svcType := .svcType -}} + {{- $svcValues := .svc -}} -{{- if ne $svcType "ClusterIP" -}} {{- with $svcValues.externalTrafficPolicy -}} {{- if not (has . (list "Cluster" "Local")) -}} {{- fail (printf "Invalid option (%s) for . Valid options are Cluster and Local" .) -}} {{- end }} externalTrafficPolicy: {{ . }} {{- end -}} +{{- if ne $svcType "ClusterIP" -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_ipFamily.tpl b/library/common/1.0.0/templates/lib/service/_ipFamily.tpl index 2edf551854..6f616f00e3 100644 --- a/library/common/1.0.0/templates/lib/service/_ipFamily.tpl +++ b/library/common/1.0.0/templates/lib/service/_ipFamily.tpl @@ -1,24 +1,21 @@ {{- define "ix.v1.common.class.serivce.ipFamily" -}} -{{- $svcType := .svcType -}} -{{- $svcValues := .svc -}} -{{- $root := .root -}} + {{- $svcValues := .svc -}} + {{- $root := .root -}} - {{- 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 }} + {{- 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 }} + {{- 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 }} + {{- 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 -}} diff --git a/library/common/1.0.0/templates/lib/service/_loadBalancer.tpl b/library/common/1.0.0/templates/lib/service/_loadBalancer.tpl deleted file mode 100644 index 50c557ba56..0000000000 --- a/library/common/1.0.0/templates/lib/service/_loadBalancer.tpl +++ /dev/null @@ -1,18 +0,0 @@ -{{- define "ix.v1.common.class.serivce.loadBalancer" -}} - {{- $svcType := .svcType -}} - {{- $svcValues := .svc -}} - {{- $root := .root -}} - - {{- if eq $svcType "LoadBalancer" -}} -type: LoadBalancer - {{- with $svcValues.loadBalancerIP }} -loadBalancerIP: {{ . }} - {{- end -}} - {{- with $svcValues.loadBalancerSourceRanges }} -loadBalancerSourceRanges: - {{- range . }} - - {{ tpl . $root }} - {{- end }} - {{- end -}} - {{- end -}} -{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_loadBalancerSpec.tpl b/library/common/1.0.0/templates/lib/service/_loadBalancerSpec.tpl new file mode 100644 index 0000000000..c8bbd94364 --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/_loadBalancerSpec.tpl @@ -0,0 +1,17 @@ +{{- define "ix.v1.common.class.serivce.loadBalancer.spec" -}} + {{- $svcValues := .svc -}} + {{- $root := .root }} +type: LoadBalancer + {{- with $svcValues.loadBalancerIP }} +loadBalancerIP: {{ . }} + {{- end -}} + {{- with $svcValues.loadBalancerSourceRanges }} +loadBalancerSourceRanges: + {{- range . }} + - {{ tpl . $root }} + {{- end }} + {{- end -}} + {{- include "ix.v1.common.class.serivce.clusterIP" (dict "svc" $svcValues) | trim | nindent 0 -}} + {{- include "ix.v1.common.class.serivce.ipFamily" (dict "svc" $svcValues "root" $root) | trim | nindent 0 -}} + {{- include "ix.v1.common.class.serivce.externalTrafficPolicy" (dict "svc" $svcValues "root" $root) | trim | nindent 0 -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_nodePortSpec.tpl b/library/common/1.0.0/templates/lib/service/_nodePortSpec.tpl new file mode 100644 index 0000000000..e8c74f99a7 --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/_nodePortSpec.tpl @@ -0,0 +1,8 @@ +{{- define "ix.v1.common.class.serivce.nodePort.spec" -}} + {{- $svcValues := .svc -}} + {{- $root := .root }} +type: NodePort + {{- include "ix.v1.common.class.serivce.clusterIP" (dict "svc" $svcValues) | trim | nindent 0 -}} + {{- include "ix.v1.common.class.serivce.ipFamily" (dict "svc" $svcValues "root" $root) | trim | nindent 0 -}} + {{- include "ix.v1.common.class.serivce.externalTrafficPolicy" (dict "svc" $svcValues "root" $root) | trim | nindent 0 -}} +{{- end -}}