From 973a903817ddf0af1bec1d78fd464fdae93f3548 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Mon, 30 Jan 2023 15:54:13 +0200 Subject: [PATCH] job and cron pod metadata --- .../tests/cronjob/metadata_test.yaml | 65 +++++++++++++++++++ .../common-test/tests/cronjob/spec_test.yaml | 2 + .../tests/cronjob/validation_test.yaml | 3 + .../common-test/tests/job/metadata_test.yaml | 63 ++++++++++++++++++ library/common-test/tests/job/spec_test.yaml | 2 + .../tests/job/validation_test.yaml | 3 + .../1.0.0/templates/classes/_cronjob.tpl | 20 +++++- .../common/1.0.0/templates/classes/_job.tpl | 20 +++++- 8 files changed, 176 insertions(+), 2 deletions(-) diff --git a/library/common-test/tests/cronjob/metadata_test.yaml b/library/common-test/tests/cronjob/metadata_test.yaml index bf8cf89b15..0437237a2a 100644 --- a/library/common-test/tests/cronjob/metadata_test.yaml +++ b/library/common-test/tests/cronjob/metadata_test.yaml @@ -29,6 +29,13 @@ tests: annotations: annotation1: "{{ .Values.annotation1 }}" annotation2: annotation2 + podSpec: + labels: + pod-label1: pod-label1 + pod-label2: "{{ .Values.label2 }}" + annotations: + pod-annotation1: pod-annotation1 + pod-annotation2: "{{ .Values.annotation2 }}" asserts: - documentIndex: &cronJobDoc 0 isKind: @@ -55,3 +62,61 @@ tests: g_label2: global_label2 label1: label1 label2: label2 + - documentIndex: *cronJobDoc + equal: + path: spec.jobTemplate.spec.template.metadata.labels + value: + pod.name: release-name-common-test + app: common-test-1.0.0 + release: release-name + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: common-test + app.kubernetes.io/version: v9.9.9 + helm-revision: 0 + helm.sh/chart: common-test-1.0.0 + g_label1: global_label1 + g_label2: global_label2 + pod-label1: pod-label1 + pod-label2: global_label2 + - documentIndex: *cronJobDoc + matchRegex: + path: spec.jobTemplate.spec.template.metadata.annotations.rollme + pattern: '^[0-9a-zA-Z]{5}$' + + - it: should pass with 2 cronjob created with correct selector labels + set: + controllers: + controller-name: + enabled: true + primary: true + type: CronJob + schedule: "*/1 * * * *" + podSpec: {} + other-controller-name: + enabled: true + primary: false + type: CronJob + schedule: "*/1 * * * *" + podSpec: {} + asserts: + - documentIndex: *cronJobDoc + isKind: + of: CronJob + - documentIndex: *cronJobDoc + isSubset: + path: spec.jobTemplate.spec.template.metadata.labels + content: + pod.name: release-name-common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test + - documentIndex: &otherCronJobDoc 1 + isKind: + of: CronJob + - documentIndex: *otherCronJobDoc + isSubset: + path: spec.jobTemplate.spec.template.metadata.labels + content: + pod.name: release-name-common-test-other-controller-name + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test diff --git a/library/common-test/tests/cronjob/spec_test.yaml b/library/common-test/tests/cronjob/spec_test.yaml index f6e4a539d9..0314da8fdf 100644 --- a/library/common-test/tests/cronjob/spec_test.yaml +++ b/library/common-test/tests/cronjob/spec_test.yaml @@ -10,6 +10,7 @@ tests: primary: true type: CronJob schedule: "* * * * *" + podSpec: {} asserts: - documentIndex: &cronJobDoc 0 isKind: @@ -58,6 +59,7 @@ tests: parallelism: 5 ttlSecondsAfterFinished: 100 activeDeadlineSeconds: 100 + podSpec: {} asserts: - documentIndex: *cronJobDoc isSubset: diff --git a/library/common-test/tests/cronjob/validation_test.yaml b/library/common-test/tests/cronjob/validation_test.yaml index 4e6e45630d..13f686d191 100644 --- a/library/common-test/tests/cronjob/validation_test.yaml +++ b/library/common-test/tests/cronjob/validation_test.yaml @@ -10,6 +10,7 @@ tests: primary: true type: CronJob concurrencyPolicy: not-a-policy + podSpec: {} asserts: - failedTemplate: errorMessage: CronJob - Expected to be one of [Allow, Forbid, Replace], but got [not-a-policy] @@ -22,6 +23,7 @@ tests: primary: true type: CronJob schedule: + podSpec: {} asserts: - failedTemplate: errorMessage: CronJob - Expected non-empty @@ -35,6 +37,7 @@ tests: type: CronJob schedule: "* * * * *" completionMode: not-a-mode + podSpec: {} asserts: - failedTemplate: errorMessage: Job - Expected to be one of [Indexed, NonIndexed], but got [not-a-mode] diff --git a/library/common-test/tests/job/metadata_test.yaml b/library/common-test/tests/job/metadata_test.yaml index 1f84e19579..40daad116e 100644 --- a/library/common-test/tests/job/metadata_test.yaml +++ b/library/common-test/tests/job/metadata_test.yaml @@ -28,6 +28,13 @@ tests: annotations: annotation1: "{{ .Values.annotation1 }}" annotation2: annotation2 + podSpec: + labels: + pod-label1: pod-label1 + pod-label2: "{{ .Values.label2 }}" + annotations: + pod-annotation1: pod-annotation1 + pod-annotation2: "{{ .Values.annotation2 }}" asserts: - documentIndex: &jobDoc 0 isKind: @@ -54,3 +61,59 @@ tests: g_label2: global_label2 label1: label1 label2: label2 + - documentIndex: *jobDoc + equal: + path: spec.template.metadata.labels + value: + pod.name: release-name-common-test + app: common-test-1.0.0 + release: release-name + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: common-test + app.kubernetes.io/version: v9.9.9 + helm-revision: 0 + helm.sh/chart: common-test-1.0.0 + g_label1: global_label1 + g_label2: global_label2 + pod-label1: pod-label1 + pod-label2: global_label2 + - documentIndex: *jobDoc + matchRegex: + path: spec.template.metadata.annotations.rollme + pattern: '^[0-9a-zA-Z]{5}$' + + - it: should pass with 2 job created with correct selector labels + set: + controllers: + controller-name: + enabled: true + primary: true + type: Job + podSpec: {} + other-controller-name: + enabled: true + primary: false + type: Job + podSpec: {} + asserts: + - documentIndex: *jobDoc + isKind: + of: Job + - documentIndex: *jobDoc + isSubset: + path: spec.template.metadata.labels + content: + pod.name: release-name-common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test + - documentIndex: &otherJobDoc 1 + isKind: + of: Job + - documentIndex: *otherJobDoc + isSubset: + path: spec.template.metadata.labels + content: + pod.name: release-name-common-test-other-controller-name + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test diff --git a/library/common-test/tests/job/spec_test.yaml b/library/common-test/tests/job/spec_test.yaml index 0cec770944..52c6272498 100644 --- a/library/common-test/tests/job/spec_test.yaml +++ b/library/common-test/tests/job/spec_test.yaml @@ -9,6 +9,7 @@ tests: enabled: true primary: true type: Job + podSpec: {} asserts: - documentIndex: &jobDoc 0 isKind: @@ -39,6 +40,7 @@ tests: parallelism: 5 ttlSecondsAfterFinished: 100 activeDeadlineSeconds: 100 + podSpec: {} asserts: - documentIndex: *jobDoc isSubset: diff --git a/library/common-test/tests/job/validation_test.yaml b/library/common-test/tests/job/validation_test.yaml index 2827cb27cd..cc0ff197df 100644 --- a/library/common-test/tests/job/validation_test.yaml +++ b/library/common-test/tests/job/validation_test.yaml @@ -10,6 +10,7 @@ tests: primary: true type: Job completionMode: not-a-mode + podSpec: {} asserts: - failedTemplate: errorMessage: Job - Expected to be one of [Indexed, NonIndexed], but got [not-a-mode] @@ -23,6 +24,7 @@ tests: type: Job completionMode: Indexed completions: + podSpec: {} asserts: - failedTemplate: errorMessage: Job - Expected to be set when is set to [Indexed] @@ -37,6 +39,7 @@ tests: completionMode: Indexed completions: 5 parallelism: + podSpec: {} asserts: - failedTemplate: errorMessage: Job - Expected to be set when is set to [Indexed] diff --git a/library/common/1.0.0/templates/classes/_cronjob.tpl b/library/common/1.0.0/templates/classes/_cronjob.tpl index 0cbce5a2f2..0b1c719d85 100644 --- a/library/common/1.0.0/templates/classes/_cronjob.tpl +++ b/library/common/1.0.0/templates/classes/_cronjob.tpl @@ -28,5 +28,23 @@ metadata: {{- end }} spec: {{- include "ix.v1.common.lib.controller.cronjobSpec" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 2 }} - + template: + metadata: + {{- $labels := (mustMerge ($objectData.podSpec.labels | default dict) + (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml) + (include "ix.v1.common.lib.metadata.podLabels" $rootCtx | fromYaml) + (include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "podName" $objectData.name) | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 12 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.podSpec.annotations | default dict) + (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml) + (include "ix.v1.common.lib.metadata.podAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 12 }} + {{- end }} + spec: + {{/* pod */}} {{- end -}} diff --git a/library/common/1.0.0/templates/classes/_job.tpl b/library/common/1.0.0/templates/classes/_job.tpl index e845cec101..015afd9cce 100644 --- a/library/common/1.0.0/templates/classes/_job.tpl +++ b/library/common/1.0.0/templates/classes/_job.tpl @@ -28,5 +28,23 @@ metadata: {{- end }} spec: {{- include "ix.v1.common.lib.controller.jobSpec" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 2 }} - + template: + metadata: + {{- $labels := (mustMerge ($objectData.podSpec.labels | default dict) + (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml) + (include "ix.v1.common.lib.metadata.podLabels" $rootCtx | fromYaml) + (include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "podName" $objectData.name) | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 8 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.podSpec.annotations | default dict) + (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml) + (include "ix.v1.common.lib.metadata.podAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 8 }} + {{- end }} + spec: + {{/* pod */}} {{- end -}}