Andrey Kozichev
Andrey Kozichev

Reputation: 31

Cannot patch Kubernetes custom resource from Python SDK

Trying to patch my custom resource to add extra element into an Array.

Works fine using kubectl:

kubectl patch my-resource default --type=json -p '[ { "op":"add", "path": "/spec/data/-", "value": "3342, 43243.343, 434343" } ]' -v 9

but can't make it work using Python:

body = '[ { "op":"add", "path":"/spec/data/-", "value": "3342, 43243.343, 434343" } ]'

api_response = api_instance.patch_namespaced_custom_object(group, version, namespace, plural, name, json.loads(body) )

getting Response

"status":"Failure","message":"json: cannot unmarshal array into Go value of type map[string]interface {}","code":500

weirdly when I drop [ ] and just pass a {}

api_response = api_instance.patch_namespaced_custom_object(group, version, namespace, plural, name, json.loads('{ "spec": { "data": [ "3342, 43243.343, 434343" ] } } ') )

it works, but the effect is not what I want - it overwrites the "data" Array completely, whilst I want to append.

What am I missing?

Upvotes: 2

Views: 2700

Answers (2)

iTayb
iTayb

Reputation: 12743

That's a bug in the Kubernetes python package: https://github.com/kubernetes-client/python/issues/2039

Upvotes: 0

user2283158
user2283158

Reputation: 11

Might be too late for this, but here it goes anyway...

Kubernetes Python API supports JSON-merge patching strategy only: https://github.com/kubernetes-client/python/blob/96dade6021dc2e9ee1430172e1b65d9e9e232b10/kubernetes/client/api/custom_objects_api.py#L2943

That strategy implies you can't extend arrays: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment

Upvotes: 1

Related Questions