14
votes

I'm trying to template variables from a map inside the values.yaml into my final Kubernetes ConfigMap YAML.

I've read through https://github.com/helm/helm/issues/2492 and https://helm.sh/docs/chart_template_guide/ but can't seem to find an answer.

For some context, this is roughly what I'm trying to do:

values.yaml

config:
  key1: value
  key2: value-{{ .Release.Name }}

configmap.yaml

kind: ConfigMap
data:
  config-file: |
    {{- range $key, $value := .Values.config }}
    {{ $key }} = {{ $value }}
    {{- end }}

Where the desired output with would be:

helm template --name v1 mychart/

kind: ConfigMap
data:
  config-file: |
    key1 = value
    key2 = value-v1

I've tried a few variations using template functions and pipelining, but to no avail:

{{ $key }} = {{ tpl $value . }}
{{ $key }} = {{ $value | tpl . }}
{{ $key }} = {{ tpl $value $ }}
3

3 Answers

10
votes

The above would also have worked in this way

values.yaml

config:
  key1: "value"
  key2: "value-{{ .Release.Name }}"

configmap.yaml

kind: ConfigMap
data:
  config-file: |
    {{- range $key, $value := .Values.config }}
    {{ $key }} = {{ tpl $value $ }}
    {{- end }}

What I changed was : I put value in quotes in value.yaml and used template tpl in the config map.

7
votes

Managed to solve this using the following syntax:

configmap.yaml

kind: ConfigMap
data:
  config-file: |
    {{- range $key, $value := .Values.config }}
    {{ $key }} = {{ tpl ($value | toString) $ }}
    {{- end }}
3
votes

there is fight in this PR here about this topic.

I know that it's possible now, but this require maintenance of the chart to be in-house (e.g. answer of Amrut ).

Let's summarize :

To have templating in values.yaml , these are the available options:

  1. helm may support that in future ( watch this thread about this topic.)

  2. use tpl function inside the chart

  3. use another tool on top of helm : terraform or helmfile.