kubernetes-helmgo-templatessprig-template-functions

Using has in Helm Go Template


values.yaml

{{if (has 1 .Values.xs)}}
a: 42
{{ else }}
b: 1
c: 2
{{ end }}

template.yaml

xs:
  - 1
  - 2
  - 3

I get an output of

b: 1
c: 2

How can I fix it so that I get an output of a: 42 since 1 is in [1,2,3]?


Solution

  • This is due to the types not matching.

    {{ kindOf (first .Values.xs) }} returns float64, while {{ kindOf (1) }} returns int.

    An alternative is use float64, by either writing 1.0, or you can cast it:

    {{ if has (float64 1) .Values.xs }}
    

    If converting to a float64 isn't your idea of good, you can instead use strings on both the definition of the list and in your comparison.