-
Notifications
You must be signed in to change notification settings - Fork 14
/
PodWatch.java
66 lines (58 loc) · 2.63 KB
/
PodWatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package io.fabric8;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class PodWatch {
private static final Logger logger = LoggerFactory.getLogger(PodWatch.class.getSimpleName());
public static void main(String[] args) {
String namespace = "default";
// Latch for Watch termination
final CountDownLatch isWatchClosed = new CountDownLatch(1);
try (KubernetesClient client = new KubernetesClientBuilder().build()) {
Watch watch = client.pods().inNamespace(namespace).watch(new Watcher<>() {
@Override
public void eventReceived(Action action, Pod pod) {
switch (action.name()) {
case "ADDED":
logger.info("{}/{} got added", pod.getMetadata().getNamespace(), pod.getMetadata().getName());
break;
case "DELETED":
logger.info("{}/{} got deleted", pod.getMetadata().getNamespace(), pod.getMetadata().getName());
break;
case "MODIFIED":
logger.info("{}/{} got modified", pod.getMetadata().getNamespace(), pod.getMetadata().getName());
break;
default:
logger.error("Unrecognized event: {}", action.name());
}
}
@Override
public void onClose() {
logger.info("Watch closed");
isWatchClosed.countDown();
}
@Override
public void onClose(WatcherException e) {
logger.info("Watched closed due to exception ", e);
isWatchClosed.countDown();
}
});
// Wait till watch gets closed
boolean isTerminatedSuccessfully = isWatchClosed.await(5, TimeUnit.MINUTES);
if (!isTerminatedSuccessfully) {
logger.error("Time out");
}
watch.close();
} catch (InterruptedException interruptedException) {
logger.info( "Thread Interrupted!");
Thread.currentThread().interrupt();
}
}
}