Another implementation, that I use:
inside the _helpers.tpl, create named template
{{- /*
Flattens a yaml structure into a key-value dict. The keys become uppercase underscored version of the yaml structure.
Example values:
something:
hello:
world: 123
universe: 456
xy: abc
Call: {{- $flattened := include "flattenYamlToUnderscored" (dict "prefix" "SOMETHING" "structure" $.Values.something) | fromYaml }}
Returns: Key-Value-Map with flattened yaml, e.g.
SOMETHING_HELLO_UNIVERSE: 456
SOMETHING_HELLO_WORLD: 123
SOMETHING_XY: abc
*/}}
{{- define "flattenYamlToUnderscored" -}}
{{- $prefix := .prefix -}}
{{- $structure := .structure -}}
{{- if kindIs "map" $structure -}}
{{/* This is a map, go on iterating over the children */}}
{{- $result := dict -}}
{{- range $key, $value := $structure -}}
{{/* For each child generate the new key and call the recursion, and add its result to the result list */}}
{{- $newKey := printf "%s_%s" $prefix $key | trimAll "_" | upper -}}
{{- $childResult := (include "flattenYamlToUnderscored" (dict "prefix" $newKey "structure" $value) | fromYaml) -}}
{{- $_ := merge $result $result $childResult -}}
{{- end -}}
{{/* Return the merged results of the children */}}
{{- $result | toYaml -}}
{{- else if not (eq $structure nil) -}}
{{/* We are not a map, nor a nil, so we are a leaf inside the recursion. $structure is the leaf value. Return [$prefix : $structure] as result */}}
{{- dict $prefix $structure | toYaml -}}
{{- end -}}
{{- end -}}
Using the values
config:
server:
port: 3333
other:
setting:
name: test
And then call it like this:
env:
{{- $flattenedValues := include "flattenYamlToUnderscored" (dict "prefix" "CONFIG" "structure" $.Values.config) | fromYaml }}
{{- range $key, $value := $flattenedValues }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
results in
env:
- name: CONFIG_OTHER_SETTING_NAME
value: "test"
- name: CONFIG_SERVER_PORT
value: "3333"