Assign same helm value to more than one targets

3/11/2019

I am working on a Helm chart, which should spin up a database as well a client which consumes the database. To avoid redundancy I only wanna provide one value (username, password), which should be assigned to the client and the DB. And only if it's not the default value, all other values should be overridden.

So, when I create the chart and pass a different value to the db.env.POSTGRES_PASSWORD it should override the users.env.DB_PASSWORD as well

helm install --debug --dry-run --set db.env.POSTGRES_PASSWORD=test

Here a short snippet from my values.yml

db:
  env:
    POSTGRES_PASSWORD: "default123"

users:
  env:
    DB_PASSWORD: "default123"

I think I have to do this somehow int the _helpers.tpl file. Something like this, but it is not working.

{{- define "db.env.POSTGRES_PASSWORD" -}}
{{- default "password123" -}}
{{- end -}}

{{- if ne db.env.POSTGRES_PASSWORD "default123" }} # => This line causes the error
{{- define "users.env.DB_PASSWORD" -}}
{{- default .Values.db.env.POSTGRES_PASSWORD -}}
{{- end -}}
{{- end }}

ERROR:

unexpected <define> in command

Please point me to the right direction, thank you

-- Jan
go-templates
kubernetes-helm

1 Answer

3/13/2019

You might restructure this to have a single setting of that password and then inject it into multiple places. The Helm values.yaml file could include

databasePassword: default123

and then individual Deployments' YAML files could include

env:
  - name: DB_PASSWORD
    value: {{ .Values.databasePassword | quote }}

For the specific logic you're asking about, the important thing is that {{ define }} must always be at the top level (you can't conditionally define a template); also, the templates and values settings are in different namespaces (you can't use define to change a .Values value). You can write a template that always produces some specific password value, choosing a source for it:

{{- define "users.env.DB_PASSWORD" -}}
{{- if ne .Values.db.env.POSTGRES_PASSWORD "default123" -}}
{{- .Values.db.env.POSTGRES_PASSWORD -}}
{{- else -}}
{{- .Values.users.env.DB_PASSWORD -}}
{{- end -}}

and call it in the Deployment template:

env:
  - name: DB_PASSWORD
    value: {{ include "users.env.DB_PASSWORD" . | quote }}

It's important to note here that default takes two parameters; calling db.env.POSTGRES_PASSWORD as you've shown it will produce an error because it doesn't know what value to apply that default to.

-- David Maze
Source: StackOverflow