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

Helm子图表间无法共享模板?求可行实现方案

Helm多子图表协作实现思路

方案1:通过全局值传递计算结果

在platform子图表的模板(比如_helpers.tpl)里,把计算好的平台相关值注入到全局Values中,让service子图表可以直接引用:

  • platform模板示例:
    {{- $_ := set .Values "platform" dict -}}
    {{- $_ := set .Values.platform "imageRegistryBase" (根据命名空间计算地址的逻辑) -}}
    {{- $_ := set .Values.platform "httpProxy" (代理配置计算逻辑) -}}
    
  • service子图表中直接调用:{{ .Values.platform.imageRegistryBase }}

方案2:将platform封装为纯库图表

把platform改成仅提供函数的库图表(无需生成K8s资源),让其模板函数全局可访问:

  1. 在platform的Chart.yaml中声明类型:
    type: library
    
  2. 在platform的_helpers.tpl中定义全局函数:
    {{- define "platform.imageRegistryBase" -}}
      {{- /* 根据命名空间前缀判断环境 */ -}}
      {{- $namespaceParts := splitList "-" .Release.Namespace -}}
      {{- if eq (index $namespaceParts 0) "prod" -}}
        prod-registry.example.com
      {{- else -}}
        test-registry.example.com
      {{- end -}}
    {{- end -}}
    
  3. service子图表中直接引用该函数:{{ include "platform.imageRegistryBase" . }}

方案3:伞形图表做中间层转发

由伞形图表调用platform的计算逻辑,再将结果传递给service子图表:

  • 伞形图表的values.yaml中通过tpl传递计算值:
    service:
      imageRegistryBase: "{{ tpl (include "platform.imageRegistryBase" .) . }}"
      httpProxy: "{{ tpl (include "platform.httpProxy" .) . }}"
    
  • service子图表直接使用自身Values中的配置:{{ .Values.imageRegistryBase }}

不建议硬编码的原因

硬编码会大幅提升维护成本,一旦平台规则(比如镜像仓库地址、代理配置)变更,所有伞形图表都需要逐一修改;而通过子图表或库函数的方式,只需更新platform图表即可统一生效,更符合可复用的设计初衷。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 21:12:13