fabric8 watch API 中的 Action ADD 是什么意思?我对它的初步理解是添加一个资源,但我测试了它并触发了一堆 ADD 操作。
是的,Fabric8 Kubernetes 客户端的WatchEvent
基本上映射到 Kubernetespkg/watch/json
。所以EventType
它基本上是一个String
值,由 Kubernetes API 服务器作为响应发送,你需要处理你需要对事件类型做的事情。它可以有这些值:ADDED
、DELETED
、MODIFIED
、BOOKMARK
、ERROR
。当您在没有任何resourceVersion
作为参数的情况下启动手表时,它只会从资源版本设置为0
开始。它从一开始就开始监视,您会看到为集群中已有的资源触发ADD
操作。我想当你使用kubectl
时,你一定也看到了这种行为:
~/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
如果继续创建更多资源,则应该能够看到触发的更多ADDED
操作。在修改时,您还应该看到MODIFIED
事件。它们由 Kubernetes APIServer 发送,Fabric8 Kubernetes Client 只是将它们反序列化为 Java 对象。
因此,在观看时,我建议您根据您在eventReceived()
通话中的情况处理所有情况,如下所示:
// 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();
}
我希望这能让你对 Fabric8 的 WatchEvent 类型有更深入的了解。