Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass entire JSON string to Helm chart value?

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?

like image 461
Kostrahb Avatar asked Jan 04 '19 12:01

Kostrahb


People also ask

What is TPL in Helm?

Using the 'tpl' Function The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files.

Can I use JSON in YAML?

One thing that most people might not know is that YAML is a superset of JSON. This means that any JSON is a valid YAML file! YAML just extends the JSON syntax to provide more features (like comments etc) and alternatives to represent the same data structures.


1 Answers

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.

like image 99
Ryan Dawson Avatar answered Sep 27 '22 23:09

Ryan Dawson