Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mkopecny-cen88446 opened this issue Jun 5, 2024 · 4 comments
Open

Comments

@mkopecny-cen88446
Copy link

mkopecny-cen88446 commented Jun 5, 2024

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 -}}
@kfox1111
Copy link

kfox1111 commented Jun 5, 2024

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?

@mkopecny-cen88446
Copy link
Author

mkopecny-cen88446 commented Jun 5, 2024

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.

@Yagya-omnius
Copy link

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

@gjenkins8
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants