How to merge 2 lists together (of containers) in helm?

2.8k views Asked by At

I'm trying to create my own chart library from tutorial: https://helm.sh/docs/topics/library_charts/ . In the tutorial, there is really nice function, which merges 2 yamls together:

{{- define "libchart.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 -}}

I define web deployment template as:

{{- define "libchart.web.tpl" -}}
(...)
      containers:
      - env:
        envFrom:
        - configMapRef:
            name: app-configmap
        - secretRef:
            name: app-secrets
        image: {{ include "libchart.image" . }}
        imagePullPolicy: Always
        name: web
        resources: {}

(...)
{{- end -}}
{{- define "libchart.web" -}}
{{- include "libchart.util.merge" (append . "libchart.web.tpl") -}}
{{- end -}}

When I want to override a dictionary it's ok:

{{- include "libchart.web" (list . "web") -}}
{{- define "web" -}}
metadata:
    annotations:
      test1: "test1"
{{- end -}}

And now I want to override resources:

{{- include "libchart.web" (list . "web") -}}
{{- define "web" -}}
spec:
  template:
    spec:
      containers:
      - resources:
          requests:
            cpu: '0.01'
            memory: 109.0Mi
{{- end -}}

And the error which I get is:

Error: unable to build kubernetes objects from release manifest: error validating "": error validating data: ValidationError(Deployment.spec.template.spec.containers[0]): missing required field "name" in io.k8s.api.core.v1.Container

Which means that it tries to create 2 elements on the container list.

I found closed issue on githug: https://github.com/helm/charts/issues/19855.

0

There are 0 answers