11
votes

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" . }}
3
I think I have a very similar problem. Question is here: stackoverflow.com/questions/47757968/… - look at the update below the question, thats how I currently deal with with (I overwrite the template definition of the subchart with a name I know)wemu

3 Answers

2
votes

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

1
votes

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

0
votes

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) }}