本文介绍了如何在 kubernetes 的 helm 子图中引用模板中定义的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始为我们的服务编写舵图.

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.

第一个:版本名称.安装 chart 时,您指定 helm 用于创建发布的名称.此版本名称通常在图表中引用以正确隔离图表安装?例如 postgres 图表包含:

First: the release name. When installing a chart, you specify a name which helm uses to create a release. This release name is often referenced within a chart to properly isolate chart installs from each other? For example the postgres chart contains:

{{- define "postgresql.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

然后用于服务:

metadata:
  name: {{ template "postgresql.fullname" . }}

它在 kubernetes 中最终看起来确实像myrelease-postgresql".我想知道一个好的发布名称是什么?什么通常用于此?厌恶?或者一些代号,比如 ubuntu 版本?

It does look like "myrelease-postgresql" in the end in kubernetes.I wonder what a good release name is? What is typically used for this? A version? Or some code-name like the ubuntu releases?

第二:引用值.

我的图表使用 postgresql 作为子图表.我不想重复创建 postgresql 服务名称值的方式(参见上面的剪辑).

My chart uses postgresql as a sub-chart. I'd like to not duplicate the way the value for the name of the postgresql service is created (see snipped above).

有没有办法可以引用子图表的服务名称或该模板定义 {{ template "postgresql.fullname" .}} 在父图表中?我需要它作为数据库主机将它传递到我的服务中(如果我对所有内容进行硬编码,它就可以工作,但这不是这个意思).

Is there a way I can reference the service name of a sub-chart or that template define {{ template "postgresql.fullname" . }} in the parent chart? I need it to pass it into my service as database host (which works if I hardcode everything but that cannot be the meaning of this).

我试过了:

      env:
        - name: DB_HOST
          value: {{ template "mychart.postgresql.fullname" . }}

但这会导致错误消息:

template "mychart.postgresql.fullname" not defined

我见过 Charts 做类似事情的例子,比如 odoo 图表.但在这里,如何创建 postgresql 主机名的逻辑被复制,并在模板中创建了自己的定义.

I've seen examples of Charts doing similar things, like the odoo chart. But in here that logic how the postgresql host name is created is copied and an own define in the template is created.

那么有没有办法访问子图表名称?还是值或模板定义?

So is there a way to access sub-chart names? Or values or template defines?

谢谢!

经过一些挖掘后更新:根据 子图表和全局,模板在图表之间共享.

Update after some digging:According to Subcharts and Globals the templates are shared between charts.

所以我能做的是:

在_helpers.tpl 的图表中,我添加(覆盖)postgres 块:

In my chart in _helpers.tpl I add (overwrite) the postgres block:

{{- define "postgresql.fullname" -}}
{{- $name := .Values.global.name -}}
{{- printf "%s-%s" $name "postgresql" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

所以这个值是在部署子图的时候使用的.我无法在此处引用所有值或图表名称,因为它在子图表中会有所不同 - 所以我使用了全局值.

So this value is used when the sub-chart is deployed. I cannot reference all values or the chart name in here as it will be different in the sub-chart - so I used a global value.

这样我就知道子图中创建的服务的价值了.

Like this I know the value of the service that is created in the sub-chart.

不确定这是否是最好的方法:-/

Not sure if this is the best way to do this :-/

推荐答案

您是否将 postgresql 作为图表的子图表(通过图表的 requirements.yaml)?如果是这样,postgresql(子)图表和您的图表都将具有 same .Release.Name - 因此,您可以指定容器的环境为

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 }}

如果您通过将以下内容添加到图表的 values.yaml 来覆盖 postgresql 的名称:

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

postgresql:
  nameOverride: your-postgresql

那么您的容器的 env 将是:

then your container's env would be:

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

这篇关于如何在 kubernetes 的 helm 子图中引用模板中定义的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 13:19