1

Inside the template I have a fragment like this:

props: {{- toYaml .Values.myApp.container.props }}

currently props contains 4 children:

...
    container:
      props:
        a: ...
        b: ...
        c: ...
        d: ...

But I want to exclude c on the fly. Is there way to do it ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

3 Answers3

1

Try:

{{ $myProps := .Values.myApp.container.props }}
{{ $_ := unset $myProps "c" }}

Then you can use it, for example:

props: {{ $myProps | toYaml | nindent 2 }}

which leads to:

props: 
  a: 1
  b: 2
  d: 4
Andromeda
  • 1,205
  • 1
  • 14
  • 21
0

The docs say

This function is equivalent to GoLang yaml.Marshal function, see docs here: https://pkg.go.dev/gopkg.in/yaml.v2#Marshal

That function does support excluding certain fields, however this works by annotating Go types which is not viable here. So you'd need to remove the field before piping the values into toYaml.

Helm does provide a without function, however that only works on lists. So you seem to be out of luck here.

A last resort would be to use regexReplaceAll on the resulting YAML string, but… do you really want to do that?

flyx
  • 35,506
  • 7
  • 89
  • 126
0

Helm includes a function on dictionaries called omit.

The omit function is similar to pick, except it returns a new dict with all the keys that do not match the given keys. Example:

$myDict := dict "name1" "value1" "name2" "value2" "name3" "value 3"
$new := omit $myDict "name1" "name3"

The above returns {name2: value2}

In your case this would mean:

props: {{ omit .Values.myApp.container.props "c" | toYaml }}
Itay Grudev
  • 7,055
  • 4
  • 54
  • 86