Search code examples
kuberneteskubernetes-helm

Embedded tpl statements in helm


I am working with helm.

I have a condtion where a variable in values.yaml ( variable name is db) will get conditional value (either oracle or postgres).

In the same values.yaml i have two sections containing respective properties for oracle & postgres.

How can I use the variable in db in nested manner? I want to avoid if else blocks.

I tried {{tpl .Values.{{tpl .Values.db .}}.port .}}. but it doesn't work.

Please find below code snippet

Values.yaml

db: postgres
postgres:
  port:5432
oracle:
  port:1521

templatefile.yaml

port: "{{tpl .Values.{{tpl .Values.db .}}.port .}}"

Solution

  • You can't nest the {{ ... }} blocks in the Helm templating language.

    You can set a variable to the value of the inner "template" or just invoke it directly as an expression

    {{- $dbname := tpl .Values.db . -}}
    {{- printf "%s" (tpl .Values.db .) -}}
    

    To actually use this as a field in the .Values structure, you need the text/template index function.

    {{- $settings := index .Values (tpl .Values.db .) -}}
    port: "{{ $settings.port }}"