We have some template engine for the values that rendered boolean as strings, but then in other places we were passing boolean.
Because that, our chart had to support both a boolean true
|false
or strings "true"|"false"
.
But the helm templating (I guess golang) keeps converting a "true"
to boolean, but not "false"
, meanwhile the boolean operators consider any string as true
... Also, the eq
fails if you compare a bool and a string, so you must convert it.
The easiest is to set the value to empty string, that evaluates as false in the if
https://golang.org/pkg/text/template/#hdr-Actions, so:
rbac:
enabled: ""
After lots of tests, I came to this expression:
value: |
{{ .Values.rbac.enabled }}
toString: |
{{ toString .Values.rbac.enabled }}
evaluated: |
{{ and (not (eq (toString .Values.rbac.enabled) `"false"`)) .Values.rbac.enabled }}
{{- if and (not (eq (toString .Values.rbac.enabled) `"false"`)) .Values.rbac.enabled }}
isTrue: yeah
{{- end }}
So, this will check if the string version of the value is not the string "false"
, if it is not, then evaluate the value.
It works for all combinations:
for i in true '"true"' false '"false"'; do helm template charts/foo/ --show-only templates/rbac.yaml --set "rbac.enabled=$i"; done
---
# Source: foo/templates/rbac.yaml
value: |
true
toString: |
true
evaluated: |
true
isTrue: yeah
---
# Source: foo/templates/rbac.yaml
value: |
"true"
toString: |
"true"
evaluated: |
"true"
isTrue: yeah
---
# Source: foo/templates/rbac.yaml
value: |
false
toString: |
false
evaluated: |
false
---
# Source: foo/templates/rbac.yaml
value: |
"false"
toString: |
"false"
evaluated: |
false