Using has in Helm Go Template

59 views Asked by At

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]?

1

There are 1 answers

0
Matthew On BEST ANSWER

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.