Search code examples
templatesgokuberneteskubernetes-helmgo-templates

Helm template variables are nil


I'm trying to use a template to pull values from values.yaml, concatenate them into a CSV list. Here's an example of my solution.

values.yaml:

testValue1: "string1"
testValue2: "String2"
credentials: 
- c1: "string3"
- c2: "string4"

_helpers.tpl:

{{- define "test.template" -}}
{{- $value1 := .Values.testValue1 -}}
{{- $value2 := .Values.testValue2 -}}
{{- $credentials := "" -}}
{{- range $index, $cred := .Values.credentials -}}
{{- $credentials = $cred "," $credentials -}}
{{- end -}}
{{- printf "%s,%s,%s" $value1 $value2 $credentials -}}
{{- end -}}

test.yaml

templatedValue: {{ template "test.template" }}

When I run helm template --output-dir outputs chart I get:

test.yaml

templatedValue: %!s(<nil>),%!s(<nil>),

both the variables I set as values are nil, and the credentials are just an empty string. If I put the values in the values.yaml directly into the test.yaml file it works fine. So I'm not sure why I'm getting these nil values from the template. There's other templates in the _helpers.tpl which grab values from the values.yaml file, so I'm not sure why my template doesn't work. Any help is greatly appreciated.

helm version: 
Client: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}

Solution

  • The {{ template "test.template" }} action executes "test.template" template but does not pass any parameters to it. So inside test.template the pipeline will be <nil> and so .Values is invalid.

    Quoting from text/template:

    {{template "name"}}
      The template with the specified name is executed with nil data.
    
    {{template "name" pipeline}}
      The template with the specified name is executed with dot set
      to the value of the pipeline.
    

    You have to pass something in {{template}}. If you have no other info what to pass, try passing the dot ., the current pipeline.

    {{ template "test.template" . }}