Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert YAML to JSON when saving files into container using Kubernetes Configmap

We are going to write Helm chart and providing configuration file using configmap.

For some reasons our app is using JSON format configuration file. Currently we provide configuration file in Helm chart's values.yaml like this.

conffiles:
  app_conf.json:
    ...(content in YAML)...

And to make it easy to modify, in values.yaml we use YAML format and in configmap's template we did conversion using "toJson",

data:
{{- range $key, $value := .Values.conffiles }}
  {{ $key }}: |      
{{ toJson $value | default "{}" | indent 4 }}
{{- end -}}
{{- end -}}

So in values.yaml it's YAML, and in configmap it will be JSON, then in container it will be stored into JSON file.

Our question is,

  • Is there a way to convert YAML to JSON when saving files into container? That is, we hope those configuration content could be 1) YAML in values.yaml 2) YAML in configmap 3) JSON file in container

Thank in advance.

like image 493
Lijing Zhang Avatar asked Jan 16 '19 02:01

Lijing Zhang


1 Answers

I don't think there is anything out of the box but you do have options, depending upon your motivation.

Your app is looking for json and the configmap is mounted for your app to read that json. Your helm deployment isn't going to modify the container itself. But you could change your app to read yaml instead of json.

If you want to be able to easily see the yaml and json versions you could create two configmaps - one containing yaml and one with json.

Or if you're just looking to be able to see what the yaml was that was used to create the configmap then you could use helm get values <release_name> to look at the values that were used to create that release (which will include the content of the conffiles entry).

like image 152
Ryan Dawson Avatar answered Sep 20 '22 10:09

Ryan Dawson