From d0d993aa8190c44fcc78ad43af33af5baeabb772 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Mon, 30 Jan 2023 19:37:01 +0200 Subject: [PATCH] first targetSelector --- .../tests/pod/image_pull_secret_test.yaml | 155 ++++++++++++++++++ library/common-test/values.yaml | 2 +- .../1.0.0/templates/classes/_cronjob.tpl | 2 +- .../1.0.0/templates/classes/_daemonset.tpl | 2 +- .../1.0.0/templates/classes/_deployment.tpl | 2 +- .../common/1.0.0/templates/classes/_job.tpl | 2 +- .../1.0.0/templates/classes/_statefulset.tpl | 2 +- .../1.0.0/templates/lib/controller/_pod.tpl | 5 + .../templates/lib/pod/_imagePullSecrets.tpl | 40 +++++ .../1.0.0/templates/spawner/_controller.tpl | 3 + library/common/1.0.0/values.yaml | 1 - 11 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 library/common-test/tests/pod/image_pull_secret_test.yaml create mode 100644 library/common/1.0.0/templates/lib/pod/_imagePullSecrets.tpl diff --git a/library/common-test/tests/pod/image_pull_secret_test.yaml b/library/common-test/tests/pod/image_pull_secret_test.yaml new file mode 100644 index 0000000000..bbfc385618 --- /dev/null +++ b/library/common-test/tests/pod/image_pull_secret_test.yaml @@ -0,0 +1,155 @@ +suite: pod imagePullSecret test +templates: + - common.yaml +tests: + - it: should assign multiple imagePullSecrets to primary pod + set: + controllers: + controller-name1: + enabled: true + primary: true + type: CronJob + schedule: "* * * * *" + podSpec: {} + controller-name2: + enabled: true + primary: false + type: Deployment + podSpec: {} + imagePullSecrets: + pull-secret1: + enabled: true + data: + registry: reg + username: user + password: pass + email: mail + pull-secret2: + enabled: true + data: + registry: reg + username: user + password: pass + email: mail + asserts: + - documentIndex: &cronJobDoc 2 + isKind: + of: CronJob + - documentIndex: *cronJobDoc + equal: + path: metadata.name + value: release-name-common-test + - documentIndex: *cronJobDoc + equal: + path: spec.jobTemplate.spec.template.spec.imagePullSecrets + value: + - release-name-common-test-pull-secret1 + - release-name-common-test-pull-secret2 + - documentIndex: &otherDeploymentDoc 3 + isKind: + of: Deployment + - documentIndex: *otherDeploymentDoc + isNull: + path: spec.template.spec.imagePullSecrets + + - it: should assign multiple imagePullSecrets to all pods + set: + controllers: + controller-name1: + enabled: true + primary: true + type: Deployment + podSpec: {} + controller-name2: + enabled: true + primary: false + type: Deployment + podSpec: {} + imagePullSecrets: + pull-secret1: + enabled: true + data: + registry: reg + username: user + password: pass + email: mail + targetSelector: all + pull-secret2: + enabled: true + data: + registry: reg + username: user + password: pass + email: mail + targetSelector: all + asserts: + - documentIndex: &deploymentDoc 2 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + equal: + path: spec.template.spec.imagePullSecrets + value: + - release-name-common-test-pull-secret1 + - release-name-common-test-pull-secret2 + - documentIndex: &otherDeploymentDoc 3 + isKind: + of: Deployment + - documentIndex: *otherDeploymentDoc + equal: + path: spec.template.spec.imagePullSecrets + value: + - release-name-common-test-pull-secret1 + - release-name-common-test-pull-secret2 + + - it: should assign imagePullSecrets to selected pods + set: + controllers: + controller-name1: + enabled: true + primary: true + type: DaemonSet + podSpec: {} + controller-name2: + enabled: true + primary: false + type: Deployment + podSpec: {} + imagePullSecrets: + pull-secret1: + enabled: true + data: + registry: reg + username: user + password: pass + email: mail + targetSelector: + - controller-name1 + - controller-name2 + pull-secret2: + enabled: true + data: + registry: reg + username: user + password: pass + email: mail + targetSelector: + - controller-name1 + asserts: + - documentIndex: &daemonSetDoc 2 + isKind: + of: DaemonSet + - documentIndex: *daemonSetDoc + equal: + path: spec.template.spec.imagePullSecrets + value: + - release-name-common-test-pull-secret1 + - release-name-common-test-pull-secret2 + - documentIndex: &otherDeploymentDoc 3 + isKind: + of: Deployment + - documentIndex: *otherDeploymentDoc + equal: + path: spec.template.spec.imagePullSecrets + value: + - release-name-common-test-pull-secret1 diff --git a/library/common-test/values.yaml b/library/common-test/values.yaml index 120c72590d..2f91c7228a 100644 --- a/library/common-test/values.yaml +++ b/library/common-test/values.yaml @@ -4,4 +4,4 @@ # controllers (Tests use a different named controller) controllers: main: - enabled: true + enabled: false diff --git a/library/common/1.0.0/templates/classes/_cronjob.tpl b/library/common/1.0.0/templates/classes/_cronjob.tpl index 4944c55f65..585b953993 100644 --- a/library/common/1.0.0/templates/classes/_cronjob.tpl +++ b/library/common/1.0.0/templates/classes/_cronjob.tpl @@ -46,5 +46,5 @@ spec: {{- . | nindent 12 }} {{- end }} spec: - {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 10 }} + {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 10 }} {{- end -}} diff --git a/library/common/1.0.0/templates/classes/_daemonset.tpl b/library/common/1.0.0/templates/classes/_daemonset.tpl index 6ff6249c10..74367439b5 100644 --- a/library/common/1.0.0/templates/classes/_daemonset.tpl +++ b/library/common/1.0.0/templates/classes/_daemonset.tpl @@ -49,5 +49,5 @@ spec: {{- . | nindent 8 }} {{- end }} spec: - {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 6 }} + {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} {{- end -}} diff --git a/library/common/1.0.0/templates/classes/_deployment.tpl b/library/common/1.0.0/templates/classes/_deployment.tpl index 75ee7f7015..358fc25bd4 100644 --- a/library/common/1.0.0/templates/classes/_deployment.tpl +++ b/library/common/1.0.0/templates/classes/_deployment.tpl @@ -49,5 +49,5 @@ spec: {{- . | nindent 8 }} {{- end }} spec: - {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 6 }} + {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} {{- end -}} diff --git a/library/common/1.0.0/templates/classes/_job.tpl b/library/common/1.0.0/templates/classes/_job.tpl index a73d7dc5c0..0f566f4779 100644 --- a/library/common/1.0.0/templates/classes/_job.tpl +++ b/library/common/1.0.0/templates/classes/_job.tpl @@ -46,5 +46,5 @@ spec: {{- . | nindent 8 }} {{- end }} spec: - {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 6 }} + {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} {{- end -}} diff --git a/library/common/1.0.0/templates/classes/_statefulset.tpl b/library/common/1.0.0/templates/classes/_statefulset.tpl index 955e46c42f..ffd1127805 100644 --- a/library/common/1.0.0/templates/classes/_statefulset.tpl +++ b/library/common/1.0.0/templates/classes/_statefulset.tpl @@ -49,5 +49,5 @@ spec: {{- . | nindent 8 }} {{- end }} spec: - {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 6 }} + {{- include "ix.v1.common.lib.controller.pod" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 6 }} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/controller/_pod.tpl b/library/common/1.0.0/templates/lib/controller/_pod.tpl index 8aa9cccc9c..c95298c938 100644 --- a/library/common/1.0.0/templates/lib/controller/_pod.tpl +++ b/library/common/1.0.0/templates/lib/controller/_pod.tpl @@ -7,5 +7,10 @@ objectData: The object data to be used to render the Pod. {{- define "ix.v1.common.lib.controller.pod" -}} {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData -}} + {{- with (include "ix.v1.common.lib.pod.imagePullSecrets" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} +imagePullSecrets: +{{- . | nindent 2 }} + {{- end }} +hostNetwork: false {{- end -}} diff --git a/library/common/1.0.0/templates/lib/pod/_imagePullSecrets.tpl b/library/common/1.0.0/templates/lib/pod/_imagePullSecrets.tpl new file mode 100644 index 0000000000..52f77451c3 --- /dev/null +++ b/library/common/1.0.0/templates/lib/pod/_imagePullSecrets.tpl @@ -0,0 +1,40 @@ +{{/* Returns Image Pull Secret List */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.pod.imagePullSecrets" (dict "rootCtx" $ "objectData" $objectData) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: The object data to be used to render the Pod. +*/}} +{{- define "ix.v1.common.lib.pod.imagePullSecrets" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- $imgPullSecrets := list -}} + {{- $podSelected := true -}} + + {{- range $name, $imgPull := $rootCtx.Values.imagePullSecrets -}} + {{- $pullName := (printf "%s-%s" (include "ix.v1.common.lib.chart.names.fullname" $rootCtx) $name) -}} + + {{- if $imgPull.enabled -}} + {{- if $imgPull.targetSelector -}} + + {{- if and (kindIs "string" $imgPull.targetSelector) (eq $imgPull.targetSelector "all") -}} + {{- $imgPullSecrets = mustAppend $imgPullSecrets $pullName -}} + {{- else -}} + {{- if (mustHas $objectData.shortName $imgPull.targetSelector) -}} + {{- $imgPullSecrets = mustAppend $imgPullSecrets $pullName -}} + {{- end -}} + {{- end -}} + + {{/* If not targetSelector, but is the primary pod */}} + {{- else if $objectData.primary -}} + {{- $imgPullSecrets = mustAppend $imgPullSecrets $pullName -}} + {{- end -}} + + {{- end -}} + {{- end -}} + + {{- range $imgPullSecrets }} +- {{ . }} + {{- end -}} +{{- end -}} +{{/* TODO: tests */}} diff --git a/library/common/1.0.0/templates/spawner/_controller.tpl b/library/common/1.0.0/templates/spawner/_controller.tpl index 7b1862b329..36e9c8b359 100644 --- a/library/common/1.0.0/templates/spawner/_controller.tpl +++ b/library/common/1.0.0/templates/spawner/_controller.tpl @@ -28,6 +28,9 @@ {{/* Set the name of the controller */}} {{- $_ := set $objectData "name" $objectName -}} + {{/* Short name is the one that defined on the chart, used on selectors */}} + {{- $_ := set $objectData "shortName" $name -}} + {{/* Call class to create the object */}} {{- if eq $objectData.type "Deployment" -}} {{- include "ix.v1.common.class.deployment" (dict "objectData" $objectData "rootCtx" $) -}} diff --git a/library/common/1.0.0/values.yaml b/library/common/1.0.0/values.yaml index 3e48090ee9..e77f4e3170 100644 --- a/library/common/1.0.0/values.yaml +++ b/library/common/1.0.0/values.yaml @@ -549,7 +549,6 @@ portal: {} # password: "" # # -- Email # email: "" -# # TODO: Add support for targetSelector # # targetSelector: all # targetSelector: # - pod-name