helm to iterate a list of lists

862 Views Asked by At

Given the follow values.yaml

configurations:
  endpoints:
    - firstEndpoint
      - firstPath
      - secondPath
    - secondEndpoint
      - thirdPath
      - fourthPath

I need to generate different resources from those values in the following way:

- name: firstEndpoint
  paths:
    - firstPath
    - secondPath

- name: secondEndpoint
  paths:
    - thirdPath
    - fourthPath

I can do this if "endpoints" were to be a map, instead of a list/array, but in this case, I need "endpoints" to be a list of endpoints, and "paths" to be a list of paths for each endpoint.

How could this be achieved?

1

There are 1 best solutions below

0
z.x On BEST ANSWER

As David Maze said. Your proposed endpoints: isn't valid.

You may try this:

values.yaml

configurations:
  endpoints:
    - firstEndpoint:
      - firstPath
      - secondPath
    - secondEndpoint:
      - thirdPath
      - fourthPath

(Note the : after firstEndpoint and secondEndpoint)

template/cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  test: |-
    {{- range $_, $item := .Values.configurations.endpoints }}
    {{- range $k, $v := . }}
    - name: {{ $k }}
      path:
      {{- range $_, $path := $v }}
        - {{ $path }}
      {{- end }}
    {{- end }}
    {{- end }}

output

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  test: |-
    - name: firstEndpoint
      path:
        - firstPath
        - secondPath
    - name: secondEndpoint
      path:
        - thirdPath
        - fourthPath