You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Helm子chart中引用其他子chart的.Release.name或辅助函数?

问题分析与解决方案

核心问题:Helm的values.yaml文件不支持模板语法(如{{ template }}),直接在父chart的values里写模板函数会被当成普通字符串,不会被解析渲染,这是配置未生效的根本原因。

推荐解决方案:直接在subchart-main模板中引用子chart资源

修改subchart-main/templates/configmap.yaml,直接调用子chart的模板或值,无需通过父values中转,这是符合Helm子chart引用逻辑的最简方式:

kind: ConfigMap
data:
  appsettings.json: |-
    {
      "Services": [
        {
          "Name": "{{ template "subchart01.name" .Subcharts.subchart01 }}",
          "Endpoint": "{{ template "subchart01.fullname" .Subcharts.subchart01 }}:5080"
        },
        {
          "Name": "{{ template "subchart02.name" .Subcharts.subchart02 }}",
          "Endpoint": "{{ template "subchart02.fullname" .Subcharts.subchart02 }}:5081"
        }
      ]
    }

注意事项:

  • 确保子chart(subchart01/02)中定义了对应的模板(如subchart01.name、subchart01.fullname);如果子chart没有自定义这些模板,可直接用子chart的Release名称替代:{{ .Subcharts.subchart01.Release.Name }}
  • 修复了原JSON的语法错误:每个键值对末尾需加逗号,字符串类型的Name字段必须用双引号包裹

备选方案:通过父chart模板预渲染值(不推荐)

如果一定要通过父values传递,需在父chart的模板中预渲染好值再传递给subchart-main:

  1. 在父chart的templates/_helpers.tpl中添加模板:
{{- define "parent.service1.name" -}}
{{ template "subchart01.name" .Subcharts.subchart01 }}
{{- end -}}

{{- define "parent.service1.endpoint" -}}
{{ template "subchart01.fullname" .Subcharts.subchart01 }}
{{- end -}}

{{- define "parent.service2.name" -}}
{{ template "subchart02.name" .Subcharts.subchart02 }}
{{- end -}}

{{- define "parent.service2.endpoint" -}}
{{ template "subchart02.fullname" .Subcharts.subchart02 }}
{{- end -}}
  1. 在父chart的templates目录下创建subchart-main-values.yaml,动态设置subchart-main的values:
{{- $_ := set .Values.subchart-main "service1" (dict "name" (include "parent.service1.name" .) "endpoint" (include "parent.service1.endpoint" .)) -}}
{{- $_ := set .Values.subchart-main "service2" (dict "name" (include "parent.service2.name" .) "endpoint" (include "parent.service2.endpoint" .)) -}}

内容的提问来源于stack exchange,提问作者Kent Ho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 08:47:22