Search code examples
kuberneteskubernetes-helm

Import parent template with subchart values


I have multiple sucharts with applications and a parent chart that will deploy them.

All subcharts have the same manifests for the underlying application. Therefore I decided to create a library and put general variables from subcharts in it.

Example from lib:

{{- define "app.connect.common.release.common_libs.servicetemplate" -}}  
apiVersion: v1
kind: Service
metadata:
  labels:
  annotations:
    service.beta.kubernetes.io/azure-load-balancer-internal: "true"
  name: {{ .Values.application.name }}-service
  namespace: {{ .Values.global.environment.namespace }}
spec:
  type: LoadBalancer
  ports:
    - name: https
      port: 443
      targetPort: 8080
    - name: http
      port: 80
      targetPort: 8080
  selector:
    app: {{ .Values.application.name }}
status:
  loadBalancer: {}
{{- end }}

I declared a dependency in Chart.yaml and executed helm dep up. Then in my subchart I'm importing this template. But when I'm trying to run --dry-run on parent chart I'm receiving the following error:

Error: template: app.connect.common.release/charts/app.connect.common.release.chtmgr/templates/service.yaml:1:4: executing "app.connect.common.release/charts/app.connect.common.release.chtmgr/templates/service.yaml" at <include "app.connect.common.release.common_libs.servicetemplate" .>: error calling include: template: app.connect.common.release/charts/app.connect.common.release.chtmgr/charts/app.connect.common.release.common_libs/templates/_helpers.tpl:169:18: executing "app.connect.common.release.common_libs.servicetemplate" at <.Values.application.name>: nil pointer evaluating interface {}.name

My values values.yaml in the subchart:

application:
  name: chtmgr-api
  image: cht-mgr-api

The same error with named template.

Is it possible to put general values from subchart in parent template(example _helper.tpl) and import it in subchart?

If not, how do you implement this?

I've checked a lot of resources but still don't have an idea am I going in the right direction.


Solution

  • Found a solution. Problem was related to the chart/template names all of them include '.' in the names what causes problem. Don't include dots in your templates/charts names. Example how not to do: "ibext.common.connect.etc".

    My assumption

    I'm quite newbie in helm and my assumption that helm when template engine is looking in the subchart it goes through the following way to find variables .Values.subchartName in my case it was .Values.ibext.common.connect.etc So when it faced with .Values.ibext it can't understand what it is(but helm doesn't show anything).

    But this is only my assumption. If someone there understands the behaviour of template engine in this case please reveal the secret.