Helm Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=External= * https://helm.sh/docs/chart_template_guide/variables/ =Internal= * Helm Templates =Overview=")
 
 
(9 intermediate revisions by the same user not shown)
Line 4: Line 4:
* [[Helm_Templates#Variables|Helm Templates]]
* [[Helm_Templates#Variables|Helm Templates]]
=Overview=
=Overview=
A variable is a named reference to another built-in object or a constant. It is assigned and referred using the $<name> syntax. := is used as assignment operator.
<syntaxhighlight lang='yaml'>
{{- $relname := .Release.Name -}}
{{- $color := "green" -}}
...
data:
  release: {{ $relname }}
  color: {{ $color }}
</syntaxhighlight>
Variables are less frequently used, as an alternative preferred construct is the [[Helm_Named_Templates|named template]].
=Variable Scope=
Variables are normally not "global". They are scoped to the block in which they are declared. However, there is one variable that is aways global: $. This variable will always point to the root context:
<syntaxhighlight lang='yaml'>
{{ $.Chart.AppVersion }}
</syntaxhighlight>
=Variables and Range Loops=
Variables are particularly useful in [[Helm Template range|range loops]]. They can be used on list-like objects to capture both the index and the value:
<syntaxhighlight lang='yaml'>
toppings: |-
  {{- range $index, $topping := .Values.pizzaToppings }}
    {{ $index }}: {{ $topping }}
  {{- end }}
</syntaxhighlight>
This will assign the integer index (0-based) to $index and the value to $topping.
For data structures that have both key and value, range can be used to get both and assign them to variables:
<syntaxhighlight lang='yaml'>
{{- range $key, $val := .Values.favorite }}
{{ $key }}: {{ $val | quote }}
{{- end }}
</syntaxhighlight>

Latest revision as of 01:06, 8 October 2020

External

Internal

Overview

A variable is a named reference to another built-in object or a constant. It is assigned and referred using the $<name> syntax. := is used as assignment operator.

{{- $relname := .Release.Name -}}
{{- $color := "green" -}}
...
data:
  release: {{ $relname }}
  color: {{ $color }}

Variables are less frequently used, as an alternative preferred construct is the named template.

Variable Scope

Variables are normally not "global". They are scoped to the block in which they are declared. However, there is one variable that is aways global: $. This variable will always point to the root context:

{{ $.Chart.AppVersion }}

Variables and Range Loops

Variables are particularly useful in range loops. They can be used on list-like objects to capture both the index and the value:

toppings: |-
  {{- range $index, $topping := .Values.pizzaToppings }}
    {{ $index }}: {{ $topping }}
  {{- end }}

This will assign the integer index (0-based) to $index and the value to $topping. For data structures that have both key and value, range can be used to get both and assign them to variables:

{{- range $key, $val := .Values.favorite }}
{{ $key }}: {{ $val | quote }}
{{- end }}