Skip to content

Commit

Permalink
feat: Allow propagating global labels and annotations to Pod metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bjw-s committed Dec 26, 2024
1 parent 695f41f commit 754743a
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions charts/library/common-test/tests/pod/metadata_labels_test.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions charts/library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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" -}}
Expand Down
8 changes: 8 additions & 0 deletions charts/library/common/templates/lib/pod/metadata/_labels.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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) -}}
Expand Down
10 changes: 7 additions & 3 deletions charts/library/common/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"global": {
"type": "object",
"properties": {
"annotations": {
"$ref": "schemas/definitions.json#/annotations"
},
"nameOverride": {
"type": [
"string",
Expand All @@ -21,8 +18,15 @@
"null"
]
},
"propagateGlobalMetadataToPods": {
"type": "boolean",
"default": false
},
"labels": {
"$ref": "schemas/definitions.json#/labels"
},
"annotations": {
"$ref": "schemas/definitions.json#/annotations"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions charts/library/common/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 754743a

Please sign in to comment.