jbrown
jbrown

Reputation: 7986

How to get the name of a child chart with Helm?

I have a helm chart that requires stable/redis as a child chart. The parent chart needs to expose the redis service as an environment variable.

The redis chart includes a template called redis.fullname. How can I refer to this in my parent chart? I.e. I want something like this in my parent deployment but it doesn't work:

kind: Deployment
spec:
  template:
    containers:
        env:
        - name: REDIS_CLUSTER_SERVICE_HOST
          value: {{ template "redis.fullname" . }}

Upvotes: 12

Views: 4084

Answers (3)

dpoetzsch
dpoetzsch

Reputation: 765

Improving on Akash's answer, you could try to emulate the correct scope. This also only works if you know what variables the sub-template uses, but might be a bit more stable:

{{ template "redis.fullname" (dict "Values" $.Values.redis "Chart" (dict "Name" "redis") "Release" $.Release) }}

Upvotes: 0

Akash
Akash

Reputation: 969

Templates are now sharable across parent and child charts. Refer this - https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/subcharts_and_globals.md#sharing-templates-with-subcharts

Problem that I see will be:

If your redis.fullname template uses a variable (eg: .Values.commonVariable) that has the same name in both the charts but with different value, then, while referencing it in the parent chart, the value that will be used will be of the parent chart and not the child's.

Consider this:

{{- define "zookeeper.fullname" -}}
{{- printf "%s-%s" (.Values.component) (.Values.subcomponent) -}}
{{- end -}}

Although I want my zookeeper.fullname to be referenced in a kafka (parent) chart. But the .Values.component and .Values.subcomponent will be used of kafka and not that for zookeeper (the subchart) in the case, which totally destroys the idea.

The way out in this specific case will be to use Jainish Shah's answer. But if this is not the case, please don't follow that answer. That destroys the idea of templating. If in any way you ought to change the template function in the subchart, you will also need to modify the value {{ .Release.Name }}-redis in your parent chart. This is not templating.

Link for the aforementioned issue - https://github.com/kubernetes/helm/issues/4314

Upvotes: 1

Jainish Shah
Jainish Shah

Reputation: 1257

You can use '{{ .Release.Name }}-redis' in your parent chart. I had same requirement. This is my example in case you want to take a look ->https://github.com/kubernetes/charts/tree/master/incubator/distribution

Upvotes: 2

Related Questions