如何将整个 JSON 字符串传递给 Helm 图表值? [英] How to pass entire JSON string to Helm chart value?

查看:36
本文介绍了如何将整个 JSON 字符串传递给 Helm 图表值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整个 JSON 字符串传递给 Helm 图表值?

How can I pass the entire JSON string to a Helm chart value?

我有 values.yml,其中配置值应包含整个 JSON 和应用程序的配置

I have values.yml where the config value should contain entire JSON with a configuration of an application

...
config: some JSON here
...

我需要将此值传递给一个秘密模板,然后将它作为一个卷挂载到 Kubernetes pod.

and I need to pass this value to a secret template and then mount it as a volume to a Kubernetes pod.

{{- $env := default "integration" .Values.env}}
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-{{ $env }}
type: Opaque
data:
  config.json: {{ .Values.config | b64enc | quote }}

然而,传递单引号字符串(如 '{"redis": "localhost:6379"}' 的明显方法失败了,因为 Helm 由于某种原因删除了字符串中的所有双引号(即使我转义它们)所以我最终得到 {redis: localhost:6379} 这不是有效的 JSON.

However the obvious approach of passing single quoted string like '{"redis": "localhost:6379"}' fails because Helm for some reason deletes all double quotes in the string (even if I escape them) so I end up with {redis: localhost:6379} which is not a valid JSON.

是否有任何其他可能性如何一次将配置传递给 pod,而无需使用 tpl 函数加载模板文件,并分别通过 values.yml 访问所有需要的字段?

Is there any other possibility how to pass configuration to the pod all at once without loading template files with tpl function and making all needed fields accessible via values.yml separately?

推荐答案

如果 .Values.config 包含 json 那么你可以在你的模板化秘密中使用它

If .Values.config contains json then you can use it in your templated secret with

{{ .Values.config | toJson | b64enc | quote }}

使用 toJson 将 JSON 转换为 JSON 似乎很奇怪,但是 helm 在您告诉它之前不会本机将其视为 JSON.请参阅 SO 问题 如何使用 json 变量在 yaml 文件 (Helm) 中 以获取执行此操作的示例.

It may seem strange to use toJson to convert JSON to JSON but helm doesn't natively treat it as JSON until you tell it to. See the SO question How do I use json variables in a yaml file (Helm) for an example of doing this.

这篇关于如何将整个 JSON 字符串传递给 Helm 图表值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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