Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 }}
like image 572
Oliver Avatar asked Feb 04 '26 20:02

Oliver


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

like image 130
Oliver Avatar answered Feb 06 '26 13:02

Oliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!