4
votes

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"}
1
The {{ template "test.template" }} action executes "test.template" but does not pass any parameters to it. So inside test.template the pipeline .Values is invalid.icza
I don't really understand what you mean. There are other templates in the _helpers.tpl file which read from the values file just fine: {{- define "chart.name" -}} {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} {{- end -}} It has no parameters in it either eh?roflcopter1101
What matters is how you call the template. How is "chart.name" called? I'm sure there are parameters passed to it when calling it.icza
Ah. it's called with {{ include "chart.name" . }}. So should I do similar for my test.template template?roflcopter1101
Yes, try to pass the dot . if you have no other info what to pass, e.g. {{ template "test.template" . }}.icza

1 Answers

3
votes

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" . }}