can't use range inside library chart defined template in helm #348
Description
Hello,
I have pretty much the same issue as described here:
https://stackoverflow.com/questions/77352107/cant-use-range-inside-library-chart-defined-template-in-helm
I have created a simple library chart following the documentation here: https://helm.sh/docs/topics/library_charts/#create-a-simple-library-chart
_configmap.yaml:
{{- define "linux-shared.configmap.tpl" -}}
{{- range $i, $v := .Values.configMaps }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ default ( list ( include "linux-shared.fullname" $ ) $i | join "-" ) $v.name | trunc 63 | trimSuffix "-" }}
labels:
{{- include "linux-shared.labels" $ | nindent 4 }}
{{- if $v.data }}
data:
{{- range $k, $v := $v.data }}
{{ $k | quote }}: {{ $v | quote }}
{{- end }}
{{- end }}
{{- if $v.binaryData }}
binaryData:
{{- range $k, $v := $v.binaryData }}
{{ $k | quote }}: {{ $v | quote }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- define "linux-shared.configmap" -}}
{{- include "linux-shared.util.merge" (append . "linux-shared.configmap.tpl") -}}
{{- end -}}
I want to create multiple ConfigMaps based on list variable in values.yaml:
configMaps:
- name: "1"
mountPath: /some/path
data:
foo: "bar"
blb: "kkk"
binaryData:
file: "AgAJU7..."
- name: "2"
data:
foo: "bar"
binaryData:
file: "AgAJU7..."
In a chart I include this library in configmap.yaml:
{{- include "linux-shared.configmap" (list . "linux-httpd.configmap") -}}
{{- define "linux-httpd.configmap" -}}
pepa: ahoj
{{- end -}}
I would expect 2 ConfigMap objects to be created but only the first one is. On other hand if I change configmap.yaml to:
{{- include "linux-shared.configmap.tpl" . -}}
then it works as expected. But I would also like to use the "merge" functionality mentioned in documentation.
Would appreciate any help. Thank you.
helm version: version.BuildInfo{Version:"v3.15.0-rc.2", GitCommit:"c4e37b39dbb341cb3f716220df9f9d306d123a58", GitTreeState:"clean", GoVersion:"go1.22.3"}
_util.tpl:
{{- /*
linux-shared.util.merge will merge two YAML templates and output the result.
This takes an array of three values:
- the top context
- the template name of the overrides (destination)
- the template name of the base (source)
*/}}
{{- define "linux-shared.util.merge" -}}
{{- $top := first . -}}
{{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}}
{{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}}
{{- toYaml (merge $overrides $tpl) -}}
{{- end -}}