如何从具有正确上下文的子图中调用头盔“助手"模板? [英] How can you call a helm 'helper' template from a subchart with the correct context?

查看:176
本文介绍了如何从具有正确上下文的子图中调用头盔“助手"模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helm 图表在_helpers.tpl中定义了帮助程序模板,用于创建服务的规范化名称.服务(DNS)名称模板的标准格式为:

Helm charts define helper templates in _helpers.tpl which are used to create normalized names for the services. The standard form of the template for a service (DNS) name is:

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

如果使用postgresql作为子图,应该如何使用基于DNS的服务发现来引用它?一种常见的模式似乎是将子图助手复制到父图表中.

If using postgresql as a subchart, how are you supposed to use DNS-based service discovery to refer to it? A common pattern seems to be to copy the subchart helpers into the parent chart.

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

出于显而易见的原因,这完全是疯狂!

This is total madness for obvious reasons!!!

肯定有更好的方法来使用子图助手吗?如果您可以创建上下文对象,则可以调用它,例如:

Surely there's a better way to use the subchart helper? If you could create a context object then it would be possible to invoke it, something like:

value: {{ template "postgresql.fullname" ({Chart: {Name: 'not-used'}, Release: {Name: .Release.Name}, Values: { nameOverride: .Values.postgresql.nameOverride}}) }}

可悲的是,我不知道如何动态地创建这样的上下文.如果辅助函数更改为引用新属性,但这仍然会中断,但是很明显.

Sadly I have no idea how to actually create such a context dynamically. This would still break if the helper function changed to reference new properties, but in an obvious way.

或者,从子图中提供服务名称的另一种方法?

Alternatively, a different way to make available the service name from a subchart?

推荐答案

我写了一个问题 总结了现状并提出了对Helm的增强功能,可以解决这种情况.

I wrote an issue helm/helm#4535 that summarizes the status-quo and proposes an enhancement to Helm which will solve this case.

对于任何寻求临时解决方案的人,我写过(请参阅我的后续行动注释以获取详细信息)一个元模板,该模板调用"ersatz"子图表范围内的任何给定模板.它通过合成点对象来工作.这不是完美的(并非所有字段都是综合的),但可以做到:

For anyone looking for an interim solution, I wrote (see my follow-up comment for details) a meta-template that calls any given template in an "ersatz" subchart's scope. It works by synthesizing a dot-object. It is not perfect (not all fields are synthesized), but it will do:

{{- define "call-nested" }}
{{- $dot := index . 0 }}
{{- $subchart := index . 1 }}
{{- $template := index . 2 }}
{{- include $template (dict "Chart" (dict "Name" $subchart) "Values" (index $dot.Values $subchart) "Release" $dot.Release "Capabilities" $dot.Capabilities) }}
{{- end }}

用法(调用redis子图表的redis.fullname模板):

Usage (to call a redis.fullname template of a redis subchart):

{{ include "call-nested" (list . "redis" "redis.fullname") }}

这篇关于如何从具有正确上下文的子图中调用头盔“助手"模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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