Skip to content

Instantly share code, notes, and snippets.

@rmetzler
Last active June 20, 2023 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmetzler/bc9401874553724afb4341d31ad2c427 to your computer and use it in GitHub Desktop.
Save rmetzler/bc9401874553724afb4341d31ad2c427 to your computer and use it in GitHub Desktop.
flatten lists in helm and go templating
{{- define "flatten_list" -}}
{{- $output := list -}}
{{- range . -}}
{{- if (kindIs "slice" . ) -}}
{{- $output = (concat $output ( get (fromYaml (include "flatten_list" . ) ) "list" ) ) -}}
{{- else -}}
{{- $output = (append $output . ) -}}
{{- end -}}
{{- end -}}
{{- toYaml (dict "list" $output) -}}
{{- end -}}
{{- define "flatten" -}}
{{- get ( fromYaml (include "flatten_list" . ) ) "list" | toYaml }}
{{- end -}}
{{- $nested := (list "a" "b" "c" (list 1 2 3)) -}}
flattend_list:
{{- include "flatten" $nested | nindent 4 }}
@rmetzler
Copy link
Author

rmetzler commented Aug 5, 2021

currently you need to jump through some hoops to make flattened list in helm

@rmetzler
Copy link
Author

rmetzler commented Aug 7, 2021

I think it also would be possible to create a flatten function with tail-recursion so we don't have to do fromYaml and toYaml, but it would be preferable to have something like this in sprig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment