34

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 $ }}
ahstn
  • 471
  • 1
  • 4
  • 6

4 Answers4

28

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.

Amrut Prabhu
  • 1,161
  • 11
  • 11
  • 5
    In case anyone was wondering: When the `tpl` function is used in a range block, it needs the top-level context (e.g. `$`) as the second argument instead of the usual `.` argument. https://stackoverflow.com/a/67815474/392184 – Lee Grissom Feb 17 '22 at 04:56
26

I'll refer to the question's title regarding templating variables in helm and suggest another option to use on values.yaml which is YAML Anchors.

Docs reference

As written in here:

The YAML spec provides a way to store a reference to a value, and later refer to that value by reference. YAML refers to this as "anchoring":

coffee: "yes, please"
favorite: &favoriteCoffee "Cappucino"
coffees:
  - Latte
  - *favoriteCoffee
  - Espresso

In the above, &favoriteCoffee sets a reference to Cappuccino.

Later, that reference is used as *favoriteCoffee.
So coffees becomes Latte, Cappuccino, Espresso.

A more practical example

Referring to a common image setup (Registry and PullPolicy) in all values.yaml.

Notice how the default values are being set at Global.Image next to the reference definition which starts with &:

Global:
  Image:
    Registry: &global-docker-registry "12345678910.dkr.ecr.us-west-2.amazonaws.com" # <--- Default value
    PullPolicy: &global-pull-policy "IfNotPresent" # <--- Default value

Nginx:
  Image:
    Registry: *global-docker-registry
    PullPolicy: *global-pull-policy
    Version: 1.21.4
    Port: 80

MySql:
  Image:
    Registry: *global-docker-registry
    PullPolicy: *global-pull-policy
    Name: mysql
    Version: 8.0.27
    Port: 3306
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rot-man
  • 18,045
  • 12
  • 118
  • 124
  • how to use this approach if I want to use this variable as a part in some value like MySql: Image: Registry: Prefix-*global-docker-registry-Suffix – Prabhat Jan 07 '22 at 07:32
  • 1
    I haven't tried string concatenation in the values file - not sure it would work. You can add the prefix/suffux in the resource itself, and let the *global-docker-registry injected from the global reference. – Rot-man Jan 07 '22 at 09:07
  • 3
    that has a very limited use-case due to how helm anchors work. https://helm.sh/docs/chart_template_guide/yaml_techniques/#yaml-anchors : if you override Global.Image.Registry with --set all your references will remain with the original value of the anchor. which is nasty – Dmitry May 14 '22 at 10:30
13

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 }}
ahstn
  • 471
  • 1
  • 4
  • 6
10

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.

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • Saw the github thread, would be helpful if that has been implemented. I see the changes are merged. Are the changes available officially? – Sudhanva c Sep 02 '21 at 07:01