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

如何将Map作为Helm tpl字符串传入以配置Service注解?

问题

现有Helm Service模板如下:

apiVersion: v1
kind: Service
metadata:
  name: example
  namespace: {{ .Release.Namespace }}
  annotations:
    {{- if .Values.service.annotations }}
    {{ tpl .Values.service.annotations . | nindent 4 | trim }}
    {{- end }}

(其余内容省略)

在values.yaml中按如下方式配置注解:

service:
  annotations:
    foo: my-foo
    bar: 6
    baz: false

运行Helm时报错:

[ERROR] templates/: template: my-chart/templates/service.yaml:20:18: executing "my-chart/templates/service.yaml" at <.Values.service.annotations>: wrong type for value; expected string; got map[string]interface {}

已知无法修改模板,查阅tpl函数文档仅找到字符串插值或外部文件传值示例,请问该如何配置Service的注解?

解决方案

由于tpl函数仅接受字符串类型的输入,当前配置传入map类型导致类型不匹配报错。需将values.yaml中的service.annotations改为YAML格式的字符串,让tpl函数正确解析,具体有两种配置方式:

  • 多行字符串(可读性更好)

    service:
      annotations: |
        foo: my-foo
        bar: 6
        baz: false
    
  • 单行YAML字符串

    service:
      annotations: 'foo: my-foo, bar: 6, baz: false'
    

这样修改后,tpl函数会将字符串内容作为模板渲染,最终生成符合要求的注解键值对,解决类型错误问题。

内容的提问来源于stack exchange,提问作者Roddy of the Frozen Peas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 13:40:54