Understanding Kubernetes Watch (kubernetes-client/java api)

5/17/2018

I'm using kubernetes-client/java api and I want to programmatically get the pod status of all the pods in all the namespaces. My code is based on this example on Kubernetes java library.

Here's a snippet of my code:

Watch<V1Pod> watch = Watch.createWatch(
        client,
        api.listPodForAllNamespacesCall(
            null, null, null, null, limit,
            null, null, null, watchTrue,
            null, null),
        new TypeToken<Watch.Response<V1Pod>>() {}.getType());

for (Watch.Response<V1Pod> item : watch) {
    V1PodStatus podStatus = item.object.getStatus();
    String name = item.object.getMetadata().getName();
    String status = podStatus.getPhase();
    String kind = item.object.getKind();
    String details = podStatus.toString();

    System.out.printf("NAME: %s | KIND: %s | STATUS: %s | DETAILS: %n%s%n====================%n", name, kind, status, details);
}

My question is this: Is Watch equivalent to an event handler? This code shows me a list of all the statuses of the pods, but will it automatically "push" more pod status events as they occur in realtime? Or is this only triggered once?

-- BlueChips23
java
kubernetes

1 Answer

5/18/2018

Watch is designed to send continuous updates. If you run your program for a while and start/stop something, you will see new updates coming.

I'm not sure it's correct to call it an event handler though. It's a different pattern.

-- Alexandr Lurye
Source: StackOverflow