diff --git a/charts/openhab/templates/_helpers.tpl b/charts/openhab/templates/_helpers.tpl index e2f748c40..5040267d9 100644 --- a/charts/openhab/templates/_helpers.tpl +++ b/charts/openhab/templates/_helpers.tpl @@ -99,6 +99,19 @@ Validate ConfigMap sync configuration. {{- end }} {{- end }} +{{/* +Validate pod labels do not override immutable selector labels. +*/}} +{{- define "openhab.validatePodLabels" -}} +{{- $podLabels := .Values.podLabels | default dict -}} +{{- if hasKey $podLabels "app.kubernetes.io/name" -}} +{{- fail "podLabels must not override the selector label app.kubernetes.io/name" -}} +{{- end -}} +{{- if hasKey $podLabels "app.kubernetes.io/instance" -}} +{{- fail "podLabels must not override the selector label app.kubernetes.io/instance" -}} +{{- end -}} +{{- end }} + {{/* Resolve the admin secret name. */}} diff --git a/charts/openhab/templates/statefulset.yaml b/charts/openhab/templates/statefulset.yaml index f0602ca98..9249ee7df 100644 --- a/charts/openhab/templates/statefulset.yaml +++ b/charts/openhab/templates/statefulset.yaml @@ -2,6 +2,7 @@ {{- include "openhab.validateReplicaCount" . }} {{- include "openhab.validateAdmin" . }} {{- include "openhab.validateConfigMaps" . }} +{{- include "openhab.validatePodLabels" . }} {{- $hasSitemaps := and .Values.configMaps.sitemaps.enabled (gt (len (.Values.configMaps.sitemaps.files | default dict)) 0) }} {{- $hasThings := and .Values.configMaps.things.enabled (gt (len (.Values.configMaps.things.files | default dict)) 0) }} {{- $hasItems := and .Values.configMaps.items.enabled (gt (len (.Values.configMaps.items.files | default dict)) 0) }} @@ -35,7 +36,7 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} labels: - {{- include "openhab.labels" . | nindent 8 }} + {{- include "openhab.selectorLabels" . | nindent 8 }} {{- with .Values.podLabels }} {{- toYaml . | nindent 8 }} {{- end }} diff --git a/charts/openhab/tests/podlabels-selector-combined-values.yaml b/charts/openhab/tests/podlabels-selector-combined-values.yaml new file mode 100644 index 000000000..cb4a46aa4 --- /dev/null +++ b/charts/openhab/tests/podlabels-selector-combined-values.yaml @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: Apache-2.0 +podLabels: + app.kubernetes.io/name: custom + app.kubernetes.io/instance: custom diff --git a/charts/openhab/tests/podlabels-selector-instance-values.yaml b/charts/openhab/tests/podlabels-selector-instance-values.yaml new file mode 100644 index 000000000..5a3999b7e --- /dev/null +++ b/charts/openhab/tests/podlabels-selector-instance-values.yaml @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: Apache-2.0 +podLabels: + app.kubernetes.io/instance: custom diff --git a/charts/openhab/tests/podlabels-selector-name-values.yaml b/charts/openhab/tests/podlabels-selector-name-values.yaml new file mode 100644 index 000000000..44e895083 --- /dev/null +++ b/charts/openhab/tests/podlabels-selector-name-values.yaml @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: Apache-2.0 +podLabels: + app.kubernetes.io/name: custom diff --git a/charts/openhab/tests/validation_test.yaml b/charts/openhab/tests/validation_test.yaml index f11c5c7b3..61bfa1f04 100644 --- a/charts/openhab/tests/validation_test.yaml +++ b/charts/openhab/tests/validation_test.yaml @@ -41,3 +41,24 @@ tests: asserts: - isKind: of: StatefulSet + + - it: should fail when podLabels override selector name + values: + - podlabels-selector-name-values.yaml + asserts: + - failedTemplate: + errorMessage: "podLabels must not override the selector label app.kubernetes.io/name" + + - it: should fail when podLabels override selector name and instance + values: + - podlabels-selector-combined-values.yaml + asserts: + - failedTemplate: + errorMessage: "podLabels must not override the selector label app.kubernetes.io/name" + + - it: should fail when podLabels override selector instance + values: + - podlabels-selector-instance-values.yaml + asserts: + - failedTemplate: + errorMessage: "podLabels must not override the selector label app.kubernetes.io/instance"