Kubernetes-client: watch custom resource

Created on 9 Sep 2018  ·  22Comments  ·  Source: fabric8io/kubernetes-client

How to watch custom resource?

I defined a custom resource definition, and create one resource and then want to watch it but it doesn't enter the eventReceived method. Even i tried the example in kubernetes-client/kubernetes-examples/CRDExample.java, it didn;'t work.

what should i take care when watching custom resource

client.customResources(dubboServiceCRD, DubboService.class, DubboServiceList.class, DoneableDubboService.class)
                    .inNamespace("wuhuhu").withName(serviceName)
                    .watch(new Watcher<DubboService>() {
                        @Override
                        public void eventReceived(Action action, DubboService resource) {
                            System.out.println("receive: pause");
                            System.out.println(action);
                            System.out.println(resource);
                        }

                        @Override
                        public void onClose(KubernetesClientException cause) {
                            System.out.println("close: pause");
                            System.out.println(cause);
                        }
                    });

Most helpful comment

Also.. the CRDs for that repo, and the instructions can be found here: https://github.com/salaboy/extending-k8s-with-spring-cloud

All 22 comments

First, i tried in another cluster and it succeeded, but there was a new problem is when watching the custromResouce, it threw an exception about deserialize, so i follow the source code and found that the class must defined under the special package name:

KubernetesDeserializer.java

PACKAGES = new ArrayList<String>(){{
            add("io.fabric8.kubernetes.api.model.");
            add("io.fabric8.kubernetes.api.model.apiextensions.");
            add("io.fabric8.kubernetes.api.model.apps.");
            add("io.fabric8.kubernetes.api.model.authentication.");
            add("io.fabric8.kubernetes.api.model.authorization.");
            add("io.fabric8.kubernetes.api.model.batch.");
            add("io.fabric8.kubernetes.api.model.extensions.");
            add("io.fabric8.kubernetes.api.model.networking.");
            add("io.fabric8.kubernetes.api.model.policy.");
            add("io.fabric8.kubernetes.api.model.rbac.");
            add("io.fabric8.kubernetes.api.model.storage.");
            add("io.fabric8.openshift.api.model.");
        }};

i created the new package use these names, move class into the package and it succeed.

Are you getting something like this:

Caused by: io.fabric8.kubernetes.client.KubernetesClientException: resourceVersion: Invalid value: "v1beta1": strconv.ParseUint: parsing "v1beta1": invalid syntax
at io.fabric8.kubernetes.client.dsl.internal.WatchConnectionManager$2.onFailure(WatchConnectionManager.java:198)
at okhttp3.internal.ws.RealWebSocket.failWebSocket(RealWebSocket.java:543)
at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:185)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
?

yes i remembered that i saw this.

@WywTed yeah.. that is because the object mapper doesn't have the type.. I will try to create a PR to fix that..

First, i tried in another cluster and it succeeded, but there was a new problem is when watching the custromResouce, it threw an exception about deserialize, so i follow the source code and found that the class must defined under the special package name:

KubernetesDeserializer.java

PACKAGES = new ArrayList<String>(){{
            add("io.fabric8.kubernetes.api.model.");
            add("io.fabric8.kubernetes.api.model.apiextensions.");
            add("io.fabric8.kubernetes.api.model.apps.");
            add("io.fabric8.kubernetes.api.model.authentication.");
            add("io.fabric8.kubernetes.api.model.authorization.");
            add("io.fabric8.kubernetes.api.model.batch.");
            add("io.fabric8.kubernetes.api.model.extensions.");
            add("io.fabric8.kubernetes.api.model.networking.");
            add("io.fabric8.kubernetes.api.model.policy.");
            add("io.fabric8.kubernetes.api.model.rbac.");
            add("io.fabric8.kubernetes.api.model.storage.");
            add("io.fabric8.openshift.api.model.");
        }};

i created the new package use these names, move class into the package and it succeed.

