3
votes

I was trying to implement the Helm Kubernetes deployment using helm chart. I would to give the deployment,app and service name using the helm release name dynamically.

I have implemented the below approach which help me to create the dynamic name based upon the helm file.

Written the below method inside the _helper.tpl

{{- define "sample-chart-getServiceName" -}}
{{- $arg1 := index . "arg1" -}}
{{- $fullName := include "sample-chart.fullname" . -}}
{{- printf "%s-%s" $fullName $arg1 -}}
{{- end -}} 

and below ways to calling the function inside the yaml file

name: {{ include "sample-chart-getServiceName" (dict "arg1" "service") }}
2

2 Answers

4
votes

A Helm template takes a single parameter, and it's assigned to the special variable .. In your example you're trying to use . as both the parameter holding the service name and also the top-level context to pass to the downstream sample-chart.fullname template.

To work around this, you can pack the two parameters into a list:

{{ include "sample-chart-getServiceName" (list . (dict "arg1" "service")) }}
{{- define "sample-chart-getServiceName" -}}
{{- $top := index . 0 -}}
{{- $arg1 := index . 1 "arg1" -}}
{{- $fullName := include "sample-chart.fullname" $top -}}
...

Or, if you were intending to pass multiple values by name, include the top-level context as one of those parameters

{{ include "sample-chart-getServiceName" (dict "top" . "arg1" "service") }}
{{- $top := index . "top" -}}
{{- $arg1 := index . "arg1" -}}
0
votes

from rektide at https://github.com/helm/helm/issues/3215#issuecomment-699558821

he uses a merge technique

invoking: {{ template "rocking-template" (merge (dict "subdomain" "hello") .) }}

using: {{ .subdomain }}.{{ .domain }}