helm: Cannot access $ in template

Output of helm version: v3.12.0

Output of kubectl version: none

Cloud Provider/Platform (AKS, GKE, Minikube etc.): mac

mininal reproducable example:

./templates/foo/configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: qwe
{{- include "hello.configmap" .Values.qq }}

./templates/_helper.tpl

{{- define "hello.configmap" -}}
data:
  foo: |
    {{ .bar }}
    {{$.Values.foo}}
{{- end }}

values.yaml

foo: "adasds"
qq:
  bar: "qqqq"

helm template .

outputs:

Error: template: example/templates/foo/configmap.yaml:6:4: executing "example/templates/foo/configmap.yaml" at <include "hello.configmap" .Values.qq>: error calling include: template: example/templates/_helpers.tpl:8:7: executing "hello.configmap" at <$.Values.foo>: nil pointer evaluating interface {}.foo

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 22 (11 by maintainers)

Most upvoted comments

Not a bug.

The documentation briefly mentions:

Note that we pass . at the end of the template call. We could just as easily pass .Values or .Values.favorite or whatever scope we want. But what we want is the top-level scope. (Bold italic added by me)

https://helm.sh/docs/chart_template_guide/named_templates/#setting-the-scope-of-a-template

Often I build my own dicts to pass to named templates to have the data available:

{{- include "mytemplate" (dict "Values" .Values "var1" $var1) }}

@PavanButke this is not a bug. thanks.

Honestly you’re being pedantic and veering off on some academic school lesson I can’t say anyone is massively interested in.

It’s very clear (now) how $ works. Again, this is a Helm community, not Go developer community; although I appreciate the $$ Go-developer effort may seem like/be more effort than it’s worth the possible Helm-templater UX/value

Right. That quote comes from https://helm.sh/docs/chart_template_guide/variables/ which is about template variables. Named templates are special in that their root context is whatever you pass to them in your include statement.

@caibirdme the key issue is $ in define. you can test it locally.

@caibirdme you should know the scope of $ in define.

For context:

When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.

https://pkg.go.dev/text/template#hdr-Variables