Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r

tuxfight3r/kubect_patch.md

Last active Mar 10, 2021
Embed
What would you like to do?
kubectl configmap json patch

use kubectl to patch configmap via json patch operations (add/remove/replace/test)

## Add a new key to the existing configmap
$ kubectl patch cm demo-app-config --type json --patch '[{ "op": "add", "path": "/data/TEST_VALUE", "value": "test_key" }]'
configmap/demo-app-config patched

$ kubectl get cm demo-app-config -o json | jq .data.TEST_VALUE
"test_key"


##  Test if a key exist on the path
$ kubectl patch cm demo-app-config --type json --patch '[{ "op": "test", "path": "/data/TEST_VALUE", "value": "test_key" }]'
configmap/demo-app-config patched (no change)


## Replace an existing key in the configmap
$ kubectl patch cm demo-app-config --type json --patch '[{ "op": "replace", "path": "/data/TEST_VALUE", "value": "test_key2" }]'
configmap/demo-app-config patched

$ kubectl get cm demo-app-config -o json | jq .data.TEST_VALUE
"test_key2"


## Remove an existing key in the configmap
$ kubectl patch cm demo-app-config --type json --patch '[{ "op": "remove", "path": "/data/TEST_VALUE", "value": "test_key2" }]'
configmap/demo-app-config patched

$ kubectl get cm demo-app-config -o json | jq .data.TEST_VALUE

use kubectl strategic merge patch

$ kubectl patch cm demo-app-config --type strategic --patch '{ "data": { "TEST_VALUE": "test_key2" }}'
configmap/demo-app-config patched

$ kubectl get cm fdp-roroxml-history-app-config -o json | jq .data.TEST_VALUE
"test_key2"

$ kubectl patch cm demo-app-config --patch '{ "data": { "TEST_VALUE": "test_key3" }}'
configmap/demo-app-config patched

$ kubectl get cm demo-app-config -o json | jq .data.TEST_VALUE
"test_key3"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment