Search code examples
kubernetes-helmgo-templates

How to check for a non-existent dictionary value within a helm template?


In my values config file, I have an array of dictionaries as follows:

    connects_to
     - name: myname
       release: optional release
     - name: another name

Note that name will always be provided but release may or may not be. In my template, I have:

    {{- if .Values.connects_to }}
        app.openshift.io/connects-to: '
    {{- range .Values.connects_to -}}
    {{- if .release -}}  
        {{- .release -}}-{{- .name -}},
    {{- else -}}
        {{- $.Release.Name -}}-{{- .name -}},
    {{- end -}}
    {{- end -}}
    '
    {{- end }}

which gives the error:

    can't evaluate field release in type interface {}

I have also tried using "hasKey" as follows:

    {{- if .Values.connects_to }}
        app.openshift.io/connects-to: '
    {{- range .Values.connects_to -}}
    {{- if hasKey . "release" -}}  
        {{- .release -}}-{{- .name -}},
    {{- else -}}
        {{- $.Release.Name -}}-{{- .name -}},
    {{- end -}}
    {{- end -}}
    '
    {{- end }}

which gives the error:

    wrong type for value; expected map[string]interface {}; got string

How would I accomplish this check without having to specify a value for "release" every time? My helm version:

    version.BuildInfo{Version:"v3.2.3+4.el8", GitCommit:"2160a65177049990d1b76efc67cb1a9fd21909b1", GitTreeState:"clean", GoVersion:"go1.13.4"}

Solution

  • This actually works with the "hasKey" approach, there was an error in the values definition.