21
votes

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 :-/

3

3 Answers

6
votes

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 }}
2
votes

You can overwrite the values of the subchart with the values of the parent chart as described here: https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/subcharts_and_globals.md

I don't think it's possible (and it also doesn't make sense) to override the template name of the subchart.

What I would do is define the database service name in the .Values files both in the parent and sub charts and let helm override the one in the subchart - that way you will always have the database name in the parent chart. This would however mean that the service name of the database should not be {{ template "name" . }}, but something like {{ .Values.database.service.name }}

mychart/.Values

mysubchart:
   service:
      name: my-database

mychart/templates/deployment.yaml

env:
   - name: DB_HOST
     value: {{ .Values.mysubchart.service.name }}

mychart/charts/mysubchart/.Values

service:
   name: my-database

mychart/charts/mysubchart/templates/service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name }}

Another way is to use global chart values, also described in https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/subcharts_and_globals.md

0
votes

You could import values from a sub chart as described here: https://helm.sh/docs/topics/charts/#importing-child-values-via-dependencies.

However there is a caveat. This works not for values defined at the root level in the values.yaml.

See this issue for more information: https://github.com/helm/helm/issues/9817