Kostrahb
Kostrahb

Reputation: 741

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?

Upvotes: 27

Views: 68975

Answers (6)

2x_sham_y
2x_sham_y

Reputation: 89

helm has introduce a --set-json flag for this purpose refer to https://helm.sh/docs/helm/helm_install/

Upvotes: 0

Marc
Marc

Reputation: 5508

You can do it from the yaml file pretty easy like this:

# ... values.yaml
myspecialvalue:
  somefield: 'some string'
# ... more stuff if you'd like

then inside one of your helm files you can reference it like so:

# ... cron.yaml
kind: CronJob
metadata:
  name: ...
  namespace: ...
spec:
  schedule: ...
  jobTemplate:
    spec:  
      template:
        spec:
          containers:
          - name: some-container  
            image: ...
            command: ...
            env:
            - name: SOME_ENVIRONMENT_VARIABLE
              value: {{ .Values.myspecialvalue | toJson | quote }}

# ... more stuff if you'd like

this will pass the appropriately quoted json string as an environment variable to your pod like so:

'{"somefield": "some string"}'

Upvotes: 1

Eljah
Eljah

Reputation: 5155

Don't forget to escape this JSON:

in Notepad++ regex replace of {{(\w+)}} to {{{{}}$1{{}}}} helped a bit.

Upvotes: 0

Ryan Dawson
Ryan Dawson

Reputation: 12548

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.

Upvotes: 38

Dmytro Leonenko
Dmytro Leonenko

Reputation: 1501

when passing something to --set or --set-string and you don't want helm to interpret it you want to escape every single of=

{
[
,
.
]
}

with backslash. Remember about your shell that might interpret \ so sometimes you might want to \ or use value in single quotas instead. Something like this works for me in bash:

--set airflow.config.AIRFLOW__SECRETS__BACKEND_KWARGS='\{\\\"variables_prefix\\\": \\\"/here-is-my-prefix/'${bamboo_deploy_environment}'/airflow/variables\\\"\, \\\"connections_prefix\\\": \\\"/here-is-my-prefix/'${bamboo_deploy_environment}'/airflow/connections\\\"\}'

Upvotes: 5

Rose
Rose

Reputation: 2809

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.

Upvotes: 7

Related Questions