I'm using helm and given a yaml object I want to flatten it while applying some recursive formatting.
Given this:
some_map:
with: different
indentation:
levels: and
nested:
sub:
maps: "42"
and_more:
maps: 42
I want to (for example) get this:
some_map.with="different"
some_map.indentation.levels="and"
some_map.nested.sub.maps="42"
some_map.nested.and_more.maps=42
I haven't read anything about recursive looping in the helm docs, keep in mind that the format of the recursion in the example ( "%v.%v" if !root else "%v=%v" ) may vary.
another approach
_helpers.tpl:
{{- define "template.flattenFn" -}}
{{- $ctx := . -}}
{{- if or (eq (kindOf .data) "map") (eq (kindOf .data) "slice") }}
{{- range $key, $value := .data }}
{{- include "template.flattenFn" (dict "prefixes" (append $ctx.prefixes $key) "data" $value ) }}
{{- end }}
{{- else if .prefixes }}
{{- printf "\"%s\":%s," (join "__" .prefixes) ( mustToJson ( .data | toString )) }}
{{- end }}
{{- end -}}
{{- define "template.flatten" -}}
{{- $result := include "template.flattenFn" (dict "prefixes" list "data" . ) -}}
{ {{- trimSuffix "," $result -}} }
{{- end -}}
values.yaml
environment_map:
Log:
Level:
Default: Debug
Target:
- Name: Console
Args:
formatter: "JsonFormatter"
usage
{{- range $env, $value := include "template.flatten" .Values.environment_map | fromYaml }}
- name: {{ $env }}
value: {{ $value | quote }}
{{- end }}
result
- name: Log__Level__Default
value: "Debug"
- name: Log__Target__0__Args__formatter
value: "JsonFormatter"
- name: Log__Target__0__Name
value: "Console"