diff --git a/charts/library/common-test/tests/pod/metadata_annotations_test.yaml b/charts/library/common-test/tests/pod/metadata_annotations_test.yaml new file mode 100644 index 00000000..92eb2ad6 --- /dev/null +++ b/charts/library/common-test/tests/pod/metadata_annotations_test.yaml @@ -0,0 +1,66 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: "pod metadata: annotations" +templates: + - common.yaml +values: + - ../_values/controllers_main_default_container.yaml +tests: + - it: default pod annotations + documentSelector: &DeploymentSelector + path: $[?(@.kind == "Deployment")].metadata.name + value: RELEASE-NAME + asserts: + - notExists: + path: spec.template.metadata.annotations + + - it: pod annotations from defaultPodOptions should be included + set: + defaultPodOptions: + annotations: + defaultPodOptionsAnnotation: test + documentSelector: *DeploymentSelector + asserts: + - isSubset: + path: spec.template.metadata.annotations + content: + defaultPodOptionsAnnotation: test + + - it: pod annotations from pod options should be included + set: + controllers: + main: + pod: + annotations: + podOptionsAnnotation: test + documentSelector: *DeploymentSelector + asserts: + - isSubset: + path: spec.template.metadata.annotations + content: + podOptionsAnnotation: test + + - it: pod annotations from global annotations should not be included by default + set: + global: + annotations: + globalAnnotation: test + documentSelector: *DeploymentSelector + asserts: + - isNotSubset: + path: spec.template.metadata.annotations + content: + globalAnnotation: test + + - it: pod annotations from global annotations should be included when requested + set: + global: + propagateGlobalMetadataToPods: true + annotations: + globalAnnotation: test + documentSelector: *DeploymentSelector + asserts: + - isSubset: + path: spec.template.metadata.annotations + content: + globalAnnotation: test diff --git a/charts/library/common-test/tests/pod/metadata_labels_test.yaml b/charts/library/common-test/tests/pod/metadata_labels_test.yaml new file mode 100644 index 00000000..4dd47aa5 --- /dev/null +++ b/charts/library/common-test/tests/pod/metadata_labels_test.yaml @@ -0,0 +1,70 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: "pod metadata: labels" +templates: + - common.yaml +values: + - ../_values/controllers_main_default_container.yaml +tests: + - it: default pod labels + documentSelector: &DeploymentSelector + path: $[?(@.kind == "Deployment")].metadata.name + value: RELEASE-NAME + asserts: + - equal: + path: spec.template.metadata.labels + value: + app.kubernetes.io/component: main + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: RELEASE-NAME + + - it: pod labels from defaultPodOptions should be included + set: + defaultPodOptions: + labels: + defaultPodOptionsLabel: test + documentSelector: *DeploymentSelector + asserts: + - isSubset: + path: spec.template.metadata.labels + content: + defaultPodOptionsLabel: test + + - it: pod labels from pod options should be included + set: + controllers: + main: + pod: + labels: + podOptionsLabel: test + documentSelector: *DeploymentSelector + asserts: + - isSubset: + path: spec.template.metadata.labels + content: + podOptionsLabel: test + + - it: pod labels from global labels should not be included by default + set: + global: + labels: + globalLabel: test + documentSelector: *DeploymentSelector + asserts: + - isNotSubset: + path: spec.template.metadata.labels + content: + globalLabel: test + + - it: pod labels from global labels should be included when requested + set: + global: + propagateGlobalMetadataToPods: true + labels: + globalLabel: test + documentSelector: *DeploymentSelector + asserts: + - isSubset: + path: spec.template.metadata.labels + content: + globalLabel: test diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 07e1024f..e0504b75 100644 --- a/charts/library/common/Chart.yaml +++ b/charts/library/common/Chart.yaml @@ -17,3 +17,6 @@ annotations: - kind: added description: |- Add hostUsers field to pod spec for k8s clusters >= 1.29 + - kind: added + description: |- + Allow propagating global labels and annotations to Pod metadata diff --git a/charts/library/common/templates/lib/pod/metadata/_annotations.tpl b/charts/library/common/templates/lib/pod/metadata/_annotations.tpl index 1f0a8def..d799115a 100644 --- a/charts/library/common/templates/lib/pod/metadata/_annotations.tpl +++ b/charts/library/common/templates/lib/pod/metadata/_annotations.tpl @@ -6,7 +6,17 @@ Returns the value for annotations {{- $controllerObject := .controllerObject -}} {{- /* Default annotations */ -}} - {{- $annotations := dict -}} + {{- $annotations := merge + (dict) + -}} + + {{- /* Include global annotations if specified */ -}} + {{- if $rootContext.Values.global.propagateGlobalMetadataToPods -}} + {{- $annotations = merge + (include "bjw-s.common.lib.metadata.globalAnnotations" $rootContext | fromYaml) + $annotations + -}} + {{- end -}} {{- /* Set to the default if it is set */ -}} {{- $defaultOption := get (default dict $rootContext.Values.defaultPodOptions) "annotations" -}} diff --git a/charts/library/common/templates/lib/pod/metadata/_labels.tpl b/charts/library/common/templates/lib/pod/metadata/_labels.tpl index 837ffa29..c6e43d39 100644 --- a/charts/library/common/templates/lib/pod/metadata/_labels.tpl +++ b/charts/library/common/templates/lib/pod/metadata/_labels.tpl @@ -10,6 +10,14 @@ Returns the value for labels (dict "app.kubernetes.io/component" $controllerObject.identifier) -}} + {{- /* Include global labels if specified */ -}} + {{- if $rootContext.Values.global.propagateGlobalMetadataToPods -}} + {{- $labels = merge + (include "bjw-s.common.lib.metadata.globalLabels" $rootContext | fromYaml) + $labels + -}} + {{- end -}} + {{- /* Fetch the Pod selectorLabels */ -}} {{- $selectorLabels := include "bjw-s.common.lib.metadata.selectorLabels" $rootContext | fromYaml -}} {{- if not (empty $selectorLabels) -}} diff --git a/charts/library/common/values.schema.json b/charts/library/common/values.schema.json index 9b314018..90097c8b 100644 --- a/charts/library/common/values.schema.json +++ b/charts/library/common/values.schema.json @@ -6,9 +6,6 @@ "global": { "type": "object", "properties": { - "annotations": { - "$ref": "schemas/definitions.json#/annotations" - }, "nameOverride": { "type": [ "string", @@ -21,8 +18,15 @@ "null" ] }, + "propagateGlobalMetadataToPods": { + "type": "boolean", + "default": false + }, "labels": { "$ref": "schemas/definitions.json#/labels" + }, + "annotations": { + "$ref": "schemas/definitions.json#/annotations" } } }, diff --git a/charts/library/common/values.yaml b/charts/library/common/values.yaml index 4d9bc625..0dcb9afb 100644 --- a/charts/library/common/values.yaml +++ b/charts/library/common/values.yaml @@ -4,6 +4,8 @@ global: nameOverride: # -- Set the entire name definition fullnameOverride: + # -- Propagate global metadata to Pod labels. + propagateGlobalMetadataToPods: false # -- Set additional global labels. Helm templates can be used. labels: {} # -- Set additional global annotations. Helm templates can be used.