Difference between revisions of "Helm Template range"

From NovaOrdis Knowledge Base
Jump to: navigation, search
(Iterating over an In-Line List)
(Iterating over a .Values List)
Line 32: Line 32:
  
 
=Iterating over a .Values List=
 
=Iterating over a .Values List=
 +
values.yaml:
 +
<syntaxhighlight lang='yaml'>
 +
colors:
 +
  - blue
 +
  - red
 +
  - green
 +
  - yellow
 +
</syntaxhighlight>
 +
 +
Template:
 +
<syntaxhighlight lang='yaml'>
 +
env:
 +
{{- range .Values.colors }}
 +
- name: {{ . | upper | printf "COLOR_%s" }}
 +
  value: {{ . -}}
 +
{{ end }}
 +
</syntaxhighlight>
 +
 +
Result:
 +
<syntaxhighlight lang='yaml'>
 +
env:
 +
- name: COLOR_BLUE
 +
  value: blue
 +
- name: COLOR_RED
 +
  value: red
 +
- name: COLOR_GREEN
 +
  value: green
 +
- name: COLOR_YELLOW
 +
  value: yellow
 +
</syntaxhighlight>
  
 
=Iterating over a Map=
 
=Iterating over a Map=
  
 
=Numeric Iteration Variable=
 
=Numeric Iteration Variable=

Revision as of 00:21, 10 October 2019

External

Internal

Overview

range provides a "for each" loop. The simples way to access the current iteration element is with {{ . }}. Variables can also be used.

Iterating over an In-Line List

env:
{{- range list "blue" "red" "green" }}
- name: {{ . | upper | printf "COLOR_%s" }}
  value: {{ . -}}
{{ end }}

The result is:

env:
- name: COLOR_BLUE
  value: blue
- name: COLOR_RED
  value: red
- name: COLOR_GREEN
  value: green

Iterating over a .Values List

values.yaml:

colors:
  - blue
  - red
  - green
  - yellow

Template:

env:
{{- range .Values.colors }}
- name: {{ . | upper | printf "COLOR_%s" }}
  value: {{ . -}}
{{ end }}

Result:

env:
- name: COLOR_BLUE
  value: blue
- name: COLOR_RED
  value: red
- name: COLOR_GREEN
  value: green
- name: COLOR_YELLOW
  value: yellow

Iterating over a Map

Numeric Iteration Variable