Access parent range values in nested range in Helm

31 views Asked by At

I have a helm chart where I try to generate a list of "targets", where each target has a different recipient but common config. This essentially means I have nested ranges, where the inner range needs to access values in the outer range. Unfortunately I've not been able to correctly pass values from the outer range to the inner range.

let's say I have the following value structure:

emailReceivers:
- name: 'receiver 1'
  targets:
    - '[email protected]'
    - '[email protected]'
  text: exampletext 1
  sendResolved: true
- name: 'receiver 2'
  targets:
    - '[email protected]'
    - '[email protected]'
  text: exampletext 2
  sendResolved: false

and the following snippet from my template:

receivers:
{{- if .Values.emailReceivers }}
{{- with .Values.emailReceivers }}
{{- range . }}
- name: {{ .name }}
  emailConfigs:
  {{- range .targets }}
  - to: {{ . }}
    text: <needs to come from outer range>
    sendResolved: <needs to come from outer range>
  {{- end }}
{{- end }}
{{- end }}

I want to reach a state where I get the following output:

receivers:
- name: 'receiver 1'
  emailConfigs:
  - to: '[email protected]'
    text: 'exampletext 1'
    sendResolved: true
  - to: '[email protected]'
    text: 'exampletext 1'
    sendResolved: true
- name: 'receiver 2'
  emailConfigs:
  - to: '[email protected]'
    text: 'exampletext 2'
    sendResolved: false
  - to: '[email protected]'
    text: 'exampletext 2'
    sendResolved: false

I can get the structure generated, but I can't figure out how to access the text and sendResolved value from the nested range or how to pass it into the range. I feel it's something trivial but so far no luck.. How can I achieve this? Thanks!

1

There are 1 answers

1
David Maze On BEST ANSWER

You can assign the current range item to a non-. variable. The Go text/template language has specific syntax for this

{{- range $receiver := . }}
- name: {{ .name }}
  emailConfigs:
  {{- range .targets }}
  - to: {{ . }}
    text: {{ $receiver.text }}
    sendResolved: {{ $receiver.sendResolved }}
  {{- end }}
{{- end }}

So note the assignment range $name := expression, and inside the nested loop, the references to the outer $receiver variable. The current range item is still assigned to . as well, so you could use either .name or $receiver.name when you're not in the nested loop.