Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active January 20, 2024 19:40
Show Gist options
  • Save tuxfight3r/e18fdc23f303c301d16859af36a67169 to your computer and use it in GitHub Desktop.
Save tuxfight3r/e18fdc23f303c301d16859af36a67169 to your computer and use it in GitHub Desktop.
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