Does Kubernetes take JSON format as input file to create configmap and secret?

user1684651 picture user1684651 · May 7, 2020 · Viewed 10k times · Source

I have an existing configuration file in JSON format, something like below

{
    "maxThreadCount": 10,
    "trackerConfigs": [{
            "url": "https://example1.com/",
            "username": "username",
            "password": "password",
            "defaultLimit": 1
        },
        {
            "url": "https://example2.com/",
            "username": "username",
            "password": "password",
            "defaultLimit": 1
        }
    ],
    "repoConfigs": [{
        "url": "https://github.com/",
        "username": "username",
        "password": "password",
        "type": "GITHUB"
    }],
    "streamConfigs": [{
        "url": "https://example.com/master.json",
        "type": "JSON"
    }]
}

I understand that I am allowed to pass key/value pair properties file with --from-file option for configmap and secret creation.

But How about JSON formatted file ? Does Kubernetes take JSON format file as input file to create configmap and secret as well?

$ kubectl create configmap demo-configmap --from-file=example.json

If I run this command, it said configmap/demo-configmap created. But how can I refer this configmap values in other pod ?

Answer

hoque picture hoque · May 7, 2020

When you create configmap/secret using --from-file, by default the file name will be the key name and content of the file will be the value.

For example, You created configmap will be like

apiVersion: v1
data:
  test.json: |
    {
        "maxThreadCount": 10,
        "trackerConfigs": [{
                "url": "https://example1.com/",
                "username": "username",
                "password": "password",
                "defaultLimit": 1
            },
            {
                "url": "https://example2.com/",
                "username": "username",
                "password": "password",
                "defaultLimit": 1
            }
        ],
        "repoConfigs": [{
            "url": "https://github.com/",
            "username": "username",
            "password": "password",
            "type": "GITHUB"
        }],
        "streamConfigs": [{
            "url": "https://example.com/master.json",
            "type": "JSON"
        }]
    }
kind: ConfigMap
metadata:
  creationTimestamp: "2020-05-07T09:03:55Z"
  name: demo-configmap
  namespace: default
  resourceVersion: "5283"
  selfLink: /api/v1/namespaces/default/configmaps/demo-configmap
  uid: ce566b36-c141-426e-be30-eb843ab20db6

You can mount the configmap into your pod as volume. where the key name will be the file name and value will be the content of the file. like following

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: demo-configmap
  restartPolicy: Never

When the pod runs, the command ls /etc/config/ produces the output below:

test.json