Extract/Patch a File inside a ConfigMap

ConfigMap is excellent for virtually mounting multiple files inside a container.

Imagine you have a ConfigMap with multiple files inside, and you want to extract or modify just one of them. Here is an example of a ConfigMap with multiple files embedded inside it.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  config.json: |
    {
      "server": "0.0.0.0",
      "feature": true
    }

  hosts: |
    127.0.0.1  localhost
    10.10.0.1  example.com

  app.conf: |
    [main]
    appName=Awesome App

To extract a single file, such as config.json, run the following command.

kubectl get cm app-config \
  -o go-template='{{index .data "config.json"}}' > config.json

To update a single file, like app.conf, inside the ConfigMap, run the following command.

kubectl create cm app-config \
  --from-file=app.conf=app.conf \
  --dry-run=client -o yaml | \
kubectl patch cm app-config \
  --type merge \
  --patch-file /dev/stdin

I hope that helps.

0
Subscribe to my newsletter

Read articles from Md. Minhazul Haque directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md. Minhazul Haque
Md. Minhazul Haque

DevOps Engineer | Kubernetes | Python | Terraform | AWS | GCP