Can we use OR operator in Helm yaml files

Kubernetes Helm

Kubernetes Helm Problem Overview


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?

Kubernetes Helm Solutions


Solution 1 - Kubernetes Helm

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 }}

Solution 2 - Kubernetes Helm

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

{{ or .Values.someSetting "dafault_value" }}

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames IsaacView Question on Stackoverflow
Solution 1 - Kubernetes HelmykweyerView Answer on Stackoverflow
Solution 2 - Kubernetes HelmflixView Answer on Stackoverflow