How to patch a ConfigMap in Kubernetes

Muhammad Rehan Saeed picture Muhammad Rehan Saeed · Feb 7, 2019 · Viewed 21.4k times · Source

Kubernetes ships with a ConfigMap called coredns that lets you specify DNS settings. I want to modify or patch a small piece of this configuration by adding:

apiVersion: v1
kind: ConfigMap
data:
  upstreamNameservers: |
    ["1.1.1.1", "1.0.0.1"]

I know I can use kubectrl edit to edit the coredns ConfigMap is there some way I can take the above file containing only the settings I want to insert or update and have it merged on top of or patched over the existing ConfigMap?

The reason for this is that I want my deployment to be repeatable using CI/CD. So, even if I ran my Helm chart on a brand new Kubernetes cluster, the settings above would be applied.

Answer

Jordan Liggitt picture Jordan Liggitt · Feb 9, 2019

This will apply the same patch to that single field:

kubectl patch configmap/coredns \
  -n kube-system \
  --type merge \
  -p '{"data":{"upstreamNameservers":"[\"1.1.1.1\", \"1.0.0.1\"]"}}'