Reputation: 1633
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 -}}
Upvotes: 17
Views: 88936
Reputation: 568
MyKey
key exists and has non-empty values, i.e. not one of 0
, false
, ""
, null
:{{- if .Values.MyKey }}
MyKey
key exists and its value is not null:{{ if not (quote .Values.MyKey | empty) }}
MyKey
key exists:{{- if hasKey .Values "MyKey" }}
e.g.
MyKey: 5
returns true for 1, true for 2, true for 3
MyKey: false
returns false for 1, true for 2, true for 3
MyKey:
returns false for 1, false for 2, true for 3
DifferentKey:
returns false for 1, false for 2, false for 3
The same result applies to map and list as well.
The answer is inspired by following StackOverflow answer: https://stackoverflow.com/a/74732808/9749861
Upvotes: 9
Reputation: 524
i opt out for this to check and empty string:
{{- if eq .Values.MyemptyString "" }}
you can also use the below code to check non empty string:
{{- if not (eq .Values.notEmptyString "") }}
Upvotes: 0
Reputation: 8152
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 }}
Upvotes: 44