From 64b4d88c727a1d7ad163e5449a79ca21dce7ceaa Mon Sep 17 00:00:00 2001 From: Ben Ash <32777270+benashz@users.noreply.github.com> Date: Fri, 23 Jul 2021 12:05:24 -0400 Subject: [PATCH] feature: imagePullSecrets from string array. (#576) * allow configuring imagePullSecrets from an array of strings in addition to the already supported array of maps --- templates/_helpers.tpl | 17 +++++++++++++++++ templates/csi-daemonset.yaml | 5 +---- templates/injector-deployment.yaml | 5 +---- templates/server-statefulset.yaml | 5 +---- test/unit/csi-daemonset.bats | 27 +++++++++++++++++++++++++++ test/unit/server-statefulset.bats | 26 ++++++++++++++++++++++++++ values.yaml | 1 + 7 files changed, 74 insertions(+), 12 deletions(-) diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl index 72b0e6803..29364aa33 100644 --- a/templates/_helpers.tpl +++ b/templates/_helpers.tpl @@ -623,3 +623,20 @@ Inject extra environment populated by secrets, if populated {{ "https" }} {{- end -}} {{- end -}} + +{{/* +imagePullSecrets generates pull secrets from either string or map values. +A map value must be indexable by the key 'name'. +*/}} +{{- define "imagePullSecrets" -}} +{{- with .Values.global.imagePullSecrets -}} +imagePullSecrets: +{{- range . -}} +{{- if typeIs "string" . }} + - name: {{ . }} +{{- else if index . "name" }} + - name: {{ .name }} +{{- end }} +{{- end -}} +{{- end -}} +{{- end -}} diff --git a/templates/csi-daemonset.yaml b/templates/csi-daemonset.yaml index 75bde9a32..66fe055de 100644 --- a/templates/csi-daemonset.yaml +++ b/templates/csi-daemonset.yaml @@ -77,8 +77,5 @@ spec: {{- if .Values.csi.volumes }} {{- toYaml .Values.csi.volumes | nindent 8}} {{- end }} - {{- if .Values.global.imagePullSecrets }} - imagePullSecrets: - {{- toYaml .Values.global.imagePullSecrets | nindent 8 }} - {{- end }} + {{- include "imagePullSecrets" . | nindent 6 }} {{- end }} diff --git a/templates/injector-deployment.yaml b/templates/injector-deployment.yaml index c063a8705..261be1c2a 100644 --- a/templates/injector-deployment.yaml +++ b/templates/injector-deployment.yaml @@ -174,8 +174,5 @@ spec: secret: secretName: "{{ .Values.injector.certs.secretName }}" {{- end }} - {{- if .Values.global.imagePullSecrets }} - imagePullSecrets: - {{- toYaml .Values.global.imagePullSecrets | nindent 8 }} - {{- end }} + {{- include "imagePullSecrets" . | nindent 6 }} {{ end }} diff --git a/templates/server-statefulset.yaml b/templates/server-statefulset.yaml index 718c9a03e..031b17905 100644 --- a/templates/server-statefulset.yaml +++ b/templates/server-statefulset.yaml @@ -202,10 +202,7 @@ spec: {{- if .Values.server.extraContainers }} {{ toYaml .Values.server.extraContainers | nindent 8}} {{- end }} - {{- if .Values.global.imagePullSecrets }} - imagePullSecrets: - {{- toYaml .Values.global.imagePullSecrets | nindent 8 }} - {{- end }} + {{- include "imagePullSecrets" . | nindent 6 }} {{ template "vault.volumeclaims" . }} {{ end }} {{ end }} diff --git a/test/unit/csi-daemonset.bats b/test/unit/csi-daemonset.bats index d7152c61f..c546d0a5b 100644 --- a/test/unit/csi-daemonset.bats +++ b/test/unit/csi-daemonset.bats @@ -72,6 +72,33 @@ load _helpers . | tee /dev/stderr | yq -r '.spec.template.spec.imagePullSecrets' | tee /dev/stderr) + local actual=$(echo $object | + yq -r '. | length' | tee /dev/stderr) + [ "${actual}" = "2" ] + + local actual=$(echo $object | + yq -r '.[0].name' | tee /dev/stderr) + [ "${actual}" = "foo" ] + + local actual=$(echo $object | + yq -r '.[1].name' | tee /dev/stderr) + [ "${actual}" = "bar" ] +} + +@test "csi/daemonset: Custom imagePullSecrets - string array" { + cd `chart_dir` + local object=$(helm template \ + --show-only templates/csi-daemonset.yaml \ + --set "csi.enabled=true" \ + --set 'global.imagePullSecrets[0]=foo' \ + --set 'global.imagePullSecrets[1]=bar' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.imagePullSecrets' | tee /dev/stderr) + + local actual=$(echo $object | + yq -r '. | length' | tee /dev/stderr) + [ "${actual}" = "2" ] + local actual=$(echo $object | yq -r '.[0].name' | tee /dev/stderr) [ "${actual}" = "foo" ] diff --git a/test/unit/server-statefulset.bats b/test/unit/server-statefulset.bats index 62f252919..b93905188 100755 --- a/test/unit/server-statefulset.bats +++ b/test/unit/server-statefulset.bats @@ -146,6 +146,32 @@ load _helpers . | tee /dev/stderr | yq -r '.spec.template.spec.imagePullSecrets' | tee /dev/stderr) + local actual=$(echo $object | + yq -r '. | length' | tee /dev/stderr) + [ "${actual}" = "2" ] + + local actual=$(echo $object | + yq -r '.[0].name' | tee /dev/stderr) + [ "${actual}" = "foo" ] + + local actual=$(echo $object | + yq -r '.[1].name' | tee /dev/stderr) + [ "${actual}" = "bar" ] +} + +@test "server/standalone-StatefulSet: Custom imagePullSecrets - string array" { + cd `chart_dir` + local object=$(helm template \ + --show-only templates/server-statefulset.yaml \ + --set 'global.imagePullSecrets[0]=foo' \ + --set 'global.imagePullSecrets[1]=bar' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.imagePullSecrets' | tee /dev/stderr) + + local actual=$(echo $object | + yq -r '. | length' | tee /dev/stderr) + [ "${actual}" = "2" ] + local actual=$(echo $object | yq -r '.[0].name' | tee /dev/stderr) [ "${actual}" = "foo" ] diff --git a/values.yaml b/values.yaml index 728769615..908e33213 100644 --- a/values.yaml +++ b/values.yaml @@ -5,6 +5,7 @@ global: # will enable or disable all the components within this chart by default. enabled: true # Image pull secret to use for registry authentication. + # Alternatively, the value may be specified as an array of strings. imagePullSecrets: [] # imagePullSecrets: # - name: image-pull-secret