From 2385bf5edc1493102bf577a8c8490bcb7a52ea8b Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Mon, 30 Jan 2023 12:17:29 +0200 Subject: [PATCH] job spec --- .../tests/configmap/metadata_test.yaml | 2 +- .../tests/daemonset/metadata_test.yaml | 2 +- .../tests/deployment/metadata_test.yaml | 2 +- .../tests/imagePullSecret/metadata_test.yaml | 2 +- .../common-test/tests/job/metadata_test.yaml | 56 +++++++++++++++++++ library/common-test/tests/job/spec_test.yaml | 52 +++++++++++++++++ .../tests/job/validation_test.yaml | 42 ++++++++++++++ .../tests/secret/metadata_test.yaml | 2 +- .../tests/statefulset/metadata_test.yaml | 2 +- .../1.0.0/templates/classes/_daemonset.tpl | 6 +- .../1.0.0/templates/classes/_deployment.tpl | 6 +- .../common/1.0.0/templates/classes/_job.tpl | 38 +++++++++++++ .../1.0.0/templates/classes/_statefulset.tpl | 6 +- .../templates/lib/controller/_jobSpec.tpl | 24 ++++++++ .../validation/_daemonsetValidation.tpl | 1 + .../validation/_deploymentValidation.tpl | 1 + .../controller/validation/_jobValidation.tpl | 33 +++++++++++ .../validation/_statefusetValidation.tpl | 1 + .../1.0.0/templates/spawner/_controller.tpl | 2 + library/common/1.0.0/values.yaml | 2 +- 20 files changed, 266 insertions(+), 16 deletions(-) create mode 100644 library/common-test/tests/job/metadata_test.yaml create mode 100644 library/common-test/tests/job/spec_test.yaml create mode 100644 library/common-test/tests/job/validation_test.yaml create mode 100644 library/common/1.0.0/templates/lib/controller/_jobSpec.tpl create mode 100644 library/common/1.0.0/templates/lib/controller/validation/_jobValidation.tpl diff --git a/library/common-test/tests/configmap/metadata_test.yaml b/library/common-test/tests/configmap/metadata_test.yaml index b56e511b72..6b9d0530c0 100644 --- a/library/common-test/tests/configmap/metadata_test.yaml +++ b/library/common-test/tests/configmap/metadata_test.yaml @@ -49,7 +49,7 @@ tests: helm-revision: 0 helm.sh/chart: common-test-1.0.0 app.kubernetes.io/managed-by: Helm - app.kubernetes.io/version: v9.9.9 + app.kubernetes.io/version: *appVer g_label1: global_label1 g_label2: global_label2 label1: label1 diff --git a/library/common-test/tests/daemonset/metadata_test.yaml b/library/common-test/tests/daemonset/metadata_test.yaml index f322b70b6b..074ae1fd7c 100644 --- a/library/common-test/tests/daemonset/metadata_test.yaml +++ b/library/common-test/tests/daemonset/metadata_test.yaml @@ -49,7 +49,7 @@ tests: helm-revision: 0 helm.sh/chart: common-test-1.0.0 app.kubernetes.io/managed-by: Helm - app.kubernetes.io/version: v9.9.9 + app.kubernetes.io/version: *appVer g_label1: global_label1 g_label2: global_label2 label1: label1 diff --git a/library/common-test/tests/deployment/metadata_test.yaml b/library/common-test/tests/deployment/metadata_test.yaml index 281bc15195..7e4a3832a1 100644 --- a/library/common-test/tests/deployment/metadata_test.yaml +++ b/library/common-test/tests/deployment/metadata_test.yaml @@ -49,7 +49,7 @@ tests: helm-revision: 0 helm.sh/chart: common-test-1.0.0 app.kubernetes.io/managed-by: Helm - app.kubernetes.io/version: v9.9.9 + app.kubernetes.io/version: *appVer g_label1: global_label1 g_label2: global_label2 label1: label1 diff --git a/library/common-test/tests/imagePullSecret/metadata_test.yaml b/library/common-test/tests/imagePullSecret/metadata_test.yaml index ba60cb7eb3..87acfe34f7 100644 --- a/library/common-test/tests/imagePullSecret/metadata_test.yaml +++ b/library/common-test/tests/imagePullSecret/metadata_test.yaml @@ -52,7 +52,7 @@ tests: helm-revision: 0 helm.sh/chart: common-test-1.0.0 app.kubernetes.io/managed-by: Helm - app.kubernetes.io/version: v9.9.9 + app.kubernetes.io/version: *appVer g_label1: global_label1 g_label2: global_label2 label1: label1 diff --git a/library/common-test/tests/job/metadata_test.yaml b/library/common-test/tests/job/metadata_test.yaml new file mode 100644 index 0000000000..1f84e19579 --- /dev/null +++ b/library/common-test/tests/job/metadata_test.yaml @@ -0,0 +1,56 @@ +suite: job metadata test +templates: + - common.yaml +chart: + appVersion: &appVer v9.9.9 +tests: + - it: should pass with job created with labels and annotations + set: + label1: label1 + label2: global_label2 + annotation1: annotation1 + annotation2: global_annotation2 + global: + labels: + g_label1: global_label1 + g_label2: "{{ .Values.label2 }}" + annotations: + g_annotation1: global_annotation1 + g_annotation2: "{{ .Values.annotation2 }}" + controllers: + controller-name: + enabled: true + primary: true + type: Job + labels: + label1: "{{ .Values.label1 }}" + label2: label2 + annotations: + annotation1: "{{ .Values.annotation1 }}" + annotation2: annotation2 + asserts: + - documentIndex: &jobDoc 0 + isKind: + of: Job + - documentIndex: *jobDoc + equal: + path: metadata.annotations + value: + annotation1: annotation1 + annotation2: annotation2 + g_annotation1: global_annotation1 + g_annotation2: global_annotation2 + - documentIndex: *jobDoc + equal: + path: metadata.labels + value: + app: common-test-1.0.0 + release: release-name + helm-revision: 0 + helm.sh/chart: common-test-1.0.0 + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/version: *appVer + g_label1: global_label1 + g_label2: global_label2 + label1: label1 + label2: label2 diff --git a/library/common-test/tests/job/spec_test.yaml b/library/common-test/tests/job/spec_test.yaml new file mode 100644 index 0000000000..0cec770944 --- /dev/null +++ b/library/common-test/tests/job/spec_test.yaml @@ -0,0 +1,52 @@ +suite: job spec test +templates: + - common.yaml +tests: + - it: should pass with controller enabled + set: + controllers: + controller-name: + enabled: true + primary: true + type: Job + asserts: + - documentIndex: &jobDoc 0 + isKind: + of: Job + - documentIndex: *jobDoc + isAPIVersion: + of: batch/v1 + - documentIndex: *jobDoc + isSubset: + path: spec + content: + backoffLimit: 5 + completionMode: NonIndexed + completions: + parallelism: 1 + ttlSecondsAfterFinished: 120 + + - it: should apply spec correctly + set: + controllers: + controller-name: + enabled: true + primary: true + type: Job + backoffLimit: 5 + completionMode: Indexed + completions: 5 + parallelism: 5 + ttlSecondsAfterFinished: 100 + activeDeadlineSeconds: 100 + asserts: + - documentIndex: *jobDoc + isSubset: + path: spec + content: + backoffLimit: 5 + completionMode: Indexed + completions: 5 + parallelism: 5 + ttlSecondsAfterFinished: 100 + activeDeadlineSeconds: 100 diff --git a/library/common-test/tests/job/validation_test.yaml b/library/common-test/tests/job/validation_test.yaml new file mode 100644 index 0000000000..2827cb27cd --- /dev/null +++ b/library/common-test/tests/job/validation_test.yaml @@ -0,0 +1,42 @@ +suite: job validation test +templates: + - common.yaml +tests: + - it: should fail with invalid completionMode + set: + controllers: + controller-name: + enabled: true + primary: true + type: Job + completionMode: not-a-mode + asserts: + - failedTemplate: + errorMessage: Job - Expected to be one of [Indexed, NonIndexed], but got [not-a-mode] + + - it: should fail with completionMode to Indexed and no completions + set: + controllers: + controller-name: + enabled: true + primary: true + type: Job + completionMode: Indexed + completions: + asserts: + - failedTemplate: + errorMessage: Job - Expected to be set when is set to [Indexed] + + - it: should fail with completionMode to Indexed and no parallelism + set: + controllers: + controller-name: + enabled: true + primary: true + type: Job + completionMode: Indexed + completions: 5 + parallelism: + asserts: + - failedTemplate: + errorMessage: Job - Expected to be set when is set to [Indexed] diff --git a/library/common-test/tests/secret/metadata_test.yaml b/library/common-test/tests/secret/metadata_test.yaml index 2e1ea03ef4..31a33e7904 100644 --- a/library/common-test/tests/secret/metadata_test.yaml +++ b/library/common-test/tests/secret/metadata_test.yaml @@ -49,7 +49,7 @@ tests: helm-revision: 0 helm.sh/chart: common-test-1.0.0 app.kubernetes.io/managed-by: Helm - app.kubernetes.io/version: v9.9.9 + app.kubernetes.io/version: *appVer g_label1: global_label1 g_label2: global_label2 label1: label1 diff --git a/library/common-test/tests/statefulset/metadata_test.yaml b/library/common-test/tests/statefulset/metadata_test.yaml index b6181e47f4..54b08aab7c 100644 --- a/library/common-test/tests/statefulset/metadata_test.yaml +++ b/library/common-test/tests/statefulset/metadata_test.yaml @@ -49,7 +49,7 @@ tests: helm-revision: 0 helm.sh/chart: common-test-1.0.0 app.kubernetes.io/managed-by: Helm - app.kubernetes.io/version: v9.9.9 + app.kubernetes.io/version: *appVer g_label1: global_label1 g_label2: global_label2 label1: label1 diff --git a/library/common/1.0.0/templates/classes/_daemonset.tpl b/library/common/1.0.0/templates/classes/_daemonset.tpl index 458a7a5167..777a792677 100644 --- a/library/common/1.0.0/templates/classes/_daemonset.tpl +++ b/library/common/1.0.0/templates/classes/_daemonset.tpl @@ -4,9 +4,9 @@ rootCtx: The root context of the template. It is used to access the global context. objectData: - name: The name of the configmap. - labels: The labels of the configmap. - annotations: The annotations of the configmap. + name: The name of the daemonset. + labels: The labels of the daemonset. + annotations: The annotations of the daemonset. replicas: The number of replicas. revisionHistoryLimit: The number of old ReplicaSets to retain to allow rollback. strategy: The deployment strategy. diff --git a/library/common/1.0.0/templates/classes/_deployment.tpl b/library/common/1.0.0/templates/classes/_deployment.tpl index ba4fefe904..bc3cb1c3f5 100644 --- a/library/common/1.0.0/templates/classes/_deployment.tpl +++ b/library/common/1.0.0/templates/classes/_deployment.tpl @@ -4,9 +4,9 @@ rootCtx: The root context of the template. It is used to access the global context. objectData: - name: The name of the configmap. - labels: The labels of the configmap. - annotations: The annotations of the configmap. + name: The name of the deployment. + labels: The labels of the deployment. + annotations: The annotations of the deployment. replicas: The number of replicas. revisionHistoryLimit: The number of old ReplicaSets to retain to allow rollback. strategy: The deployment strategy. diff --git a/library/common/1.0.0/templates/classes/_job.tpl b/library/common/1.0.0/templates/classes/_job.tpl index e69de29bb2..f24dab7d5e 100644 --- a/library/common/1.0.0/templates/classes/_job.tpl +++ b/library/common/1.0.0/templates/classes/_job.tpl @@ -0,0 +1,38 @@ +{{/* Job Class */}} +{{/* Call this template: +{{ include "ix.v1.common.class.job" (dict "objectData" $objectData "rootCtx" $) }} + +rootCtx: The root context of the template. It is used to access the global context. +objectData: + name: The name of the job. + labels: The labels of the job. + annotations: The annotations of the job. + replicas: The number of replicas. + revisionHistoryLimit: The number of old ReplicaSets to retain to allow rollback. + strategy: The job strategy. +*/}} + +{{- define "ix.v1.common.class.job" -}} + + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + {{- include "ix.v1.common.lib.controller.jobValidation" (dict "objectData" $objectData) }} +--- +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ $objectData.name }} + {{- $labels := (mustMerge ($objectData.labels | default dict) (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 4 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 4 }} + {{- end }} +spec: + {{- include "ix.v1.common.lib.controller.jobSpec" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 2 }} + +{{- end -}} diff --git a/library/common/1.0.0/templates/classes/_statefulset.tpl b/library/common/1.0.0/templates/classes/_statefulset.tpl index 925b4f3f8f..e2c7df36c5 100644 --- a/library/common/1.0.0/templates/classes/_statefulset.tpl +++ b/library/common/1.0.0/templates/classes/_statefulset.tpl @@ -4,9 +4,9 @@ rootCtx: The root context of the template. It is used to access the global context. objectData: - name: The name of the configmap. - labels: The labels of the configmap. - annotations: The annotations of the configmap. + name: The name of the statefulset. + labels: The labels of the statefulset. + annotations: The annotations of the statefulset. replicas: The number of replicas. revisionHistoryLimit: The number of old ReplicaSets to retain to allow rollback. strategy: The deployment strategy. diff --git a/library/common/1.0.0/templates/lib/controller/_jobSpec.tpl b/library/common/1.0.0/templates/lib/controller/_jobSpec.tpl new file mode 100644 index 0000000000..94c2780f5c --- /dev/null +++ b/library/common/1.0.0/templates/lib/controller/_jobSpec.tpl @@ -0,0 +1,24 @@ +{{/* Job Spec */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.controller.jobSpec" (dict "objectData" $objectData "rootCtx" $rootCtx) -}} +rootCtx: The root context of the template. It is used to access the global context. +objectData: + backoffLimit: The number of retries before marking this job failed. Defaults to 6. + completions: The desired number of successfully finished pods the job should be run with. Defaults to 1. + parallelism: The maximum desired number of pods the job should run at any given time. Defaults to 1. + activeDeadlineSeconds: Specifies the duration in seconds relative to the startTime that the job may be active before the system tries to terminate it; value must be positive integer. If set to nil, the job is never terminated due to timeout. + ttlSecondsAfterFinished: TTLSecondsAfterFinished limits the lifetime of a Job that has finished execution (either Complete or Failed). If this field is set, ttlSecondsAfterFinished after the Job finishes, it is eligible to be automatically deleted. When the Job is being deleted, its lifecycle guarantees (e.g. finalizers) will be honored. If this field is unset, the Job won't be automatically deleted. If this field is set to zero, the Job becomes eligible to be deleted immediately after it finishes. This field is alpha-level and is only honored by servers that enable the TTLAfterFinished feature. + completionMode: CompletionMode specifies how Pod completions are tracked. It can be `NonIndexed` (default) or `Indexed`. +*/}} +{{- define "ix.v1.common.lib.controller.jobSpec" -}} + {{- $objectData := .objectData -}} + {{- $rootCtx := .rootCtx -}} +backoffLimit: {{ $objectData.backoffLimit | default 5 }} +completionMode: {{ $objectData.completionMode | default "NonIndexed" }} +completions: {{ $objectData.completions }}{{/* Can be nil too */}} +parallelism: {{ $objectData.parallelism | default 1 }} +ttlSecondsAfterFinished: {{ $objectData.ttlSecondsAfterFinished | default 120 }} + {{- with $objectData.activeDeadlineSeconds }} +activeDeadlineSeconds: {{ . }} + {{- end -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/controller/validation/_daemonsetValidation.tpl b/library/common/1.0.0/templates/lib/controller/validation/_daemonsetValidation.tpl index a99492e4bf..06133206ac 100644 --- a/library/common/1.0.0/templates/lib/controller/validation/_daemonsetValidation.tpl +++ b/library/common/1.0.0/templates/lib/controller/validation/_daemonsetValidation.tpl @@ -27,3 +27,4 @@ objectData: {{- end -}} {{- end -}} +{{/* TODO: Extend validation for sub-values of rollingUpdate */}} diff --git a/library/common/1.0.0/templates/lib/controller/validation/_deploymentValidation.tpl b/library/common/1.0.0/templates/lib/controller/validation/_deploymentValidation.tpl index cf08b3ecb8..deaa0896d9 100644 --- a/library/common/1.0.0/templates/lib/controller/validation/_deploymentValidation.tpl +++ b/library/common/1.0.0/templates/lib/controller/validation/_deploymentValidation.tpl @@ -27,3 +27,4 @@ objectData: {{- end -}} {{- end -}} +{{/* TODO: Extend validation for sub-values of rollingUpdate */}} diff --git a/library/common/1.0.0/templates/lib/controller/validation/_jobValidation.tpl b/library/common/1.0.0/templates/lib/controller/validation/_jobValidation.tpl new file mode 100644 index 0000000000..b89a948953 --- /dev/null +++ b/library/common/1.0.0/templates/lib/controller/validation/_jobValidation.tpl @@ -0,0 +1,33 @@ +{{/* Deployment Validation */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.controller.jobValidation" (dict "objectData" $objectData) -}} +rootCtx: The root context of the template. It is used to access the global context. +objectData: + completionMode: The completionMode of the object. + completions: The completions of the object. + parallelism: The parallelism of the object. +*/}} +{{- define "ix.v1.common.lib.controller.jobValidation" -}} + {{- $objectData := .objectData -}} + + {{- if $objectData.completionMode -}} + {{- $completionMode := $objectData.completionMode -}} + + {{- if not (mustHas $completionMode (list "Indexed" "NonIndexed")) -}} + {{- fail (printf "Job - Expected to be one of [Indexed, NonIndexed], but got [%v]" $completionMode) -}} + {{- end -}} + + {{- if eq $completionMode "Indexed" -}} + {{- if not $objectData.completions -}} + {{- fail "Job - Expected to be set when is set to [Indexed]" -}} + {{- end -}} + + {{- if not $objectData.parallelism -}} + {{- fail "Job - Expected to be set when is set to [Indexed]" -}} + {{- end -}} + {{- end -}} + + {{- end -}} + +{{- end -}} +{{/* TODO: Extend validation for other values of job */}} diff --git a/library/common/1.0.0/templates/lib/controller/validation/_statefusetValidation.tpl b/library/common/1.0.0/templates/lib/controller/validation/_statefusetValidation.tpl index 8532c48573..ca5602d368 100644 --- a/library/common/1.0.0/templates/lib/controller/validation/_statefusetValidation.tpl +++ b/library/common/1.0.0/templates/lib/controller/validation/_statefusetValidation.tpl @@ -27,3 +27,4 @@ objectData: {{- end -}} {{- end -}} +{{/* TODO: Extend validation for sub-values of rollingUpdate */}} diff --git a/library/common/1.0.0/templates/spawner/_controller.tpl b/library/common/1.0.0/templates/spawner/_controller.tpl index 61c44be2c2..701d29bcf3 100644 --- a/library/common/1.0.0/templates/spawner/_controller.tpl +++ b/library/common/1.0.0/templates/spawner/_controller.tpl @@ -36,7 +36,9 @@ {{- else if eq $objectData.type "DaemonSet" -}} {{- include "ix.v1.common.class.daemonset" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "Job" -}} + {{- include "ix.v1.common.class.job" (dict "objectData" $objectData "rootCtx" $) -}} {{- else if eq $objectData.type "CronJob" -}} + {{- include "ix.v1.common.class.cronJob" (dict "objectData" $objectData "rootCtx" $) -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/values.yaml b/library/common/1.0.0/values.yaml index 77e8100317..18a41be8d8 100644 --- a/library/common/1.0.0/values.yaml +++ b/library/common/1.0.0/values.yaml @@ -375,7 +375,7 @@ controllers: # -- Primary controller primary: true # -- Controller type - type: Deployment + type: Job # -- Controller labels labels: {} # -- Controller annotations