简体   繁体   中英

Helm3 - Reading json file into configmap produces a string?

Problem:

I want to read a json file into a configmap so it looks like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: json-test
data:
  test.json: |-
    {
      "key": "val"
    }

Instead I get

apiVersion: v1
kind: ConfigMap
metadata:
  name: json-test
data:
  test.json: |-
      "{\r\n    \"key\": \"val\"\r\n}"

What I've done:

I have the following helm chart:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2020-02-06  10:51 AM                static
d-----        2020-02-06  10:55 AM                templates
-a----        2020-02-06  10:51 AM             88 Chart.yaml

static/ contains a single file: test.json :

{
    "key": "val"
}

templates/ contains a single configmap that reads test.json: test.yml :

apiVersion: v1
kind: ConfigMap
metadata:
  name: json-test
data:
  test.json: |-
  {{ toJson ( .Files.Get "static/test.json" ) | indent 4}}

When I run helm install test. --dry-run --debug helm install test. --dry-run --debug I get the following output

NAME: test
LAST DEPLOYED: Thu Feb  6 10:58:18 2020
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
{}

HOOKS:
MANIFEST:
---
# Source: sandbox/templates/test.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: json-test
data:
  test.json: |-
      "{\r\n    \"key\": \"val\"\r\n}"

The problem here is my json is wrapped in double quotes. My process that wants to read the json is expecting actual json, not a string.

I see that this is not specific behavior only for helm 3 . It generally works in kubernetes this way.

I've just tested it on kubernetes v1.13 .

First I created a ConfigMap based on this file:

apiVersion: v1
kind: ConfigMap
metadata:
  name: json-test
data:
  test.json: |-
    {   
      "key": "val"
    }

When I run:

$ kubectl get configmaps json-test -o yaml

I get the expected output:

apiVersion: v1
data:
  test.json: |-
    {
      "key": "val"
    }
kind: ConfigMap
metadata:
...

but when I created my ConfigMap based on json file with the following content:

{   
      "key": "val"
} 

by running:

$ kubectl create configmap json-configmap --from-file=test-json.json

Then when I run:

kubectl get cm json-configmap --output yaml

I get:

apiVersion: v1
data:
  test-json.json: "    {   \n      \"key\": \"val\"\n    } \n"
kind: ConfigMap
metadata:
...

So it looks like it's pretty normal for kubernetes to transform the original json format into string when a ConfigMap is created from file.

It doesn't seem to be a bug as kubectl doesn't have any problems with extracting properly formatted json format from such ConfigMap :

kubectl get cm json-configmap -o jsonpath='{.data.test-json\.json}'

gives the correct output:

{   
  "key": "val"
}

I would say that it is application responsibility to be able to extract json from such string and it can be done probably in many different ways eg making direct call to kube-api or using serviceaccount configured to use kubectl in Pod .

I hade the samed problem with the format, also when creating the configmap from a yaml file. The fix for me was to remove all trailing whitspace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM