How to pass entire JSON string to Helm chart value?

Deadly 提交于 2019-12-22 08:30:50

问题


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

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

...
config: some JSON here
...

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 }}

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.

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?


回答1:


If .Values.config contains json then you can use it in your templated Secret with {{ .Values.config | toJson | b64enc | quote }}. 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.




回答2:


Here is another suggestion if you want to avoid encoding :

env:
  - name: MYCONFIG
  value: {{ .Files.Get "config.json" | toPrettyJson }}

According to the helm docs, helm uses template functions such as toPrettyJson which are supplied by the built-in Go text/template package and the Sprig template function library.



来源:https://stackoverflow.com/questions/54038837/how-to-pass-entire-json-string-to-helm-chart-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!