From a6d0cbfd4dac7ddecb81f47c0ee5a78008cbd15f Mon Sep 17 00:00:00 2001 From: d3adb5 Date: Tue, 11 Apr 2023 16:17:49 -0700 Subject: [PATCH] chore: apply conditional to version label partial tpl Check whether the output of the 'application.version' partial template is empty instead of the image tag when deciding whether to add the Kubernetes recommended application version label. The partial template was adjusted to avoid crashes when applying the regexReplace function. --- application/templates/_helpers.tpl | 7 ++++--- application/tests/deployment_test.yaml | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/application/templates/_helpers.tpl b/application/templates/_helpers.tpl index 48f1ef03..48f8166d 100644 --- a/application/templates/_helpers.tpl +++ b/application/templates/_helpers.tpl @@ -11,7 +11,8 @@ Define the name of the chart/application. Define the name of the chart/application. */}} {{- define "application.version" -}} -{{ regexReplaceAll "[^a-zA-Z0-9_\\.\\-]" .Values.deployment.image.tag "-" | trunc 63 | trimSuffix "-" -}} + {{- $version := default "" .Values.deployment.image.tag -}} + {{- regexReplaceAll "[^a-zA-Z0-9_\\.\\-]" $version "-" | trunc 63 | trimSuffix "-" -}} {{- end -}} {{/* @@ -39,8 +40,8 @@ Common labels */}} {{- define "application.labels" -}} helm.sh/chart: {{ include "application.chart" . }} -{{- if .Values.deployment.image.tag }} -app.kubernetes.io/version: {{ include "application.version" . | quote }} +{{- with include "application.version" . }} +app.kubernetes.io/version: {{ quote . }} {{- end }} app.kubernetes.io/managed-by: {{ .Release.Service }} app.kubernetes.io/part-of: {{ include "application.name" . }} diff --git a/application/tests/deployment_test.yaml b/application/tests/deployment_test.yaml index 1c368a35..c98cae84 100644 --- a/application/tests/deployment_test.yaml +++ b/application/tests/deployment_test.yaml @@ -50,6 +50,9 @@ tests: - equal: path: spec.template.spec.containers[0].image value: example-image:example-tag + - equal: + path: metadata.labels["app.kubernetes.io/version"] + value: example-tag - it: uses image digest when given set: @@ -60,6 +63,8 @@ tests: - equal: path: spec.template.spec.containers[0].image value: example-image@sha256:example-digest + - isNull: + path: metadata.labels["app.kubernetes.io/version"] - it: uses both image digest and tag when given both set: