java.net.SocketException 使用 fabric8 kubernetes 客户端和观察器

java.net.SocketException with fabric8 kubernetes client and watcher

我收到 java.net.SocketException: Socket closed 异常 fabric8 kubernerties client version 4.10.2 and Watcher

代码如下:

KubernetesClient client = createClient();
client.pods().watch(createLogWatcher("pod"));
client.apps().deployments().watch(createLogWatcher("deployment"));
client.services().watch(createLogWatcher("service"));
client.extensions().ingresses().watch(createLogWatcher("ingress"));

// do stuff with client

我收到以下异常 因为我的应用程序正在关闭

08:10:37.180 [OkHttp https://test-api.ib.dnsbego.de:6443/...] WARN  i.f.k.c.d.i.WatchConnectionManager - Exec Failure
javax.net.ssl.SSLException: Socket closed
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:320)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:263)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:258)
    at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1315)
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:839)
    at okio.Okio.read(Okio.java:140)
    at okio.AsyncTimeout.read(AsyncTimeout.java:237)
    at okio.RealBufferedSource.request(RealBufferedSource.java:72)
    at okio.RealBufferedSource.require(RealBufferedSource.java:65)
    at okio.RealBufferedSource.readByte(RealBufferedSource.java:78)
    at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:117)
    at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:101)
    at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:273)
    at okhttp3.internal.ws.RealWebSocket.onResponse(RealWebSocket.java:209)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
    Suppressed: java.net.SocketException: Socket closed
        at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
        at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
        at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:351)
        ... 18 common frames omitted
Caused by: java.net.SocketException: Socket closed
    at java.base/java.net.SocketInputStream.socketRead0(Native Method)
    at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
    at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448)
    at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
    at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1104)
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823)
    ... 14 common frames omitted

我自己解决了这个问题,但我想我会 post 给下一个人,因为我找不到任何关于这个的讨论。

每个 watcher(...) 调用 returns 一个可以关闭的 Watch 实例,所以我通过以下方式解决了这个问题:

KubernetesClient client = createClient();
List<Watch> watches = new ArrayList<>();
watches.add(client.pods().watch(createLogWatcher("pod")));
watches.add(client.apps().deployments().watch(createLogWatcher("deployment")));
watches.add(client.services().watch(createLogWatcher("service")));
watches.add(client.extensions().ingresses().watch(createLogWatcher("ingress")));
try {
   // do stuff
} finally {
   watches.forEach(Watch::close);
}