diff --git a/kubernetes/src/main/java/com/linecorp/armeria/client/kubernetes/endpoints/KubernetesEndpointGroup.java b/kubernetes/src/main/java/com/linecorp/armeria/client/kubernetes/endpoints/KubernetesEndpointGroup.java index b870b3ad805..dc4fb3650ef 100644 --- a/kubernetes/src/main/java/com/linecorp/armeria/client/kubernetes/endpoints/KubernetesEndpointGroup.java +++ b/kubernetes/src/main/java/com/linecorp/armeria/client/kubernetes/endpoints/KubernetesEndpointGroup.java @@ -315,7 +315,7 @@ public void eventReceived(Action action, Service service0) { service = service0; nodePort = nodePort0; - watchPodAsync(service0.getSpec().getSelector()); + watchPodAsync(); break; case DELETED: logger.warn("[{}/{}] service is deleted.", namespace, serviceName); @@ -351,7 +351,7 @@ public void onClose() { } } - private CompletableFuture watchPodAsync(Map selector) { + private CompletableFuture watchPodAsync() { return CompletableFuture.supplyAsync(() -> { final Watch oldPodWatch = podWatch; if (oldPodWatch != null) { @@ -363,7 +363,7 @@ private CompletableFuture watchPodAsync(Map selector) { } final Watch newPodwatch; try { - newPodwatch = watchPod(selector); + newPodwatch = watchPod(); } catch (Exception e) { logger.warn("[{}/{}] Failed to start the pod watcher.", namespace, serviceName, e); return null; @@ -379,7 +379,7 @@ private CompletableFuture watchPodAsync(Map selector) { }, worker); } - private Watch watchPod(Map selector) { + private Watch watchPod() { // Clear the podToNode map before starting a new pod watch. podToNode.clear(); final Watcher watcher = new Watcher() { @@ -425,7 +425,7 @@ public void onClose(WatcherException cause) { logger.info("[{}/{}] Reconnecting the pod watcher...", namespace, serviceName); // TODO(ikhoon): Add a backoff strategy to prevent rapid reconnections when the pod watcher // keeps failing. - watchPodAsync(selector); + watchPodAsync(); } @Override @@ -434,6 +434,9 @@ public void onClose() { } }; + final Service service = this.service; + assert service != null; + final Map selector = service.getSpec().getSelector(); // watch() method will block until the watch connection is established. if (namespace == null) { return client.pods().withLabels(selector).watch(watcher);