简体   繁体   中英

helm using argument in template to check if value exists in Values.yaml

The problem is following: I want to check if field in Values.yaml exists based on argument given to the template in _helpers.tpl:

{{- define "example-name" -}}
{{- $objectRef := index . 0 -}}
{{- if .Values.custom -}}
{{- if .Values.custom.$objectRef -}}
{{- if .Values.custom.$objectRef.annotations -}}
{{- include "some-library" (tuple .Values.custom.$objectRef.annotations) | indent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

Then in my deployment for example:

{{- template "example-name" "someField" }}

I want the result to be following:

{{- define "example-name" -}}
{{- $objectRef := index . 0 -}}
{{- if .Values.custom -}}
{{- if .Values.custom.someField-}}
{{- if .Values.custom.someField.annotations -}}
{{- include "some-library" (tuple .Values.custom.someField.annotations) | indent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

But the only thing I get is following error: bad character U+0024 '$' I want to use template multiple times with various arguments. I haven't seen anyone dealing with the same problem before.

Any ideas?

The standard template function you're looking for is index . In its simplest form, index $map $key does a dynamic lookup of the $key (can be any expression) in the provided $map (can be any expression). It can also do integer-index lookups in arrays (Go slices) and nested lookups if you need to.

The other problem that you'll run into is that $map.undefinedKey (or index $map "undefinedKey" ), assuming undefinedKey is not present in $map , is valid but evaluates to Go nil . So you can't do further lookups in that. The workaround to this I typically use is to use the Helm (Sprig) default function to use an empty dict if a value is not present.

That would give you a template like:

{{- define "example-name" -}}
{{- $top := index . 0 -}}
{{- $objectRef := index . 1 -}}
{{- $custom := $top.Values.custom | default dict -}}
{{- $object := index $custom $objectRef | default dict -}}
{{- with $object.annotations -}}
{{- include "some-library" (list $top .) | indent 4 }}
{{- end }}
{{- end }}

This is called with a list of two values, the top-level Helm object and a reference to a key in .Values.custom

{{- include "example-name" (list . "someField") -}}

The template extracts the two values from the list parameter. It then traverses the values structure one level at a time, at each level defaulting to an empty dictionary. So for example if there is no .Values.custom then $custom is set to an empty dictionary, which allows index $custom $objectRef to execute successfully (and return nil , but not abort). At the bottom level we use the with template function to check to see if come value is truthy, and if so, temporarily bind . to its value. When we make the inner call, we already have the top-level Helm object in a variable, and we can assemble a list of $top and the non-empty annotation structure . as the single template parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM