Md. Minhazul Haque
Minhaz's Blog

Minhaz's Blog

Extract/Patch a File inside a ConfigMap

Extract/Patch a File inside a ConfigMap

Md. Minhazul Haque's photo
Md. Minhazul Haque
·May 13, 2022·

1 min read

ConfigMap works great when you need to mount several files inside a container virtually.

Imagine you have a ConfigMap (like the following) with multiple files inside it and you want to extract/patch a single one of them.

apiVersion: v1
kind: ConfigMap
  name: app-config
  config.json: |
      "server": "",
      "feature": true

  hosts: |  localhost

  app.conf: |
    appName=Awesome App

To extract a single file say config.json, run the following.

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

To patch a single file say app.conf inside the ConfigMap run the following.

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

Hope that helps.

Share this