Polling for Pod's ready state

6/22/2018

I am using the fabric8 library for java for deploying appliations on a Kubernetes cluster.

I want to poll the status of pods to know when they are ready. I started writing my own until I read about the Watcher.

I implemented something like this

deployment =
          kubeClient.extensions().deployments().inNamespace(namespaceName).create(deployment);
      kubeClient.pods().inNamespace(namespaceName).watch(new Watcher<Pod>() {
        @Override
        public void eventReceived(io.fabric8.kubernetes.client.Watcher.Action action,
            Pod resource) {
          logger.info("Pod event {} {}", action, resource);
          logger.info("Pod status {} , Reason {} ", resource.getStatus().getPhase(),
              resource.getStatus().getReason());
        }

        @Override
        // What causes the watcher to close?
        public void onClose(KubernetesClientException cause) {
          if (cause != null) {
            // throw?
            logger.error("Pod event {} ", cause);
          }
        }

      });

I m not sure if I understand the Watcher functionality correctly. Does it time out? Or Do I still write my poller inside the eventReceivedMethod()? What is the use case for a watcher?

-- user_mda
fabric8
java
kubernetes
poller

1 Answer

6/23/2018

// What causes the watcher to close?

Since watches are implemented using websockets, a connection is subject to closure at any time for any reason or no reason.

What is the use case for a watcher?

I would imagine it's two-fold: not paying the TCP/IP + SSL connection setup cost, making it quicker, and having your system be event-driven rather than simple polling, which will make every participant use less resources (the server and your client).

But yes, the answer to your question is that you need to have retry logic to reestablish the watcher if you have not yet reached the Pod state you were expecting.

-- mdaniel
Source: StackOverflow