首页 > 解决方案 > 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,

Thank in advance.

标签: kubernetesconfigmapkubernetes-helm

解决方案


我不认为有什么开箱即用的,但你确实有选择,这取决于你的动机。

您的应用正在寻找 json,并且已安装 configmap 以供您的应用读取该 json。您的 helm 部署不会修改容器本身。但是您可以将您的应用程序更改为读取 yaml 而不是 json。

如果您希望能够轻松查看 yaml 和 json 版本,您可以创建两个配置映射 - 一个包含 yaml,一个包含 json。

或者,如果您只是希望能够查看用于创建 configmap 的 yaml 是什么,那么您可以使用helm get values <release_name>来查看用于创建该版本的值(其中将包括conffiles条目的内容)。


推荐阅读