Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
like image 313
Khary Mendez Avatar asked Nov 27 '25 10:11

Khary Mendez


2 Answers

Regarding @DieterDP 's response https://stackoverflow.com/a/72360797/17143221

I am getting the same issue when using this template. This can be solved by using parentheses around the optional dict as a mean of null-safety.

template:

# Fails when somedict doesn't exist
{{- if hasKey .Values.somedict "valueX" -}}
    {{- .Values.somedict.valueX -}}
{{- end -}}

# Works when somedict doesn't exist
{{- if hasKey (.Values.somedict) "valueX" -}}
    {{- .Values.somedict.valueX -}}
{{- end -}}

values.yaml:

# without "somedict"
like image 79
Itai B Avatar answered Nov 29 '25 02:11

Itai B


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

like image 24
Khary Mendez Avatar answered Nov 29 '25 01:11

Khary Mendez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!