helm chart template: if value does not exist, defaults to true

Viewed 14044

I am trying to declare that a block of code in a helm template should be present if a variable is true OR does not exist (ie, the default of the var is true). The following works:

    {{- if or .Values.livenessProbe (not (hasKey .Values "livenessProbe")) }}
    ...
    {{- end }}

This seems rather complex, is there something simpler? I tried with default function in a few ways but they all result in ignoring the value (present or not, true or false, the block always gets rendered):

    {{- if (default true .Values.livenessProbe) }}
    ...
    {{- end }}
3 Answers

See https://helm.sh/docs/chart_template_guide/function_list/#default for an explanation of why default does not work as expected: boolean false is considered "empty", so when value is false the default returns the default value ie ignores the actual value!

I also found https://github.com/helm/helm/issues/3308, which shows that many people get tripped by this. Looking at other solutions in that issue, I feel mine (posted as part of the question) is actually simplest, rather unfortunate. Pattern is like this:

{{- if or .Values.myVar (not (hasKey .Values "myVar")) }}
...
{{- end }}

which basically says "render the block if the value is true, OR if the value is false because the key does not exist".

simpler solution I got was to do it like this

list nil true | has .Values.value

Use false value in double quotes so default in helm will consider it as string instead of null. Following setting worked for me.

valuekey: "false"
Related