fabric8io / kubernetes-client

Java client for Kubernetes & OpenShift
http://fabric8.io
Apache License 2.0
3.41k stars 1.46k forks source link

How to use patch on custom resources? #2166

Closed csviri closed 4 years ago

csviri commented 4 years ago

Hi, I would like to add / delete some attributes using patch on custom resources. However its not clear to me how this should work using current API. (In my case I would like to add a finalizer)
This is the API I try to use

MyCustomResource resource = createMyCustomResourcePatchPojo() ;
MixedOperation  resourceOperation = .... ; 
resourceOperation.inNamespace(resource.getMetadata().getNamespace())
   .withName(resource.getMetadata().getName())
   .patch(resource);

I assume this should be then a merge patch like defined here: https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment

And in this RFC: https://tools.ietf.org/html/rfc7386

Does this mean that I should only set values that will be added / altered in the custom resource POJO? And other values should be null? But then how should I delete a field?

Thanks!

(also Is there a way to use similar to Json-P ?)

rohanKanojia commented 4 years ago

I think you can try createOrReplace() here is an example: https://github.com/fabric8io/kubernetes-client/blob/a97aeb90b2c8f0a1e3623429d59101d98912f6e4/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/TypedCustomResourceApiTest.java#L92-L94

rohanKanojia commented 4 years ago

We have a typeless api also with which you can use plain json/HashMap to deal with Custom Resources: https://github.com/fabric8io/kubernetes-client/blob/master/doc/CHEATSHEET.md#customresource-typeless-api

csviri commented 4 years ago

@rohanKanojia the createOrReplace is what I want to avoid. Since its not a patch it can lead to "lost update problem" if I don't use optimistic locking (i.e. lock version) - what I want to also avoid here.

(Also note that, kubernetes api supports https://tools.ietf.org/html/rfc6902, which I need to use in this case since want to do additive operation on a list)

But will check the typeless api, thank you for now!

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

janotav commented 3 years ago

@csviri have you managed to figure out how to safely patch using this library?

csviri commented 3 years ago

@janotav no. (We took other appraoch for unrelated reasons.)