How to reference a value defined in a template in a sub-chart in helm for kubernetes?

wemu picture wemu · Dec 11, 2017 · Viewed 11.5k times · Source

I'm starting to write helm charts for our services.

There are two things I'm not sure how they are supposed to work or what to do with them.

First: the release name. When installing a chart, you specify a name which helm uses to create a release. This release name is often referenced within a chart to properly isolate chart installs from each other? For example the postgres chart contains:

{{- define "postgresql.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

Which is then used for the service:

metadata:
  name: {{ template "postgresql.fullname" . }}

It does look like "myrelease-postgresql" in the end in kubernetes. I wonder what a good release name is? What is typically used for this? A version? Or some code-name like the ubuntu releases?

Second: referencing values.

My chart uses postgresql as a sub-chart. I'd like to not duplicate the way the value for the name of the postgresql service is created (see snipped above).

Is there a way I can reference the service name of a sub-chart or that template define {{ template "postgresql.fullname" . }} in the parent chart? I need it to pass it into my service as database host (which works if I hardcode everything but that cannot be the meaning of this).

I tried:

      env:
        - name: DB_HOST
          value: {{ template "mychart.postgresql.fullname" . }}

But that lead into an error message:

template "mychart.postgresql.fullname" not defined

I've seen examples of Charts doing similar things, like the odoo chart. But in here that logic how the postgresql host name is created is copied and an own define in the template is created.

So is there a way to access sub-chart names? Or values or template defines?

Thanks!

Update after some digging: According to Subcharts and Globals the templates are shared between charts.

So what I can do is this:

In my chart in _helpers.tpl I add (overwrite) the postgres block:

{{- define "postgresql.fullname" -}}
{{- $name := .Values.global.name -}}
{{- printf "%s-%s" $name "postgresql" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

So this value is used when the sub-chart is deployed. I cannot reference all values or the chart name in here as it will be different in the sub-chart - so I used a global value.

Like this I know the value of the service that is created in the sub-chart.

Not sure if this is the best way to do this :-/

Answer

matth-boise picture matth-boise · Jan 17, 2018

Are you pulling in postgresql as a subchart of your chart (via your chart's requirements.yaml)? If so, both the postgresql (sub) chart and your chart will have the same .Release.Name - thus, you could specify your container's environment as

  env:
    - name: DB_HOST
      value: {{ printf "%s-postgresql" .Release.Name }}

if you override postgresql's name by adding the following to your chart's values.yaml:

postgresql:
  nameOverride: your-postgresql

then your container's env would be:

  env:
    - name: DB_HOST
      value: {{ printf "%s-%s" .Release.Name .Values.postgresql.nameOverride }}