Kubernetes Watch - How to update? (Java API)

5/25/2018

I have two tasks that I run in parallel threads and I'm pulling my hair on why the Watch functionality doesn't work. Please let me know if you have any insights.

Task 1: Get the status of the pods and show the current status.

Task 2: Keep a watch on new events. This is what I'm trying to understand better.

Each of these tasks are executed every 30 seconds with scheduledAtFixedRate().

Expected behavior:

Task 1: I should get the list of all the pods with their current status (this works).

Task 2: I should expect list of new events as they happen.

Observed behavior:

Task 1: It works fine. I get updated status of the pods every 30 seconds.

Task 2: It dumps the events from the first request, but it seems like it doesn't update any new events.

Code: Task 1:

@Component
@Scope(value = org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON)
public class Task1 implements Runnable {

    private ScheduledExecutorService scheduledExecutorService;
    private CommandInvoker commandInvoker;
    private static final int INITIAL_DELAY = 15; 
    private static final int POLLING_INTERVAL = 30;

    @Autowired
    public Task1 (CommandInvoker commandInvoker,
                        ScheduledExecutorService scheduledExecutorService) {
        this.commandInvoker = commandInvoker;
        this.scheduledExecutorService = scheduledExecutorService;
        this.scheduledExecutorService.scheduleAtFixedRate(this, INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.SECONDS);
    }

    @Override
    public void run() {
        System.out.println("===== STARTING TASK 1 POD HEALTH CHECK =======");
        commandInvoker.getPodStatus();
    }
}

Task 2:

@Component
@Scope(value = org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON)
public class Task2 implements Runnable {

    private ScheduledExecutorService scheduledExecutorService;
    private CommandInvoker commandInvoker;

    @Autowired
    public Task2(CommandInvoker commandInvoker,
                              ScheduledExecutorService scheduledExecutorService) {
        this.commandInvoker = commandInvoker;
        this.scheduledExecutorService = scheduledExecutorService;
        this.scheduledExecutorService.scheduleAtFixedRate(this, 30, 30, TimeUnit.SECONDS);
    }

    @Override
    public void run() {
        System.out.println("===== STARTING TASK 2 EVENT WATCH UPDATE =======");
        commandInvoker.getWatchUpdates();
    }
}

CommandInvoker:

@Component
public class CommandInvoker {

    public void getPodStatus() {
        try {
            CoreV1Api api = new CoreV1Api();
            V1PodList list = api.listPodForAllNamespaces(null,
                    null, null,  null, null, null, null, null, null);
            for( V1Pod pod : list.getItems() ) {
                  // THIS WORKS //
            }
        } catch ( ApiException e) {
            throw new WhateverException ("Failed to handle watchlist event", e);
        }
    }

    public void getWatchUpdates() {
        CoreV1Api api = new CoreV1Api();
        try {
            Watch<V1Event> watch = Watch.createWatch(
                    apiClient,
                    api.listEventForAllNamespacesCall(null, null, null, null,
                            null, null, null, null, true, null, null),
                    new TypeToken<Watch.Response<V1Event>>() {}.getType());

            watch.forEach( response  -> {
                V1Event event = response.object;
                // THIS ONLY DUMPS EVENTS FROM FIRST CALL BUT NEVER GETS EXECUTED AGAIN
            });
            // I NEVER REACH HERE BUT I DON'T GET ANY UPDATES
        } catch ( ApiException e) {
            throw new K8ServerException("Failed to handle watchlist event", e);
        }
    }
}
-- BlueChips23
java
kubernetes
kubernetes-health-check

0 Answers