Helm range with 2 variables
Asked Answered
M

2

5

im trying to a loop with a range in helm but using 2 variables, what i have..

values.yaml

master:
  slave1: 
    - slave1value1
    - slave1value2
  slave2: 
    - slave2value1
    - slave2value2

My actual loop.

{{- range  .Values.master.slave1 }}
        name: http://slave1-{{ . }}
{{- end }}
{{- range  .Values.master.slave2 }}
        name: http://slave2-{{ . }}
{{- end }}

This is actually doing what i need, the output will be like this...

looping on .Values.master.slave1

name: http://slave1-slave1value1
name: http://slave1-slave1value2

looping on .Values.master.slave2

name: http://slave2-slave1value1
name: http://slave2-slave1value2

This is fully working for now, the question is, can i achieve the same result using just one loop block ? i tried this.

{{ alias := .Values.master }}
{{- range  $alias }}
        name: http://{{ . }}-{{ $alias.name }}
{{- end }}

But the output is not what I'm expecting, thanks in advance.

Material answered 27/5, 2021 at 20:59 Comment(0)
D
4

Almost...you need a nested loop to do this. The top-level data structure is a map, where the keys are the worker names and the values are the list of values. So you can iterate through the top-level map, then for each item iterate through the value list.

{{- $key, $values := range .Values.master -}}
{{- $value := range $values -}}
name: http://{{ $key }}-{{ $value }}
{{ end -}}
{{- end -}}

Note that we've assigned the values of range to locals to avoid some ambiguity around what exactly . means (inside each range loop it would be the iterator, for the currently-innermost loop).

Deify answered 27/5, 2021 at 21:32 Comment(3)
Hi @David Maze, i'm trying what you suggested but for some reason is not working, i'm getting this error. too many declarations in commandMaterial
That sounds like it could be an error in the context you're using this value. You might try running helm template over your chart to see what it prints out.Deify
The correct answer is the comment: https://mcmap.net/q/1976672/-helm-range-with-2-variables.Basia
M
4

Hi @DavidMaze i made it work changing the order of the "range" in the loop.

This doesn't work.

{{- $key, $values := range .Values.master -}}
{{- $value := range $values -}}
name: http://{{ $key }}-{{ $value }}
{{ end -}}
{{- end -}}

This work as expected :)

{{- range $key, $values := .Values.master -}}
{{- range $value := $values -}}
name: http://{{ $key }}-{{ $value }}
{{ end -}}
{{- end -}}
Material answered 28/5, 2021 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.