From 247b7cb51fd49753c19bebd561fe90e44f78cb53 Mon Sep 17 00:00:00 2001 From: Kjeld Schouten-Lebbing Date: Tue, 17 Jan 2023 17:56:12 +0100 Subject: [PATCH] updates --- .../ci/extra-containers-values.yaml | 25 ++++++++ .../tests/deployment/controller_test.yaml | 2 +- ...itional_containers_in_deployment_test.yaml | 30 ++++++++++ ...containers_in_deployment_install_test.yaml | 30 ++++++++++ ...containers_in_deployment_upgrade_test.yaml | 30 ++++++++++ ...install_containers_in_deployment_test.yaml | 30 ++++++++++ ...containers_in_deployment_install_copy.yaml | 30 ++++++++++ ...upgrade_containers_in_deployment_test.yaml | 30 ++++++++++ .../common-test/tests/jobs/cronJob_test.yaml | 3 - library/common-test/tests/jobs/job_test.yaml | 3 - library/common-test/values.yaml | 7 +++ .../templates/lib/container/_volumeMounts.tpl | 4 +- .../templates/lib/pod/_extraContainers.tpl | 2 +- library/common/templates/lib/pod/_pod.tpl | 4 +- library/common/templates/loader/_apply.tpl | 4 +- .../templates/spawner/_jobAndCronJob.tpl | 57 ++++++++++++++++++- 16 files changed, 278 insertions(+), 13 deletions(-) create mode 100644 library/common-test/ci/extra-containers-values.yaml diff --git a/library/common-test/ci/extra-containers-values.yaml b/library/common-test/ci/extra-containers-values.yaml new file mode 100644 index 00000000..9d4a714b --- /dev/null +++ b/library/common-test/ci/extra-containers-values.yaml @@ -0,0 +1,25 @@ +image: + repository: traefik/whoami + pullPolicy: IfNotPresent + tag: latest + +service: + main: + ports: + main: + protocol: HTTP + port: 8080 + +args: + - --port + - "8080" + +# TODO: Add extra containers + +probes: + liveness: + enabled: true + readiness: + enabled: true + startup: + enabled: true diff --git a/library/common-test/tests/deployment/controller_test.yaml b/library/common-test/tests/deployment/controller_test.yaml index a6d747b0..a7aff2ba 100644 --- a/library/common-test/tests/deployment/controller_test.yaml +++ b/library/common-test/tests/deployment/controller_test.yaml @@ -69,7 +69,7 @@ tests: type: not_valid_controller asserts: - failedTemplate: - errorMessage: Not a valid controller.type (not_valid_controller). Valid options are Deployment, DaemonSet, StatefulSet + errorMessage: Not a valid controller.type (not_valid_controller). Valid options are Deployment, DaemonSet, StatefulSet, Job - it: should fail with wrong strategy set: diff --git a/library/common-test/tests/initcontainers_in_deployment/additional_containers_in_deployment_test.yaml b/library/common-test/tests/initcontainers_in_deployment/additional_containers_in_deployment_test.yaml index 69e86d32..dd3a477c 100644 --- a/library/common-test/tests/initcontainers_in_deployment/additional_containers_in_deployment_test.yaml +++ b/library/common-test/tests/initcontainers_in_deployment/additional_containers_in_deployment_test.yaml @@ -76,6 +76,36 @@ tests: image: some-repo-additional2:some-tag-additional2 imagePullPolicy: IfNotPresent + - it: should pass with enabled false from tpl defined in additional container + documentIndex: *deploymentDoc + set: + flag: false + additionalContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNull: + path: spec.template.spec.containers[1] + + - it: should pass with enabled true from tpl defined in additional container + documentIndex: *deploymentDoc + set: + flag: true + additionalContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNotNull: + path: spec.template.spec.containers[1] + - it: should pass with tty and stdin defined in additional container documentIndex: *deploymentDoc set: diff --git a/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_install_test.yaml b/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_install_test.yaml index 32122e96..b5c11e0d 100644 --- a/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_install_test.yaml +++ b/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_install_test.yaml @@ -95,6 +95,36 @@ tests: tty: true stdin: true + - it: should pass with enabled false from tpl defined in init container + documentIndex: *deploymentDoc + set: + flag: false + initContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNull: + path: spec.template.spec.initContainers + + - it: should pass with enabled true from tpl defined in init container + documentIndex: *deploymentDoc + set: + flag: true + initContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNotNull: + path: spec.template.spec.initContainers + - it: should pass with command and args defined in init container documentIndex: *deploymentDoc set: diff --git a/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_upgrade_test.yaml b/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_upgrade_test.yaml index fed7181b..e6c83b7f 100644 --- a/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_upgrade_test.yaml +++ b/library/common-test/tests/initcontainers_in_deployment/init_containers_in_deployment_upgrade_test.yaml @@ -78,6 +78,36 @@ tests: image: some-repo-init2:some-tag-init2 imagePullPolicy: IfNotPresent + - it: should pass with enabled false from tpl defined in init container + documentIndex: *deploymentDoc + set: + flag: false + initContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNull: + path: spec.template.spec.initContainers + + - it: should pass with enabled true from tpl defined in init container + documentIndex: *deploymentDoc + set: + flag: true + initContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNotNull: + path: spec.template.spec.initContainers + - it: should pass with tty and stdin defined in init container documentIndex: *deploymentDoc set: diff --git a/library/common-test/tests/initcontainers_in_deployment/install_containers_in_deployment_test.yaml b/library/common-test/tests/initcontainers_in_deployment/install_containers_in_deployment_test.yaml index 6ac90175..8906767e 100644 --- a/library/common-test/tests/initcontainers_in_deployment/install_containers_in_deployment_test.yaml +++ b/library/common-test/tests/initcontainers_in_deployment/install_containers_in_deployment_test.yaml @@ -90,6 +90,36 @@ tests: image: some-repo-init:some-tag-init imagePullPolicy: Never + - it: should pass with enabled false from tpl defined in install container + documentIndex: *deploymentDoc + set: + flag: false + installContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNull: + path: spec.template.spec.initContainers + + - it: should pass with enabled true from tpl defined in install container + documentIndex: *deploymentDoc + set: + flag: true + installContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNotNull: + path: spec.template.spec.initContainers + - it: should pass with tty and stdin defined in install container documentIndex: *deploymentDoc set: diff --git a/library/common-test/tests/initcontainers_in_deployment/system_containers_in_deployment_install_copy.yaml b/library/common-test/tests/initcontainers_in_deployment/system_containers_in_deployment_install_copy.yaml index b0288900..c4fe50fd 100644 --- a/library/common-test/tests/initcontainers_in_deployment/system_containers_in_deployment_install_copy.yaml +++ b/library/common-test/tests/initcontainers_in_deployment/system_containers_in_deployment_install_copy.yaml @@ -76,6 +76,36 @@ tests: image: some-repo-system2:some-tag-system2 imagePullPolicy: IfNotPresent + - it: should pass with enabled false from tpl defined in system container + documentIndex: *deploymentDoc + set: + flag: false + systemContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNull: + path: spec.template.spec.initContainers + + - it: should pass with enabled true from tpl defined in system container + documentIndex: *deploymentDoc + set: + flag: true + systemContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNotNull: + path: spec.template.spec.initContainers + - it: should pass with tty and stdin defined in system container documentIndex: *deploymentDoc set: diff --git a/library/common-test/tests/initcontainers_in_deployment/upgrade_containers_in_deployment_test.yaml b/library/common-test/tests/initcontainers_in_deployment/upgrade_containers_in_deployment_test.yaml index d88d513c..fffdb229 100644 --- a/library/common-test/tests/initcontainers_in_deployment/upgrade_containers_in_deployment_test.yaml +++ b/library/common-test/tests/initcontainers_in_deployment/upgrade_containers_in_deployment_test.yaml @@ -92,6 +92,36 @@ tests: image: some-repo-init:some-tag-init imagePullPolicy: Never + - it: should pass with enabled false from tpl defined in upgrade container + documentIndex: *deploymentDoc + set: + flag: false + upgradeContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNull: + path: spec.template.spec.initContainers + + - it: should pass with enabled true from tpl defined in upgrade container + documentIndex: *deploymentDoc + set: + flag: true + upgradeContainers: + some-name: + enabled: "{{ .Values.flag }}" + tty: true + stdin: true + asserts: + - isNotNull: + path: spec.template.spec.containers[0] + - isNotNull: + path: spec.template.spec.initContainers + - it: should pass with tty and stdin defined in upgrade container documentIndex: *deploymentDoc set: diff --git a/library/common-test/tests/jobs/cronJob_test.yaml b/library/common-test/tests/jobs/cronJob_test.yaml index 6b7ee58f..62ddaad9 100644 --- a/library/common-test/tests/jobs/cronJob_test.yaml +++ b/library/common-test/tests/jobs/cronJob_test.yaml @@ -474,6 +474,3 @@ tests: backoffLimit: 3 activeDeadlineSeconds: 120 ttlSecondsAfterFinished: 150 - -# TODO: add basic test to make sure jobTemplate spec (mostly containers part) is actually rendered -# Most of the jobTemplate spec will be tested on another file diff --git a/library/common-test/tests/jobs/job_test.yaml b/library/common-test/tests/jobs/job_test.yaml index f47be0c2..0e26a8dd 100644 --- a/library/common-test/tests/jobs/job_test.yaml +++ b/library/common-test/tests/jobs/job_test.yaml @@ -125,6 +125,3 @@ tests: a2: val2 annotation1: val3 annotation2: val4 - -# TODO: add basic test to make sure jobTemplate spec (mostly containers part) is actually rendered -# Most of the jobTemplate spec will be tested on another file diff --git a/library/common-test/values.yaml b/library/common-test/values.yaml index 67cebbb6..db0e439e 100644 --- a/library/common-test/values.yaml +++ b/library/common-test/values.yaml @@ -3,3 +3,10 @@ service: ports: main: port: 65535 + +# controller: +# type: Job + +# cron: +# enabled: true +# schedule: "* * * * *" diff --git a/library/common/templates/lib/container/_volumeMounts.tpl b/library/common/templates/lib/container/_volumeMounts.tpl index fbb88e0d..5e5be10c 100644 --- a/library/common/templates/lib/container/_volumeMounts.tpl +++ b/library/common/templates/lib/container/_volumeMounts.tpl @@ -10,7 +10,7 @@ {{- if not $item.noMount -}} {{- include "ix.v1.common.container.volumeMount" (dict "root" $root "item" $item - "name" $name) | nindent 0 -}} + "name" $name) | indent 0 -}} {{- end -}} {{- end -}} {{- end -}} @@ -20,7 +20,7 @@ {{- range $index, $vct := $root.Values.volumeClaimTemplates -}} {{- include "ix.v1.common.container.volumeMount" (dict "root" $root "item" $vct - "name" (toString $index)) | nindent 0 -}} + "name" (toString $index)) | indent 0 -}} {{- end -}} {{- end -}} {{- else if not $isMainContainer -}} diff --git a/library/common/templates/lib/pod/_extraContainers.tpl b/library/common/templates/lib/pod/_extraContainers.tpl index b657ae26..09f3441c 100644 --- a/library/common/templates/lib/pod/_extraContainers.tpl +++ b/library/common/templates/lib/pod/_extraContainers.tpl @@ -19,7 +19,7 @@ {{- $enabled := true -}} {{/* Default to enable */}} {{- if hasKey $container "enabled" -}} {{/* If has enabled key */}} - {{- $flag := (tpl $container.enabled $root) -}} + {{- $flag := (tpl (toString $container.enabled) $root) -}} {{- if eq "false" $flag -}} {{/* And its kind of bool */}} {{- $enabled = false -}} {{/* Disable the container */}} {{- end -}} diff --git a/library/common/templates/lib/pod/_pod.tpl b/library/common/templates/lib/pod/_pod.tpl index 9ebd371f..8635930b 100644 --- a/library/common/templates/lib/pod/_pod.tpl +++ b/library/common/templates/lib/pod/_pod.tpl @@ -66,7 +66,9 @@ securityContext: {{- with (include "ix.v1.common.controller.mainContainer" (dict "values" $root.Values "root" $root) | trim) }} containers: {{- . | nindent 2 }} - {{- (include "ix.v1.common.controller.extraContainers" (dict "root" $root "containerList" $root.Values.additionalContainers "type" "additional") | trim) | nindent 2 }} + {{- with (include "ix.v1.common.controller.extraContainers" (dict "root" $root "containerList" $root.Values.additionalContainers "type" "additional") | trim) }} + {{- . | nindent 2 }} + {{- end -}} {{- end -}} {{- $installContainers := (include "ix.v1.common.controller.extraContainers" (dict "root" $root "containerList" $root.Values.installContainers "type" "install") | trim) -}} diff --git a/library/common/templates/loader/_apply.tpl b/library/common/templates/loader/_apply.tpl index b6a1ed5d..c74b8291 100644 --- a/library/common/templates/loader/_apply.tpl +++ b/library/common/templates/loader/_apply.tpl @@ -19,8 +19,10 @@ {{- include "ix.v1.common.daemonset" . | nindent 0 -}} {{- else if eq .Values.controller.type "StatefulSet" -}} {{- include "ix.v1.common.statefulset" . | nindent 0 -}} + {{- else if eq .Values.controller.type "Job" -}} + {{/* Pass, it will render from the spawner.jobAndCronJob bellow */}} {{- else -}} - {{- fail (printf "Not a valid controller.type (%s). Valid options are Deployment, DaemonSet, StatefulSet" .Values.controller.type) -}} + {{- fail (printf "Not a valid controller.type (%s). Valid options are Deployment, DaemonSet, StatefulSet, Job" .Values.controller.type) -}} {{- end -}} {{- end -}} diff --git a/library/common/templates/spawner/_jobAndCronJob.tpl b/library/common/templates/spawner/_jobAndCronJob.tpl index 418037ce..98b404f8 100644 --- a/library/common/templates/spawner/_jobAndCronJob.tpl +++ b/library/common/templates/spawner/_jobAndCronJob.tpl @@ -1,5 +1,60 @@ {{- define "ix.v1.common.spawner.jobAndCronJob" -}} - {{- range $jobName, $job := .Values.jobs -}} + {{- $jobs := dict -}} + + {{- if mustHas .Values.controller.type (list "Job" "CronJob") -}} + {{- $jobValues := dict -}} + + {{/* Controller holds Job/CronJob Configuration */}} + {{- $jobValues = $.Values.controller -}} + + {{/* If it's CronJob prepare the cron dict with enabled forced */}} + {{- if eq $.Values.controller.type "CronJob" -}} + {{- $_ := set $jobValues "cron" dict -}} + {{- $_ := set $jobValues.cron $.Values.controller -}} + {{- $_ := set $jobValues.cron "enabled" true -}} + {{- end -}} + + {{/* Enable the Job/CronJob and create a podSpec */}} + {{- $_ := set $jobValues "enabled" true -}} + {{- $_ := set $jobValues "podSpec" dict -}} + + {{/* Add labels/annotations if any. */}} + {{- range $key := (list "labels" "annotations") -}} + {{- if hasKey $.Values $key -}} + {{- $_ := set $jobValues $key (get $.Values $key) -}} + {{- end -}} + {{- end -}} + + {{/* Add the .Values as the main container */}} + {{- $_ := set $jobValues.podSpec "containers" dict -}} + {{- $_ := set $jobValues.podSpec.containers "main" .Values -}} + {{- $_ := set $jobValues.podSpec.containers.main "enabled" "true" -}} + + {{/* Add additional containers if any */}} + {{- range $name, $container := .Values.additionalContainers -}} + {{- if $container.enabled -}} + {{- $_ := set $jobValues.podSpec.containers $name $container -}} + {{- end -}} + {{- end -}} + + {{/* Remove probes and lifecycle as they are not supported on Jobs/Crons */}} + {{- $_ := unset $jobValues.podSpec.containers.main "probes" -}} + {{- $_ := unset $jobValues.podSpec.containers.main "lifecycle" -}} + + {{- if not $jobValues.nameOverride -}} + {{- $_ := set $jobValues "nameOverride" (include "ix.v1.common.names.fullname" $) -}} + {{- end -}} + {{/* Add $jobValues to a "main" $job, now the spawner has the "usual" format */}} + {{- $_ := set $jobs "main" $jobValues -}} + + {{- else -}} + + {{/* If it's not "standalone" Use the jobs dict */}} + {{- $jobs = .Values.jobs -}} + + {{- end -}} + + {{- range $jobName, $job := $jobs -}} {{- if $job.enabled -}} {{- $jobValues := $job -}}