Helm range with condition to generate list of keys

1/27/2021

Given the following values.yaml

elements:
  first:
    enabled: true
    url: first.url
  second:
    enabled: flase
    url: second.url
  third:
    enabled: true
    url: third.url

What would be a good way to obtain the following result:

list_of_elements=first,third

Where the resulting list needs to contain only the elements that have been enabled. The list needs to be a single line of comma separated items.

-- Sebastián Greco
kubernetes
kubernetes-helm
yaml

1 Answer

1/27/2021

A little bit lengthy but does its job:

{{ $result := list }}
{{ range $k, $v := .Values.elements }}
{{ if eq (toString $v.enabled) "true" }}
{{ $result = append $result $k }}
{{ end }}
{{ end }}
list_of_elements: {{ join "," $result }}
-- Vasili Angapov
Source: StackOverflow