Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use OR operator in Helm yaml files

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?

like image 841
James Isaac Avatar asked Apr 12 '18 06:04

James Isaac


People also ask

Can we use and operator in Helm YAML files?

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

What does {{ }} mean in Helm?

The Helm template syntax is based on the Go programming language's text/template package. The braces {{ and }} are the opening and closing brackets to enter and exit template logic.

How do I use YAML in Helm?

You can use the helm native commands to achieve this. In helm, there is a command called helm template . Using the template command you can convert any helm chart to a YAML manifest. The resultant manifest file will have all the default values set in the helm values.

Are Helm charts YAML files?

What are Helm charts? Helm Charts are simply Kubernetes YAML manifests combined into a single package that can be advertised to your Kubernetes clusters.


2 Answers

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 }} 
like image 152
ykweyer Avatar answered Sep 26 '22 16:09

ykweyer


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.

like image 23
flix Avatar answered Sep 22 '22 16:09

flix