helm template check for empty string
Asked Answered
H

3

15

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 -}}
Hardecanute answered 26/5, 2021 at 14:24 Comment(1)
I believe golang templating does not naturally distinguish between nil and "", so {{ if $var.alias }} should cover both cases; is that not your experience?Epi
P
42

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 }}

Pammy answered 27/5, 2021 at 8:48 Comment(0)
H
6
  1. Check if MyKey key exists and has non-empty values, i.e. not one of 0, false, "", null:
{{- if .Values.MyKey }}
  1. Check if MyKey key exists and its value is not null:
{{ if not (quote .Values.emptyString | empty)  }}
  1. Check if 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://mcmap.net/q/821800/-helm-template-check-if-boolean-value-is-actually-empty-not-false

Hijoung answered 16/11, 2023 at 17:49 Comment(0)
D
0

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 "") }}
Dissension answered 2/9, 2023 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.