Helm Template range

From NovaOrdis Knowledge Base
Revision as of 00:44, 10 October 2019 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

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