How to reference a value defined in a template in a sub-chart in helm for kubernetes?

前端 未结 2 466
暗喜
暗喜 2020-12-31 01:52

I\'m starting to write helm charts for our services.

There are two things I\'m not sure how they are supposed to work or what to do with them.

First: the rel

相关标签:
2条回答
  • 2020-12-31 02:18

    Are you pulling in postgresql as a subchart of your chart (via your chart's requirements.yaml)? If so, both the postgresql (sub) chart and your chart will have the same .Release.Name - thus, you could specify your container's environment as

      env:
        - name: DB_HOST
          value: {{ printf "%s-postgresql" .Release.Name }}
    

    if you override postgresql's name by adding the following to your chart's values.yaml:

    postgresql:
      nameOverride: your-postgresql
    

    then your container's env would be:

      env:
        - name: DB_HOST
          value: {{ printf "%s-%s" .Release.Name .Values.postgresql.nameOverride }}
    
    0 讨论(0)
  • 2020-12-31 02:18

    You can overwrite the values of the subchart with the values of the parent chart as described here: https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/subcharts_and_globals.md

    I don't think it's possible (and it also doesn't make sense) to override the template name of the subchart.

    What I would do is define the database service name in the .Values files both in the parent and sub charts and let helm override the one in the subchart - that way you will always have the database name in the parent chart. This would however mean that the service name of the database should not be {{ template "name" . }}, but something like {{ .Values.database.service.name }}

    mychart/.Values

    mysubchart:
       service:
          name: my-database
    

    mychart/templates/deployment.yaml

    env:
       - name: DB_HOST
         value: {{ .Values.mysubchart.service.name }}
    

    mychart/charts/mysubchart/.Values

    service:
       name: my-database
    

    mychart/charts/mysubchart/templates/service.yaml:

    apiVersion: v1
    kind: Service
    metadata:
      name: {{ .Values.service.name }}
    

    Another way is to use global chart values, also described in https://github.com/kubernetes/helm/blob/master/docs/chart_template_guide/subcharts_and_globals.md

    0 讨论(0)
提交回复
热议问题