Helm Template range: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(12 intermediate revisions by the same user not shown)
Line 9: Line 9:
=Overview=
=Overview=


<tt>range</tt> provides a "for each" loop. The simples way to access the current iteration element is with {{ . }}. [[Helm_Templates#Template_Variables|Variables]] can also be used.
<tt>range</tt> provides a "for each" loop. It "ranges over".
 
The range operator sets the scope to ".", so the simples way to access the current iteration element is with {{ . }}
 
[[Helm_Variables#Variables_and_Range_Loops|Variables]] can also be used.
 
=Playground=
{{External|https://github.com/ovidiuf/playground/tree/master/helm/if-and-range}}


=Iterating over an In-Line List=
=Iterating over an In-Line List=
Line 18: Line 25:
   value: {{ . -}}
   value: {{ . -}}
{{ end }}
{{ end }}
</syntaxhighlight>
The result is:
<syntaxhighlight lang='yaml'>
env:
- name: COLOR_BLUE
  value: blue
- name: COLOR_RED
  value: red
- name: COLOR_GREEN
  value: green
</syntaxhighlight>
</syntaxhighlight>


=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=
It uses [[Helm_Templates#Template_Variables|variables]].
 
values.yaml:
<syntaxhighlight lang='yaml'>
shapes:
  FIRST_SHAPE: square
  SECOND_SHAPE: circle
  THIRD_SHAPE: triangle
</syntaxhighlight>
 
Template:
<syntaxhighlight lang='yaml'>
env:
{{- range $k, $v := .Values.shapes }}
- name: {{ $k }}
  value: {{ $v | upper -}}
{{ end }}
</syntaxhighlight>
 
Result:
<syntaxhighlight lang='yaml'>
env:
- name: FIRST_SHAPE
  value: SQUARE
- name: SECOND_SHAPE
  value: CIRCLE
- name: THIRD_SHAPE
  value: TRIANGLE
</syntaxhighlight>
 
Iterating over a map can be used to transfer the content of a map from values.yaml in a template:
{{Internal|Helm_Templates#Transfer_the_Content_of_a_Map_from_values.yaml_to_a_Template|Transfer the Content of a Map from values.yaml to a Template}}
 
=Iterating over a Complex Structure=
values.yaml:
<syntaxhighlight lang='yaml'>
volumesAndMounts:
  volumes:
    - name: mount-0
      hostPath:
        path: /tmp
    - name: mount-1
      hostPath:
        path: /tmp
  volumesMounts:
    - mountPath: /a
      name: mount-0
    - mountPath: /b
      name: mount-1
</syntaxhighlight>
 
Template:
<syntaxhighlight lang='yaml'>
kind: Pod
[...]
spec:
  containers:
    - name: ...
      [...]
      volumeMounts:
        {{- range .Values.volumesAndMounts.volumesMounts }}
        - {{- . | toYaml | nindent 10 -}}
        {{- end }}
  volumes:
    {{- range .Values.volumesAndMounts.volumes }}
    - {{ . | toYaml | nindent 6 -}}
    {{- end }}
</syntaxhighlight>

Latest revision as of 01:06, 8 October 2020

External

Internal

Overview

range provides a "for each" loop. It "ranges over".

The range operator sets the scope to ".", so the simples way to access the current iteration element is with {{ . }}

Variables can also be used.

Playground

https://github.com/ovidiuf/playground/tree/master/helm/if-and-range

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

It uses variables.

values.yaml:

shapes:
  FIRST_SHAPE: square
  SECOND_SHAPE: circle
  THIRD_SHAPE: triangle

Template:

env:
{{- range $k, $v := .Values.shapes }}
- name: {{ $k }}
  value: {{ $v | upper -}}
{{ end }}

Result:

env:
- name: FIRST_SHAPE
  value: SQUARE
- name: SECOND_SHAPE
  value: CIRCLE
- name: THIRD_SHAPE
  value: TRIANGLE

Iterating over a map can be used to transfer the content of a map from values.yaml in a template:

Transfer the Content of a Map from values.yaml to a Template

Iterating over a Complex Structure

values.yaml:

volumesAndMounts:
  volumes:
    - name: mount-0
      hostPath:
        path: /tmp
    - name: mount-1
      hostPath:
        path: /tmp
  volumesMounts:
    - mountPath: /a
      name: mount-0
    - mountPath: /b
      name: mount-1

Template:

kind: Pod
[...]
spec:
  containers:
    - name: ...
      [...]
      volumeMounts:
        {{- range .Values.volumesAndMounts.volumesMounts }}
        - {{- . | toYaml | nindent 10 -}}
        {{- end }}
  volumes:
    {{- range .Values.volumesAndMounts.volumes }}
    - {{ . | toYaml | nindent 6 -}}
    {{- end }}