9

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 }}
Oliver
  • 27,510
  • 9
  • 72
  • 103

4 Answers4

10

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

Oliver
  • 27,510
  • 9
  • 72
  • 103
3

simpler solution I got was to do it like this

list nil true | has .Values.value
Karl Casas
  • 805
  • 9
  • 9
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"
Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35
garry
  • 44
  • 5
0

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
WesAtWork
  • 65
  • 7