Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If clause in helm chart

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"

like image 211
Riccardo Califano Avatar asked Jul 10 '20 13:07

Riccardo Califano


2 Answers

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:

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

like image 133
David Maze Avatar answered Sep 16 '22 18:09

David Maze


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.

like image 43
codeaprendiz Avatar answered Sep 19 '22 18:09

codeaprendiz