100

Can I do something like this in Helm yamls :

{{- if eq .Values.isCar true }} OR {{- if eq .Values.isBus true }}
# do something
{{- end }}

I understand that we can do a single if check. But how would I check for multiple conditions? Are there some operators equivalent to OR and AND?

James Isaac
  • 2,587
  • 6
  • 20
  • 30

2 Answers2

211

As indicated in the Helm documentation on operators:

For templates, the operators (eq, ne, lt, gt, and, or and so on) are all implemented as functions. In pipelines, operations can be grouped with parentheses ((, and )).

It means you could use

{{- if or (eq .Values.isCar true) (eq .Values.isBus true) }}

Furthermore, as noted in the if/else structure:

A pipeline is evaluated as false if the value is:

  • a boolean false
  • a numeric zero
  • an empty string
  • a nil (empty or null)
  • an empty collection (map, slice, tuple, dict, array)

Under all other conditions, the condition is true.

If your properties (isCar and isBus) are booleans, you can then skip the equal check:

{{- if or .Values.isCar .Values.isBus }}
Achton
  • 23
  • 6
ykweyer
  • 2,234
  • 1
  • 8
  • 5
  • 1
    direct links have shifted to: https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#operators-are-functions https://helm.sh/docs/chart_template_guide/control_structures/#ifelse – Joshua Hansen Aug 21 '20 at 17:55
  • I could not find this in the official documentation. Would be a useful addition! – ja6a Dec 02 '20 at 16:23
  • 2
    What about conditions with more than 2 arguments? EX-{{-if and (a) (b) (c) }} EX2-{{-if eq (a) (b) (c) }} QUESTION: Is it checking equality for all members ? – Mario Mar 15 '22 at 14:04
  • for the first example you gave `if or (eq a b) (eq c d)` are the parens required or optional? seems like they are required IMO. but i'm no expert. – Trevor Boyd Smith Mar 01 '23 at 16:19
3

Note that or can also be used instead of default like this:

{{ or .Values.someSetting "default_value" }}

This would render to .Values.someSetting if it is set or to "default_value" otherwise.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
flix
  • 1,821
  • 18
  • 23
  • thanks it was helpful...! – Harsh Manvar Nov 18 '21 at 19:20
  • 2
    This feels like a terrible syntax, especially when use with booleans... just got my brain explode when seeing `{{ or .Values.someSetting "true" }}` (which to me reads as if always resulting to `true`....) in some chart ... use `default` (or maybe `coalesce`) for falling back to a default value ... semantics matter – Julien Jul 06 '22 at 14:33
  • 1
    @Julien I would not recommed this syntax to anyone. It is indeed hard to understand and read. I added this answer here for completeness. – flix Jul 25 '22 at 16:05