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

4

13

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 }}
Connacht answered 18/2, 2021 at 17:37 Comment(0)
C
15

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".

Connacht answered 18/2, 2021 at 17:52 Comment(0)
Z
3

simpler solution I got was to do it like this

list nil true | has .Values.value
Zoster answered 7/2, 2022 at 10:35 Comment(0)
E
3

Got another clean solution using ne (not-equal)

valuekey: {{ ne .Values.value false }}

value true false null or empty not spesified
valuekey true false true true
Emotionality answered 5/4, 2023 at 22:11 Comment(0)
A
0

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"
Adenosine answered 13/9, 2021 at 11:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.