0

Helm newbie here. I am trying to loop through files and create a configmap with its contents. The file contents need to have variable defined inside the loop. Below is the configmap I am working with.

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "proj.name" . }}
  labels:
    app.kubernetes.io/name: {{ include "proj.name" . }}
    app.kubernetes.io/version: {{ include "proj.version" . }}
data:
  {{- range $path, $_ := .Files.Glob "myconfig/*.txt" -}}
  {{ $path | base | nindent 2 }}: |
  {{- tpl ($.Files.Get $path) $ | nindent 4 }}
  {{- end -}}

contents of myconfig/name.txt

i am able to access this -> {{ .Values.somekey }}
But not this -> {{ $path }}

I get error: undefined variable "$path" Any help would be greatly appreciated. Thank you.

David Maze
  • 130,717
  • 29
  • 175
  • 215
Vijayendar Gururaja
  • 752
  • 1
  • 9
  • 16

1 Answers1

2

The $path variable is a local variable. It's not accessible from other template functions or tpl expansions. The special "root context" variable $ doesn't include local variables either.

What you could do in this case is define your own context for the tpl function:

{{- $context := dict "somekey" .Values.somekey "path" $path }}
{{- tpl ($.Files.Get $path) $context | nindent 4 }}

Then in the file you'd refer to the keys provided in that dict (but not other things from outside that explicit context)

i am able to access this -> {{ .somekey }}
and also this -> {{ .path }}
but i wouldn't be able to reference .Values.anything
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I am getting error "BasePath is not a value" when i try this. I think this error could be due to the placement of tpl in range. I have not been get it working so far. But may be i am getting closer. Thank you! – Vijayendar Gururaja Oct 16 '21 at 06:24