Skip to main content

Command Palette

Search for a command to run...

Extract/Patch a File inside a ConfigMap

Updated
1 min read
Extract/Patch a File inside a ConfigMap
M

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

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.

More from this blog

M

Minhaz's Blog

111 posts