How to get the first name from the list

1/13/2021

I have a values file like:

secrets:
  file.tst |
    {
      content ....
    }

And I would like to get file.tst name in the yaml template. I already tried with:

{{ .Values.secrets.secrets |trimSuffix "-" }}

But it doesn't work.

-- Michael
kubernetes
kubernetes-helm

1 Answer

1/13/2021

With that values.yaml layout, .Values.secrets is itself a dictionary, with string keys and string values. Helm includes many functions that can operate on details of these. In particular, you can call keys to get the keys of a dictionary as a list, and first to get the first item out of a list.

<!-- language: lang-yaml -->
firstFilename: "{{ .Values.secrets | keys | first }}"

You can also use the built-in range function to iterate over either the map itself or the list of keys.

<!-- language: lang-yaml -->
hashes:
{{- $filename, $contents := range .Values.secrets }}
  {{ $filename }}: "{{ sha256sum $contents }}"
{{- end }}
-- David Maze
Source: StackOverflow