What does the Action ADD in kubernetes-fabric8 watch API mean?

643 Views Asked by At

What does the Action ADD in fabric8 watch API mean? My initial understanding of it is to add a resource, but I tested it and a bunch of ADD Actions were triggered.

1

There are 1 best solutions below

0
On

Yes, Fabric8 Kubernetes Client's WatchEvent basically maps to Kubernetes pkg/watch/json. So EventType in it is basically a String value which is sent by Kubernetes API server as a response and you need to handle what you need to do on the type of event. It can have these values: ADDED, DELETED, MODIFIED, BOOKMARK, ERROR. When you start the watch without any resourceVersion as a parameter, it would just start with resource version set to 0. It starts watch from the beginning and you see ADD actions being triggered for the resources which are already in the cluster. I think you must have seen this behavior when you use kubectl as well:

~/go/src/github.com/fabric8io/kubernetes-client/kubernetes-model/kubernetes-model-core : $ kubectl get pods -w
NAME                                           READY   STATUS    RESTARTS   AGE
<These are pre existing resources listed since start of watch>
nginx-deployment-54f57cf6bf-4swvr              1/1     Running   1   <        2d5h
nginx-deployment-54f57cf6bf-4swvr-clonem6sg6   1/1     Running   1          2d5h
testpod                                        1/1     Running   1          2d5h
testpod-cloneb5fkz                             1/1     Running   1          2d5h

<You should be able to see events after you do some changes>
testpod                                        1/1     Running   1          2d5h

If you go ahead and create some more resources, you should be able to see more ADDED actions being triggered. On modifying, you should be seeing MODIFIED events as well. These are sent by Kubernetes APIServer and Fabric8 Kubernetes Client just deserializes them into Java objects.

So, while watching I would suggest you to handle all the cases depending upon your situation inside the eventReceived() call like this:

// Latch for Watch termination
final CountDownLatch isWatchClosed = new CountDownLatch(1);
try (KubernetesClient client = new DefaultKubernetesClient()) {
    client.pods().inNamespace(namespace).watch(new Watcher<Pod>() {
        @Override
        public void eventReceived(Action action, Pod pod) {
            logger.log(Level.INFO, action.name() + " " + pod.getMetadata().getName());
            switch (action.name()) {
                case "ADDED":
                    logger.log(Level.INFO, pod.getMetadata().getName() + "got added");
                    break;
                case "DELETED":
                    logger.log(Level.INFO, pod.getMetadata().getName() + "got deleted");
                    break;
                case "MODIFIED":
                    logger.log(Level.INFO, pod.getMetadata().getName() + "got modified");
                    break;
                default:
                    logger.log(Level.SEVERE, "Unrecognized event: " + action.name());
            }
        }

        @Override
        public void onClose(KubernetesClientException e) {
            logger.log(Level.INFO, "Closed");
            isWatchClosed.countDown();
        }
    });

    // Wait till watch gets closed
    isWatchClosed.await();
} catch (InterruptedException interruptedException) {
    logger.log(Level.INFO, "Thread Interrupted!");
    Thread.currentThread().interrupt();
}

I hope this clears your understanding of Fabric8's WatchEvent types.