How to patch and update a ConfigMap in Kubernetes

8/24/2020

I have a ConfigMap as following:

kind: ConfigMap
apiVersion: v1
metadata:
  name: health-ip
data:
  ip.json: |-
    [
      1.1.1.1,
      2.2.2.2
    ]

I want to modify/append or patch a small piece of this configuration by adding ip 3.3.3.3 to the ConfigMap so that it becomes:

kind: ConfigMap
apiVersion: v1
metadata:
  name: health-ip
data:
  ip.json: |-
    [
      1.1.1.1,
      2.2.2.2,
      3.3.3.3
    ]

How can it do this using kubectl patch or equivalent?

-- maopuppets
kubernetes

1 Answer

8/25/2020

There is no way to add without replace. As mentioned in comment by zerkms, configmaps does not undestand structure data.

You have a couple of options to achive what you want: 1. Keep a "template" file of your configmap, update and apply when you need; 2. Automate the first task with a script that reads the configmap value and append the new value. 3. Use the kubectl path passing the whole ip list.

-- Mr.KoopaKiller
Source: StackOverflow