Helm can't iterate over range from set-file

8/26/2019

I want to loop over a secret file I'm generating in my ci pipeline.

helm template \
  ...
  --set-file secretmap="secretmap.yaml" \
  ...

The secretmap.yaml is generated and contains the following:

SEC_1: 111
SEC_2: 222
...

The secret.yaml looks like this:

kind: Secret
...
data:
{{- range $key, $val := .Values.secretmap }}
  {{ $key }}: {{ $val | b64enc | quote }}
{{- end }}
...

And the error I get is:

Error: render error in ".../secret.yaml": template: .../secret.yaml:4:31: ... range can't iterate over SEC_1: 111
SEC_1: 222
...

The same configuration does work with configmap though,
where I set the configmap from -f chart/values/common.yaml

-- natdev
kubernetes
kubernetes-helm

1 Answer

8/26/2019

--set-file sets the contents of the variable to the text contents of the file; it doesn’t try to interpret it at all. (Somewhat described in this section of the Helm docs; note the example there is a JavaScript script being embedded in a ConfigMap.) That means you need to tell Helm to parse the file. Helm includes a minimally-documented fromYaml function that can do this.

When you iterate through the contents of the value, try explicitly parsing it first:

{{- range $key, $val := fromYaml .Values.secretmap }}
...
{{ end }}
-- David Maze
Source: StackOverflow