如何在kubernetes掌舵的子图中引用模板中定义的值? [英] How to reference a value defined in a template in a sub-chart in helm for kubernetes?

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

问题描述

我开始为我们的服务编写头盔图表.

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.

首先:发行版名称.安装图表时,您可以指定一个名称,舵使用该名称来创建发行版.经常在图表中引用此发行​​版名称,以正确地将图表安装彼此隔离?例如,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).

有没有一种方法可以引用子图表的服务名称或模板定义{{模板"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" . }}

但这会导致出现错误消息:

But that lead into an error message:

template "mychart.postgresql.fullname" not defined

我看过图表做类似事情的示例,例如

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(子)图表和您的图表都将具有相同 .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

那么您的容器的环境将是:

then your container's env would be:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