I would like to convert a part of the structure in values.yaml to properties file in a config map.
Is it possible to convert a yaml structure like:
field1: value1
field2:
field21: value21
field22: value22
into
field1=value1
field2.field21=value21
field2.field22=value22
with Helm templating functions?
As a programming problem, this is a straightforward recursive call. You can use Go text/template
templates like functions. The one trick here is that they only take a single parameter, so you need to use the sprig list
function to pack multiple values into that parameter, and the text/template
index
function to get values back out.
{{- define "envify" -}}
{{- $prefix := index . 0 -}}
{{- $value := index . 1 -}}
{{- if kindIs "map" $value -}}
{{- range $k, $v := $value -}}
{{- template "envify" (list (printf "%s.%s" $prefix $k) $v) -}}
{{- end -}}
{{- else -}}
{{ $prefix }}={{ $value }}
{{ end -}}
data:
{{ template "envify" (list "" .Values.fields) | indent 2 }}
This will work with arbitrarily deep nested values.
If you refer to standard Helm variables (.Release
, .Values
, ...) this also becomes tricky because the .
variable is reused for the single template parameter (it also gets reused within the range
loop). I tend to explicitly pass it as an additional template parameter.
{{- $top := index . 2 -}}
# from the {{ $top.Chart.Name }} Helm chart
{{/* in your ConfigMap proper */}}
{{ template "envify" (list "" .Values.fields .) }}