39

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.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

5 Answers5

50

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\"]"}}'
Jordan Liggitt
  • 16,933
  • 2
  • 56
  • 44
  • 2
    What if I wanted to add this into a Helm chart somehow? – Muhammad Rehan Saeed Feb 09 '19 at 14:59
  • 4
    Hi @MuhammadRehanSaeed, Helm is for deploying and managing 'artifacts' or self contained systems. I don't think it's appropriate to use Helm to modify system configs. – Scott Boring Jul 17 '19 at 20:05
  • 1
    This answer is correct; but, there are more ways to patch a resource. See: https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/ – Scott Boring Jul 17 '19 at 20:07
  • This is invaluable for sooo many use cases. It's a pity this cannot be found on the docs. Thank you! – RndmSymbl May 03 '22 at 07:45
  • @ Jordan Liggitt this way doesn't work if the whole config under data field is in form of embedded json string. – esahmo Jan 17 '23 at 19:13
  • I am not sure if this will also work for file-like keys. This works perfectly for property-like keys. – Shaswat Lenka Apr 12 '23 at 07:30
  • @esahmo for that case I prefer to use `jq`. E.g. ```kubectl get configmap/coredns -n kube-system -o json | jq '.data.Corefile += "localnet:53 {\n errors\n cache 30\n forward . '"$(minikube ip)"'\n}\n"' | kubectl apply -f -``` – rmarques May 16 '23 at 13:31
3

you should try something like this:

kubectl get cm some-config -o yaml | run 'sed' commands to make updates | kubectl create cm some-config -o yaml --dry-run | kubectl apply -f - 
aurelius
  • 3,433
  • 1
  • 13
  • 22
P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
1

you can edit it using vi as follows:

    kubectl edit cm -n kube-system coredns 

or you can export it to apply any changes using kubectl get cm -n kube-system -o yaml --export then use kubectl apply -f fileName.yaml to apply your changes

Amr Nassar
  • 155
  • 2
  • 9
0

As ConfigMaps are used to mount configuration files to Pod, it seems like this is what you are looking for. ConfigMaps inside of containers will update automatically if the underlying ConfigMap or Secret is modified.

You can specify configMap location:

configMapVolume(mountPath: '/etc/mount3', configMapName: 'my-config'),

Update:

Ok, I guess this does not solve your issue. Other thing that comes to my mind is kubectl create configmap with a pipe to kubectl replace So the whole command would look like this:

kubectl create configmap NAME --from-file file.name -o yaml --dry-run | kubectl replace -f -

Note that this replaces whole file, so just replace should work too.

aurelius
  • 3,433
  • 1
  • 13
  • 22
-2

The configmap can be patched using following kubectl command.

 kubectl patch configmap your-configmap-name -n your-namespace --patch '{"data": {"key": "new-value"}}'
  • 2
    Ain't this the same command as in the top answer where Jordan Liggitt additionally made the effort to fill in the attributes that would work for OP? – Jan Vítek Jun 01 '23 at 06:11