If clause in helm chart

7/10/2020

How can I check if a variable is a boolean value "true" in helm chart? I have in Values.yaml a parameter set as:

myParameter: true

I don't understand well difference between:

{{- if .Values.service.myParameter }}

and

{{- if eq .Values.service.myParameter "true" }}

I want that flow goes in if clause if the parameter is set as boolean "true"

-- Riccardo Califano
if-statement
kubernetes
kubernetes-helm

3 Answers

7/10/2020

The documentation for the Go text/template package explains what the if statement considers as "true":

The empty "false" values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.

YAML values are typed and the usual rules are that bare true and false are booleans:

<!-- language: lang-yaml -->
boolean: true
string: other text, but not "true", "false", or "null"
also a string: 'true'

In your examples:

{{- if .Values.service.myParameter }}...{{ end }}

will evaluate to true whenever myParameter exists (isn't Go nil) and isn't zero, literal false, or an empty string.

{{- if eq .Values.service.myParameter "true" }}...{{ end }}

will evaluate to true whenever myParameter is exactly the string "true", but not a boolean true. (I think you will get a type error from the template engine if it's a boolean, in fact.)

-- David Maze
Source: StackOverflow

7/10/2020

You can use the following snipped for checking the boolean value

      {{if (default .Values.seLinux true)}}
      securityContext:
        seLinuxOptions:
          user: system_u
      {{ end }}

Then the Values file will have following snippet

seLinux: true

Please let me know if this helps.

-- codeaprendiz
Source: StackOverflow

1/15/2021

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
-- Keymon
Source: StackOverflow