如何通过Bash向Ingress-Nginx Helm Chart传递JSON格式注解
解决Ingress-Nginx Helm Chart传递ConfigMap注解的报错问题
问题原因
你遇到的错误是因为controller.configAnnotations在Chart中定义为map[string]string类型,但你通过--set传递的是一个被HTML转义符"包裹的JSON字符串,Helm将其识别为普通字符串而非期望的map类型,因此无法完成解析。
正确解决方案
方案1:使用Helm原生的--set map语法(推荐)
直接通过键值对形式传递注解,无需JSON格式,Helm会自动解析为map:
helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \ --repo https://kubernetes.github.io/ingress-nginx \ --set controller.configAnnotations.mykey=myval
如果需要传递多个注解,可追加多个--set参数,或用逗号分隔:
helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \ --repo https://kubernetes.github.io/ingress-nginx \ --set controller.configAnnotations.mykey=myval \ --set controller.configAnnotations.another-key=another-value # 或者用逗号分隔 helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \ --repo https://kubernetes.github.io/ingress-nginx \ --set controller.configAnnotations.mykey=myval,controller.configAnnotations.another-key=another-value
方案2:使用JSON格式传递(需正确转义)
如果偏好JSON写法,在Bash中用单引号包裹JSON内容,内部直接使用双引号(无需HTML转义),让Helm识别为map而非字符串:
helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \ --repo https://kubernetes.github.io/ingress-nginx \ --set 'controller.configAnnotations={"mykey":"myval", "another-key":"another-value"}'
方案3:使用自定义values文件(适用于大量配置)
创建一个custom-values.yaml文件,写入注解配置:
controller: configAnnotations: mykey: myval another-key: another-value
然后通过-f参数加载该文件:
helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \ --repo https://kubernetes.github.io/ingress-nginx \ -f custom-values.yaml
内容的提问来源于stack exchange,提问作者lbruun
相关产品推荐
相关产品推荐

