Skip to content

Commit

Permalink
chore: apply conditional to version label partial tpl
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
d3adb5 committed Apr 11, 2023
1 parent 7653522 commit a6d0cbf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions application/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}

{{/*
Expand Down Expand Up @@ -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" . }}
Expand Down
5 changes: 5 additions & 0 deletions application/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit a6d0cbf

Please sign in to comment.