@WywTed Will you be able to elaborate on this please ? Because I am getting the following

ERROR {io.fabric8.kubernetes.client.dsl.internal.WatchConnectionManager} - Could not deserialize watch event

@Shairam Maybe you can try to create a package named with one of these below
~
PACKAGES = new ArrayList(){{
add("io.fabric8.kubernetes.api.model.");
add("io.fabric8.kubernetes.api.model.apiextensions.");
add("io.fabric8.kubernetes.api.model.apps.");
add("io.fabric8.kubernetes.api.model.authentication.");
add("io.fabric8.kubernetes.api.model.authorization.");
add("io.fabric8.kubernetes.api.model.batch.");
add("io.fabric8.kubernetes.api.model.extensions.");
add("io.fabric8.kubernetes.api.model.networking.");
add("io.fabric8.kubernetes.api.model.policy.");
add("io.fabric8.kubernetes.api.model.rbac.");
add("io.fabric8.kubernetes.api.model.storage.");
add("io.fabric8.openshift.api.model.");
}};
~

and put your crd class file under the package, or even you can follow the resource code and debug it.

@WywTed : Is watch for custom resources working for you? If yes, Could you please document how to have watch with Custom resources?

Yes, I got it working as well, an example can. be found here: https://github.com/salaboy/k8s-operator

@WywTed Thank you so much .

@WywTed : Is watch for custom resources working for you? If yes, Could you please document how to have watch with Custom resources?

You can see https://github.com/salaboy/k8s-operator/blob/master/src/main/java/org/salaboy/k8s/operator/AppsOperator.java from salaboy's repository.

@WywTed not the best code ever.. but I hope that it helps to exemplify the watches in action..
also if you guys give me a "star" to the repo I will appreciate it :)

Also.. the CRDs for that repo, and the instructions can be found here: https://github.com/salaboy/extending-k8s-with-spring-cloud

@WywTed Thank you so much .

@Shairam No problem.
I don't try the latest version, maybe it doesn't exist in the latest version.
We've change to use kubernetes-client to operate k8s resource.

@WywTed : Is watch for custom resources working for you? If yes, Could you please document how to have watch with Custom resources?

You can see https://github.com/salaboy/k8s-operator/blob/master/src/main/java/org/salaboy/k8s/operator/AppsOperator.java from salaboy's repository.

This is also helpful. Thank you

@WywTed Thank you so much .

@Shairam No problem.
I don't try the latest version, maybe it doesn't exist in the latest version.
We've change to use kubernetes-client to operate k8s resource.

@WywTed Btw is kubernetes-client a standard kubernetes client to be used for Java ?

@Shairam : Yes, it's the official client. We're discussing to rename our repo to avoid conflict #1452

But our client is well adopted and more popular

@WywTed Thank you so much .

@Shairam No problem.
I don't try the latest version, maybe it doesn't exist in the latest version.
We've change to use kubernetes-client to operate k8s resource.

@WywTed Btw is kubernetes-client a standard kubernetes client to be used for Java ?

@Shairam Yes, it is. From the account info(or organization) kubernetes-client just has been verified by kubernetes.io

Alright thank you @WywTed :) .

But our client is well adopted and more popular

@rohanKanojia Btw is there any documentation for kubernetes-client to perform watch operation for Custom Resources? Could you please suggest me if there is one ?

image
image

KubernetesDeserializer.java
image
image

From the picture, you can see, first you should create package with the special name defined in the source code and put your own CRD class under the special package.

@WywTed : Thanks a lot for helping. @Shairam there is one example : https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/CRDExample.java#L164

But I think it would be nice if we add one example dedicated to watching custom resources which is well documented. Could anyone please raise a PR please?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rohanKanojia picture rohanKanojia  ·  14Comments

iocanel picture iocanel  ·  21Comments

rhuss picture rhuss  ·  15Comments

chirlo picture chirlo  ·  18Comments

kolorful picture kolorful  ·  24Comments