I’m not getting something about Helm template variable resolution.
The following template helper function won’t work.
(Note: bar is a subchart of foo in this example.)
{{/*
Create labels for component given by dict
*/}}
{{- define "foo.makeLabels" -}}
{{- $new := dict "app.kubernetes.io/managed-by" "Helm" -}}
{{- $_ := set $new "meta.helm.sh/release-name" $.Release.Name -}}
{{- range $key, $val := . }}
{{- $_ := set $new (print "app.kubernetes.io/" $key) $val }}
{{- end -}}
{{- toYaml $new }}
{{- end -}}
… when I try to invoke it as such:
apiVersion: v1
kind: ServiceAccount
{{- $labels := (include "foo.makeLabels" (dict "name" .Values.foo.namespace "component" .Values.bar.name )) }}
{{- with .Values }}
metadata:
name: {{ print .foo.namespace "-" .bar.name }}
namespace: {{ .foo.namespace }}
labels:
{{- $labels | nindent 4 }}
{{- end -}}
The resulting error claims $Release is nil:
$ helm install foo . -n foo --create-namespace --dry-run=server
Error: INSTALLATION FAILED: template: foo-log-server/templates/bar/serviceaccount.yaml:4:16: executing "foo-log-server/templates/bar/serviceaccount.yaml" at <include "foo.makeLabels" (dict "name" .foo.namespace "component" .bar.name>: error calling include: template: foo-log-server/templates/_helpers.tpl:6:48: executing "foo.makeLabels" at <$.Release.Name>: nil pointer evaluating interface {}.Name
..but when I use $.Release.Name directly in the invoking template, no problem!
(I know, the helper isn’t in the best shape right now, and I’m sure you have good suggestions for the rest of it, but I was hoping to resolve this first.)
Shouldn’t the use of $ resolve to the global context, which should be available everywhere? Or have I managed to override something?