Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 831
wemu Avatar asked Dec 11 '17 17:12

wemu


People also ask

How do you pass values to Helm chart?

You can use a --values flag in your Helm commands to override the values in a chart and pass in a new file. Specify the name of the new file after the --values flag in the Helm command. Example: helm upgrade --install <service> -f values.

How do you declare a variable in Helm template?

In Helm templates, a variable is a named reference to another object. It follows the form $name . Variables are assigned with a special assignment operator: := . We can rewrite the above to use a variable for Release.Name .

Can I use values in chart YAML?

Chart developers may supply a file called values. yaml inside of a chart. This file can contain default values. Chart users may supply a YAML file that contains values.


1 Answers

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 }} 
like image 81
matth-boise Avatar answered Sep 28 '22 03:09

matth-boise