Recently, when I tried to answer the question: Openshift: how to edit scc non-interactively? I learned about nice feature of oc patch . It allows to change single values in OpenShift objects.
Patching the resources
For this scenario, let’s say we have a deployment config called paprika. Standard way to update it is to run oc edit dc paprika and change fields. That works only until it is not a repeatable task or something that needs to be done often.
Changing single value
Using oc patch dc paprika --type=json -p ... you can easily automate simple tasks. Setting the rolling update timeout using this approach looks like this:
1 |
oc patch dc paprika --type=json -p='[{"op":"replace", "path":"/spec/strategy/rollingParams/timeoutSeconds", "value":"1200"}]' |
The patch is an array of objects with the following fields:
- op – one of the operations: add, remove, replace, copy, move, test
- from – source path used in copy and move operations
- path – destination path of the operation
- value – new value to be added/set or tested value
More examples
Check if first trigger is imageChange trigger
1 2 3 4 5 6 7 |
oc patch dc jboss-test --type=json -p='[ {"op": "test", "path": "/spec/triggers/0/type", "value": "ImageChange" } ]' "paprika" patched |
And disable the trigger only if the first trigger is ‘ImageChange’ one.
1 2 3 4 5 6 7 8 9 10 |
oc patch dc jboss-test --type=json -p='[ {"op": "test", "path": "/spec/triggers/0/type", "value": "ImageChange" }, {"op": "replace", "path": "/spec/triggers/0/imageChangeParams/automatic", "value": false } ]' "paprika" patched |
Second trigger is ‘ConfigChange’ one, so the test fails and patch is not applied
1 2 3 4 5 6 7 8 9 10 |
oc patch dc jboss-test --type=json -p='[ {"op": "test", "path": "/spec/triggers/1/type", "value": "ImageChange" }, {"op": "replace", "path": "/spec/triggers/1/imageChangeParams/automatic", "value": false } ]' Error from server: Testing value /spec/triggers/1/type failed |
Documentation
The OpenShift documentation about this feature practically doesn’t exist. One example can be found at kubectl patch docs page
You can learn more about json patch syntax at http://jsonpatch.com
Environment
The commands were executed using minishift and the following client/server versions of OpenShift.
Client:
Server:
Newsletter
Thanks for reading the OpenShift morsels. To get updates about new articles, you can sign up to the newsletter below.
As a thank you message, you will also get access to OpenShift CLI CheatSheet listing most commonly used commands together with a short explanation.