简体   繁体   中英

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?

It's not possible to use template code in values.yaml files. But you can merge several values.yaml files to reuse configuration values.

main-values.yaml

components:
  service:
    image: docker-registry.service.dev
    resources:
      requests:
        memory: "100M"
        cpu: "100m"
        limits:
          memory: "300M"
          cpu: "200m"

values.yaml

components:
  service:
    name: service
    imagePullPolicy: Always
    tag: latest
    port: 8080
    accessType: ClusterIP

If you add this to your template, it will contain values from both value files:

components: {{ deepCopy .Values.components | merge | toYaml | nindent 6 }}
  • merge + deepCopy will merge the values of all your values files.
  • toYaml will output the result in yaml syntax.
  • You also have to check the correct indentation. 6 is just a guess.

Call helm template --debug... This generates even invalid yaml output where you can easily check the correct indentation and see other errors.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM