Kubernetes Helm chart: Use _helpers.tpl to concat values and feed back into Values.yaml

10/9/2019

I am new to Kubernetes, Helm, and Golang. Basically, I'm trying to concat strings in my values.yaml file to populate another value in my values.yaml file. I'm using _helpers.tpl to do this (in an attempt to understand Helm and Golang better). I could just write "users: "datafeed:password:::incoming" " in my values.yaml file and be done with it; but would like to avoid that.

I have the following in my values.yaml file:

sftp:
  users: ""
  username: "datafeed"
  password: "password"
  incoming: "incoming"

And want the final values.yaml file to read:

sftp:
  users: "datafeed:password:::incoming"
  username: "datafeed"
  password: "password"
  incoming: "incoming"

To do this, I am trying to edit the _helpers.tpl file. I have tried

{{- define "sftp.users" -}}
{{- .Values.sftp.users: .Values.sftp.username+":"+.Values.sftp.password+":::"+.Values.sftp.incoming -}}
{{- end -}}

and

{{- define "sftp.users" -}}
{{- .Values.sftp.users:= .Values.sftp.username+":"+.Values.sftp.password+":::"+.Values.sftp.incoming -}}
{{- end -}}

Then I tried making each segment a variable (and deleted the explicit values in the values.yaml file):

{{- define "sftp.users" -}}
{{ $username:= "datafeed" }}
{{ $password:= "password" }}
{{ $incoming:= "incoming" }}
{{- .Values.sftp.users= {{$username}}+":"+{{$password}}":::"+{{$incoming}} -}}

and then setting the fields/keys explicitly:

username: {{ .Values.sftp.username | default "datafeed" }}
password: {{ .Values.sftp.password | default "password" }}
incoming: {{ .Values.sftp.incoming | default "incoming" }}
{{- .Values.sftp.users:= username+":"+password+":::"+incoming -}}

and:

{{define "username"}}datafeed{{end}}
{{define "password"}}password{{end}}
{{define "incoming"}}incoming{{end}}
{{define "users"}}{{template "username"}}:{{template "password"}}:::{{template "incoming"}}{{end}}
{{- printf "users" -}}
{{- .Values.sftp.users: users -}}

I have also looked at previous posts: Helm _helpers.tpl: Calling defined templates in other template definitions Kubernetes Helm, combine two variables with a string in the middle How to get values from values.yaml to _helpers.tpl in helm charts

None of this seems to work. I can't tell if it's my approach or my syntax. Probably both.

-- CodeNewb
go
go-templates
kubernetes-helm
yaml

0 Answers