Create and Pass the Value using helm helper function from Deployment Or Service Yaml File

4/12/2020

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") }}
-- Sachin Mishra
kubernetes
kubernetes-helm

1 Answer

4/12/2020

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" -}}
-- David Maze
Source: StackOverflow