helm template check for empty string

Viewed 25730

I have been asked to modify a Helm template to accommodate a few changes to check if a value is empty or not as in the code snippet below. I need to check $var.alias inside the printf in the code snippet and write custom logic to print a custom value. Any pointers around the same would be great.

{{- range $key, $value := .Values.testsConfig.keyVaults -}}
{{- range $secret, $var := $value.secrets -}}
{{- if nil $var.alias}}
{{- end -}}
{{ $args = append $args (printf "%s=/mnt/secrets/%s/%s" $var.alias $key $var.name | quote) }}
{{- end -}}
{{- end -}}
1 Answers

I decided to test what madniel wrote in his comment. Here are my files:

values.yaml

someString: abcdef
emptyString: ""
# nilString:

templates/test.yaml

{{ printf "someEmptyString=%q)" .Values.someString }}
{{ printf "emptyString=%q)" .Values.emptyString }}
{{ printf "nilString=%q)" .Values.nilString }}

{{- if .Values.someString }}
{{ printf "someString evaluates to true" }}
{{- end -}}

{{- if .Values.emptyString }}
{{ printf "emptyString evaluates to true" }}
{{- end -}}

{{- if .Values.nilString }}
{{ printf "nilString evaluates to true" }}
{{- end -}}

{{- if not .Values.emptyString }}
{{ printf "not emptyString evaluates to true" }}
{{- end -}}

{{- if not .Values.nilString }}
{{ printf "not nilString evaluates to true" }}
{{- end -}}

Helm template output:

āžœ  helm template . --debug
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: <REDACTED>

---
# Source: asd/templates/test.yaml
someEmptyString="abcdef")
emptyString="")
nilString=%!q(<nil>))
someString evaluates to true
not emptyString evaluates to true
not nilString evaluates to true

So yes, it should work if you use {{ if $var.alias }}

Related