helm with if condition and its scope for the value set

9/8/2018

I would like to overwrite the user template with .Values.userTemplate if it exist. But I always get $user as undefined when the .Vaues.userTemplate exist. If i remove if condition and use {{- $user := tpl .Values.userTemplate . -}}, it gives right value. But inside if codition it is not setting the value for $user.

Is there anything was doing wrong? Was the scope of $user will be only retained inside the if condition? Was there a way to overwrite the $user if .Values.userTemplate exist.

    # values
    user: "default"
       # somewhere in other chart we define the  template
    userTemplate: '{{ include "username" . }}'

    # template
    {{- define "user" -}}
    {{- if .Values.userTemplate -}}
    {{- $user := tpl .Values.userTemplate . -}}
    {{- else -}}
    {{- $user := .Values.user -}}
    {{- end -}}
    {{- $user -}}

    # output of `{{ template "user" . }}` should be the value set in `{{ template "username" . }} in different chart. 

Tom
-- user1595858
kubernetes
kubernetes-helm

1 Answer

9/8/2018

I use helm 2.10. I did a small test. If I understand correctly it is what you want:

values.yml:

user: "default"
userTemplate: '{{ include "userName" . }}'
otherName: "john"

_helpers.tpl:

{{- define "userName" -}}
{{- .Values.otherName -}}
{{- end -}}

{{- define "user" -}}
{{- if .Values.userTemplate -}}
{{- tpl .Values.userTemplate . -}}
{{- else -}}
{{- .Values.user -}}
{{- end -}}
{{- end -}}

test.yml:

name: {{ template "user" . }}

With a userTemplate, I got john, without userTemplate I got default.

-- wargre
Source: StackOverflow