From e0e0a4a2f234922ed7141478634911207cbacbc5 Mon Sep 17 00:00:00 2001 From: Kjeld Schouten-Lebbing Date: Thu, 9 Sep 2021 21:48:43 +0200 Subject: [PATCH] refactor: Restructure common init and additional container layout to dicts (#950) * refactor: Restructure common init and additional container layout to dicts BREAKING CHANGE --- charts/library/common/Chart.yaml | 2 +- charts/library/common/templates/_all.tpl | 6 ++-- .../addons/code-server/_codeserver.tpl | 3 +- .../templates/addons/netshoot/_netshoot.tpl | 3 +- .../templates/addons/promtail/_promtail.tpl | 3 +- .../templates/addons/vpn/openvpn/_addon.tpl | 3 +- .../templates/addons/vpn/wireguard/_addon.tpl | 3 +- .../templates/classes/_service_ports.tpl | 2 +- .../templates/lib/controller/_container.tpl | 3 +- .../common/templates/lib/controller/_pod.tpl | 28 ++++++++++++++++--- .../templates/lib/controller/_ports.tpl | 1 - charts/library/common/values.yaml | 18 +++++++++--- 12 files changed, 50 insertions(+), 25 deletions(-) diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 2d665873daf..63b9fbe86b7 100644 --- a/charts/library/common/Chart.yaml +++ b/charts/library/common/Chart.yaml @@ -18,4 +18,4 @@ maintainers: name: common sources: null type: library -version: 6.14.0 +version: 7.0.0 diff --git a/charts/library/common/templates/_all.tpl b/charts/library/common/templates/_all.tpl index 11907c053ce..581c27a7dfc 100644 --- a/charts/library/common/templates/_all.tpl +++ b/charts/library/common/templates/_all.tpl @@ -5,9 +5,6 @@ Main entrypoint for the common library chart. It will render all underlying temp {{- /* Merge the local chart values and the common chart defaults */ -}} {{- include "common.values.setup" . }} - {{- /* Build the templates */ -}} - {{- include "common.pvc" . }} - {{- /* Enable code-server add-on if required */ -}} {{- if .Values.addons.codeserver.enabled }} {{- include "common.addon.codeserver" . }} @@ -28,6 +25,9 @@ Main entrypoint for the common library chart. It will render all underlying temp {{- include "common.addon.netshoot" . }} {{- end -}} + {{- /* Build the templates */ -}} + {{- include "common.pvc" . }} + {{- if .Values.serviceAccount.create -}} {{- include "common.serviceAccount" . }} {{- end -}} diff --git a/charts/library/common/templates/addons/code-server/_codeserver.tpl b/charts/library/common/templates/addons/code-server/_codeserver.tpl index f8b39bfcbca..ef1ca677f29 100644 --- a/charts/library/common/templates/addons/code-server/_codeserver.tpl +++ b/charts/library/common/templates/addons/code-server/_codeserver.tpl @@ -7,8 +7,7 @@ It will include / inject the required templates based on the given values. {{/* Append the code-server container to the additionalContainers */}} {{- $container := include "common.addon.codeserver.container" . | fromYaml -}} {{- if $container -}} - {{- $additionalContainers := append .Values.additionalContainers $container -}} - {{- $_ := set .Values "additionalContainers" $additionalContainers -}} + {{- $_ := set .Values.additionalContainers "addon-codeserver" $container -}} {{- end -}} {{/* Include the deployKeySecret if not empty */}} diff --git a/charts/library/common/templates/addons/netshoot/_netshoot.tpl b/charts/library/common/templates/addons/netshoot/_netshoot.tpl index 307dab53721..9a1e9fae731 100644 --- a/charts/library/common/templates/addons/netshoot/_netshoot.tpl +++ b/charts/library/common/templates/addons/netshoot/_netshoot.tpl @@ -7,8 +7,7 @@ It will include / inject the required templates based on the given values. {{/* Append the netshoot container to the additionalContainers */}} {{- $container := include "common.addon.netshoot.container" . | fromYaml -}} {{- if $container -}} - {{- $additionalContainers := append .Values.additionalContainers $container -}} - {{- $_ := set .Values "additionalContainers" $additionalContainers -}} + {{- $_ := set .Values.additionalContainers "addon-netshoot" $container -}} {{- end -}} {{- end -}} {{- end -}} diff --git a/charts/library/common/templates/addons/promtail/_promtail.tpl b/charts/library/common/templates/addons/promtail/_promtail.tpl index 7526050563f..c8ef10f941f 100644 --- a/charts/library/common/templates/addons/promtail/_promtail.tpl +++ b/charts/library/common/templates/addons/promtail/_promtail.tpl @@ -7,8 +7,7 @@ It will include / inject the required templates based on the given values. {{/* Append the promtail container to the additionalContainers */}} {{- $container := include "common.addon.promtail.container" . | fromYaml -}} {{- if $container -}} - {{- $additionalContainers := append .Values.additionalContainers $container -}} - {{- $_ := set .Values "additionalContainers" $additionalContainers -}} + {{- $_ := set .Values.additionalContainers "addon-promtail" $container -}} {{- end -}} {{/* Include the configmap if not empty */}} diff --git a/charts/library/common/templates/addons/vpn/openvpn/_addon.tpl b/charts/library/common/templates/addons/vpn/openvpn/_addon.tpl index 32354bcbdc4..1cfcdd51fad 100644 --- a/charts/library/common/templates/addons/vpn/openvpn/_addon.tpl +++ b/charts/library/common/templates/addons/vpn/openvpn/_addon.tpl @@ -6,8 +6,7 @@ and add a credentials secret if speciffied. {{/* Append the openVPN container to the additionalContainers */}} {{- $container := include "common.addon.openvpn.container" . | fromYaml -}} {{- if $container -}} - {{- $additionalContainers := append .Values.additionalContainers $container -}} - {{- $_ := set .Values "additionalContainers" $additionalContainers -}} + {{- $_ := set .Values.additionalContainers "addon-openvpn" $container -}} {{- end -}} {{/* Include the secret if not empty */}} diff --git a/charts/library/common/templates/addons/vpn/wireguard/_addon.tpl b/charts/library/common/templates/addons/vpn/wireguard/_addon.tpl index 3213b5faa97..6c7ea35c9e2 100644 --- a/charts/library/common/templates/addons/vpn/wireguard/_addon.tpl +++ b/charts/library/common/templates/addons/vpn/wireguard/_addon.tpl @@ -6,7 +6,6 @@ Template to render Wireguard addon. It will add the container to the list of add {{/* Append the Wireguard container to the additionalContainers */}} {{- $container := fromYaml (include "common.addon.wireguard.container" .) -}} {{- if $container -}} - {{- $additionalContainers := append .Values.additionalContainers $container -}} - {{- $_ := set .Values "additionalContainers" $additionalContainers -}} + {{- $_ := set .Values.additionalContainers "addon-wireguard" $container -}} {{- end -}} {{- end -}} diff --git a/charts/library/common/templates/classes/_service_ports.tpl b/charts/library/common/templates/classes/_service_ports.tpl index 4e287de52e2..32cae4c8e19 100644 --- a/charts/library/common/templates/classes/_service_ports.tpl +++ b/charts/library/common/templates/classes/_service_ports.tpl @@ -1,5 +1,5 @@ {{/* -Return the name of the primary port for a given Service object. +Return the primary port for a given Service object. */}} {{- define "common.classes.service.ports.primary" -}} {{- $enabledPorts := dict -}} diff --git a/charts/library/common/templates/lib/controller/_container.tpl b/charts/library/common/templates/lib/controller/_container.tpl index e6f3a17b68a..64fca9938b6 100644 --- a/charts/library/common/templates/lib/controller/_container.tpl +++ b/charts/library/common/templates/lib/controller/_container.tpl @@ -102,7 +102,8 @@ - secretRef: name: {{ include "common.names.fullname" . }} {{- end }} - {{- include "common.controller.ports" . | trim | nindent 2 }} + ports: + {{- include "common.controller.ports" . | trim | nindent 4 }} {{- with (include "common.controller.volumeMounts" . | trim) }} volumeMounts: {{ nindent 4 . }} diff --git a/charts/library/common/templates/lib/controller/_pod.tpl b/charts/library/common/templates/lib/controller/_pod.tpl index fd1dc2d00af..0c91a5dc63c 100644 --- a/charts/library/common/templates/lib/controller/_pod.tpl +++ b/charts/library/common/templates/lib/controller/_pod.tpl @@ -39,14 +39,30 @@ enableServiceLinks: {{ .Values.enableServiceLinks }} terminationGracePeriodSeconds: {{ . }} {{- end }} initContainers: - {{- include "common.controller.autopermissions" . | nindent 2 }} - {{- with .Values.initContainers }} - {{- toYaml . | nindent 2 }} + {{- include "common.controller.autopermissions" . | nindent 2 }} + {{- if .Values.initContainers }} + {{- $initContainers := list }} + {{- range $index, $key := (keys .Values.initContainers | uniq | sortAlpha) }} + {{- $container := get $.Values.initContainers $key }} + {{- if not $container.name -}} + {{- $_ := set $container "name" $key }} + {{- end }} + {{- $initContainers = append $initContainers $container }} + {{- end }} + {{- tpl (toYaml $initContainers) $ | nindent 2 }} {{- end }} + containers: {{- include "common.controller.mainContainer" . | nindent 2 }} {{- with .Values.additionalContainers }} - {{- tpl (toYaml .) $ | nindent 2 }} + {{- $additionalContainers := list }} + {{- range $name, $container := . }} + {{- if not $container.name -}} + {{- $_ := set $container "name" $name }} + {{- end }} + {{- $additionalContainers = append $additionalContainers $container }} + {{- end }} + {{- tpl (toYaml $additionalContainers) $ | nindent 2 }} {{- end }} {{- with (include "common.controller.volumes" . | trim) }} volumes: @@ -64,6 +80,10 @@ nodeSelector: affinity: {{- toYaml . | nindent 2 }} {{- end }} + {{- with .Values.topologySpreadConstraints }} +topologySpreadConstraints: + {{- toYaml . | nindent 2 }} + {{- end }} {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 2 }} diff --git a/charts/library/common/templates/lib/controller/_ports.tpl b/charts/library/common/templates/lib/controller/_ports.tpl index 39e4e9300b4..5394690c237 100644 --- a/charts/library/common/templates/lib/controller/_ports.tpl +++ b/charts/library/common/templates/lib/controller/_ports.tpl @@ -14,7 +14,6 @@ Ports included by the controller. {{/* export/render the list of ports */}} {{- if $ports -}} -ports: {{- range $_ := $ports }} {{- if .enabled }} - name: {{ .name }} diff --git a/charts/library/common/values.yaml b/charts/library/common/values.yaml index 141d7cba579..b6e97818137 100644 --- a/charts/library/common/values.yaml +++ b/charts/library/common/values.yaml @@ -205,11 +205,13 @@ securityContext: # -- Configure the lifecycle for the main container lifecycle: {} -# -- Specify any initContainers here. Yaml will be passed in to the Pod as-is. -initContainers: [] +# -- Specify any initContainers here as dictionary items. Each initContainer should have its own key. +# The dictionary item key will determine the order. Helm templates can be used. +initContainers: {} -# -- Specify any additional containers here. Yaml will be passed in to the Pod as-is. -additionalContainers: [] +# -- Specify any additional containers here as dictionary items. Each additional container should have its own key. +# Helm templates can be used. +additionalContainers: {} # -- Probe configuration # -- [[ref]](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/) @@ -589,6 +591,14 @@ nodeSelector: {} # [[ref]](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity) affinity: {} +# -- Defines topologySpreadConstraint rules. +# [[ref]](https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/) +topologySpreadConstraints: [] +# - maxSkew: +# topologyKey: +# whenUnsatisfiable: +# labelSelector: + # -- Specify taint tolerations # [[ref]](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/) tolerations: []