Extract/Patch a File inside a ConfigMap

Extract/Patch a File inside a ConfigMap

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  example.com

  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.