Skip to content

can't use range inside library chart defined template in helm #348

Open

Description

@mkopecny-cen88446

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 -}}

Activity

  1. kfox1111 commented on Jun 5, 2024

    @kfox1111

    the macro is assuming '.' has Values at the range, but then your passing a list:
    (list $ "linux-httpd.configmap")
    which has no .Values, so range is probably skipping over it?

  2. mkopecny-cen88446 commented on Jun 5, 2024

    @mkopecny-cen88446
    Author

    the macro is assuming '.' has Values at the range, but then your passing a list: (list $ "linux-httpd.configmap") which has no .Values, so range is probably skipping over it?

    No idea to be honest. I just followed documentation. The first item from range is rendered properly thought.

  3. Yagya-omnius commented on Aug 30, 2024

    @Yagya-omnius

    in values.yaml, you can define as

    configmapvalues:
      DATABASE_HOST: postgres-omnius
      DATABASE_PORT: 5432
    

    in configmap.yaml

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: {{ .Chart.Name }}-config
    data:
      {{- range $key, $val := .Values.configmapvalues }}
      {{ $key }}: {{ $val | quote }}
      {{- end }}
    
  4. gjenkins8 commented on Dec 14, 2024

    @gjenkins8
    Member

    Issues regarding Helm usage are best posted over on the helm software repo https://github.com/helm/helm. Please repost there. Thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions