Helm Template If/Else: Difference between revisions
Line 41: | Line 41: | ||
==nil pointer evaluating interface== | ==nil pointer evaluating interface== | ||
While <code>nil</code> (missing) values are evaluated to false, this only applies to the '''leaf''' of the YAML path. All intermediate path elements to the leaf must exist, and must NOT be empty, otherwise the template rendering engine will error out with: | |||
<syntaxhighlight lang='text'> | |||
Error: template: simplest/templates/configmap.yaml:6:16: executing "simplest/templates/configmap.yaml" at <.Values.appearance.color>: nil pointer evaluating interface {}.color | |||
</syntaxhighlight> | |||
=Expressions= | =Expressions= |
Revision as of 19:04, 17 September 2020
External
Internal
Overview
if/else can be used to create conditional blocks.
The if control structures evaluate pipelines, not just values (values are a special case of a pipeline):
{{ if PIPELINE }}
# Do something
{{ else if OTHER_PIPELINE }}
# Do something else
{{ else }}
# Default case
{{ end }}
A pipeline evaluates to false
if the result of the pipeline, or 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).
In any other case, the condition is evaluated to true.
Example:
{{ if .Values.debug }}
# Do something
{{ else }}
# Do something else
{{ end }}
nil pointer evaluating interface
While nil
(missing) values are evaluated to false, this only applies to the leaf of the YAML path. All intermediate path elements to the leaf must exist, and must NOT be empty, otherwise the template rendering engine will error out with:
Error: template: simplest/templates/configmap.yaml:6:16: executing "simplest/templates/configmap.yaml" at <.Values.appearance.color>: nil pointer evaluating interface {}.color
Expressions
if
expressions are built with functions: eq
, ne
, lt
, gt
, and
, or
, so the function name is provided first, followed by the arguments. Functions can be grouped with ( ).
Testing the Existence of Two Configuration Elements in the Same Expression
{{- if or .Values.myApp.config.local .Values.myApp.config.nfs }}
...
{{- end }}
Testing the Non-Existence of a Configuration Element
The following conditional evaluates to true and the embedded content is rendered if the configuration element does NOT exist:
{{- if not .Values.myApp.something }}
...
{{- end }}
Testing Equality and Inequality of Values
{{- if eq .Values.myApp.color "blue" }}
...
{{- end }}
{{- if ne .Values.myApp.color "blue" }}
...
{{- end }}
{{- if eq .Values.myApp.something .Values.myApp.somethingElse }}
...
{{- end }}
{{- if and (eq .Values.myApp.color "blue") (eq .Values.myApp.shape "square") }}
...
{{- end }}