From a7436e46db9ec58550dfdc7f67f23c902036e038 Mon Sep 17 00:00:00 2001 From: Ikhun Um Date: Wed, 2 Jul 2025 18:40:49 +0900 Subject: [PATCH] Watch a Kubernetes service from the latest resource version. Motivation: I found out that Fabric8 Kubernetes client uses a list API to establish a watch connection, such as "/api/v1/namespaces//services?fieldSelector=metadata.name=&watch=true". Therefore, the resource version of a service cannot be used to inidicate the last known version. Modifications: - Do not specify a resource vesrion to watch a service. - The latest version is fetched instead. - `watcher.eventReceived()` compares the cached version with the latest version to decide whether an update is needed. Result: You no longer see `WatcherException: too old resource version` when using `KubernetesEndpointGroup`. --- .../kubernetes/endpoints/KubernetesEndpointGroup.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 1631dd1ec35..75f91b116d9 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 @@ -422,11 +422,16 @@ public void onClose() { final Service service = this.service; assert service != null; + + // Fabric8 Kubernetes client uses a list API to establish a watch connection, such as + // "/api/v1/namespaces//services?fieldSelector=metadata.name=&watch=true". + // Therefore, the resource version of a service cannot be used. Instead, it fetches the latest version + // and compares it with the cached version in `watcher.eventReceived()` to decide whether an update is + // needed. if (namespace == null) { - return client.services().withName(serviceName).withResourceVersion(resourceVersion).watch(watcher); + return client.services().withName(serviceName).watch(watcher); } else { - return client.services().inNamespace(namespace).withName(serviceName) - .withResourceVersion(resourceVersion).watch(watcher); + return client.services().inNamespace(namespace).withName(serviceName).watch(watcher); } }