简体   繁体   中英

how to use variables defined in helm templates inside files to be used in configmap

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM