Search code examples
kubernetesyamlkubernetes-helm

Inheritance of multiline helm chart template


I want to set resources to pods with helm chart with template of resource section from subchart. Because it should be several different reource templates in subchart. I have values.yaml , main-values.yaml and templates/deployment.yaml The command to update helm chart is

helm upgrade -i mynamespace ./kubernetes/mynamespace --namespace mynamespace --create-namespace -f kubernetes/mynamespace/main-values.yaml --reset-values

Files are cuted to show just an example: main-values.yaml :

namespace: mynamespace
baseUrl: myurl.com
customBranch: dev

components:

  postgresql:
    nodeport: 5432

  elasticsearch:
    nodeport: 9200

resources_minimum:
  requests:
    memory: "100M"
    cpu: "100m"
  limits:
     memory: "300M"
     cpu: "200m"

values.yaml

namespace: 
baseUrl: 
customBranch: 

components:
  service:
    name: service
    image: docker-registry.service.{{ .Values.customBranch }}
    imagePullPolicy: Always
    resources: "{{ .Values.resources_minimum }}"
    tag: latest
    port: 8080
    accessType: ClusterIP
cut

And deployment.yaml is

cut
      containers:
        - name: {{ $val.name }}
          securityContext:
            {{- toYaml $.Values.securityContext | nindent 12 }}
          image: "{{ tpl $val.image $ }}:{{ $val.tag | default "latest" }}"
          imagePullPolicy: {{ $val.imagePullPolicy }}
          resources: "{{ tpl $val.resources $ }}"
cut

And the deployment section of resources does not work at all. However image section with intermediate template {{ .Values.customBranch }} works and nodeport template works fine in services.yaml

spec:
  type: {{ $val.accessType }}
  ports:
    - port: {{ $val.port }}
      name: mainport
      targetPort: {{ $val.port }}
      protocol: TCP
      {{ if and $val.nodeport  }}
      nodePort:  {{ $val.nodeport }}

I've tried $val, toYaml, tpl , and plain $.Values options in resources section of deployment.yaml and got several errors like:

error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{".Values.resources_minimum":interface {}(nil)}

or

error converting YAML to JSON: yaml: line 29: could not find expected ':'

and other error like so.

Is it impossible to push yaml values of multiline resources_minimum through values.yaml to deployment.yaml? Which syntax should I use? What documentation can you advice me to read?


Solution

  • Ok. Fellows helped me with elegant solution. values.yaml :

    resource_pool:
      minimum:
        limits:
          memory: "200M"
          cpu: "200m"
        requests:
          memory: "100M"
          cpu: "100m"
    ...
    components:
      service:
        name: service
        image: docker.image
        imagePullPolicy: Always
        tag: latest
        resources_local: minimum
    
    

    And deployment.yaml :

              {{- range $keyResources, $valResources := $.Values.resource_pool }}
                {{- if eq $val.resources_local $keyResources }}
                  {{ $valResources | toYaml | nindent 12}}
                {{- end }}
              {{- end }}
    

    Any sugestion what to read to get familiar with all Helm trics?