Elio Lopez
Elio Lopez

Reputation: 31

Helm template .Files.Get function not workig properly inside a range

I'm trying to build a dynamic configmap manifest iterating through a values file list, using folder and file name. This is how my values.development.yaml looks like:

templates:
  folder: "foldername"
  files:
    - filename1
    - filename2

this is my original configmap, with the hardcoded file names :

apiVersion: v1
kind: ConfigMap
metadata:
  name: templates
binaryData:
  filename1: {{ .Files.Get "foldername/filename1.zip" | b64enc }}
  filename2: {{ .Files.Get "foldername/filename2.zip" | b64enc }}

this is how I've tried to start, but the .File.Get function donn't want to behave within the range, because I can use it normally outside. When I use it like so:

apiVersion: v1
kind: ConfigMap
metadata:
  name: templates
binaryData:
{{- range $.Values.templates.files }}
  {{ . }}: {{ .Files.Get "foldername/filename1.zip" | b64enc }}
{{- end }}

get this error:

helm template template  -f deploy/template/values.development.yaml  deploy/template --debug         
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: 
Error: template: template/templates/configmap.yaml:14:20: executing "template/templates/configmap.yaml" at <.Files.Get>: can't evaluate field Files in type interface {}
helm.go:81: [debug] template: template/templates/configmap.yaml:14:20: executing "template/templates/configmap.yaml" at <.Files.Get>: can't evaluate field Files in type interface {}

Needless to say that I don't know go language that much. Can you guy give me some hints on how to build that list properly? Thanks!

Upvotes: 3

Views: 6972

Answers (1)

David Maze
David Maze

Reputation: 159752

In the Go text/template language, . is a special variable that has several meanings. Of note, inside a range loop, . becomes the current item in the loop, and so .Files refers to the Files field in the current loop item rather than the top-level Helm object.

You can get around this by saving either . or .Files into a local variable outside the loop:

binaryData:
{{- $files := .Files }}
{{- range $.Values.templates.files }}
  {{ . }}: {{ $files.Get "foldername/filename1.zip" | b64enc }}
{{- end }}

Now inside the loop . is the string filename1, filename2, ... from the files array; but you've saved the top-level .Files object into the $files local variable so you can refer to that.

Upvotes: 4

Related Questions