From ad65bb9a926e53e811a896a280f51bac79d2420f Mon Sep 17 00:00:00 2001 From: Brendan Burns <5751682+brendandburns@users.noreply.github.com> Date: Tue, 3 Nov 2026 19:46:55 +0000 Subject: [PATCH 1/2] Initial plan From 30cfc5ccf3b4d7a2226a619c41674d8b8d70d83b Mon Sep 17 00:00:00 2001 From: Brendan Burns <5751682+brendandburns@users.noreply.github.com> Date: Tue, 3 Nov 2026 19:53:18 +0000 Subject: [PATCH 2/2] Fix cross-cluster Spring polling cache leakage (#4974) --- .../configmaps/PollingConfigMapGetter.java | 2 +- .../endpoints/PollingEndpointsGetter.java | 2 +- .../PollingConfigMapGetterTest.java | 81 ++++++++++++++++++ .../endpoints/PollingEndpointsGetterTest.java | 84 +++++++++++++++++++ 4 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 spring/src/test/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetterTest.java create mode 100644 spring/src/test/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetterTest.java diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetter.java b/spring/src/main/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetter.java index 5e8d2e6989..d9249e29c2 100644 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetter.java +++ b/spring/src/main/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetter.java @@ -24,7 +24,7 @@ public class PollingConfigMapGetter implements ConfigMapGetter { - private static final Cache lastObservedConfigMap = + private final Cache lastObservedConfigMap = Caffeine.newBuilder().expireAfterWrite(Duration.ofMinutes(5)).build(); @Autowired private ApiClient apiClient; diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java b/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java index 10ed7f8190..6234a2c4fa 100644 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java +++ b/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java @@ -23,7 +23,7 @@ public class PollingEndpointsGetter implements EndpointsGetter { - private static final Cache lastObservedEndpoints = + private final Cache lastObservedEndpoints = Caffeine.newBuilder().expireAfterWrite(Duration.ofMinutes(5)).build(); private final ApiClient apiClient; diff --git a/spring/src/test/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetterTest.java b/spring/src/test/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetterTest.java new file mode 100644 index 0000000000..cdcdb2bcfb --- /dev/null +++ b/spring/src/test/java/io/kubernetes/client/spring/extended/manifests/configmaps/PollingConfigMapGetterTest.java @@ -0,0 +1,81 @@ +/* +Copyright 2026 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.spring.extended.manifests.configmaps; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.assertj.core.api.Assertions.assertThat; + +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.JSON; +import io.kubernetes.client.openapi.models.V1ConfigMap; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.test.util.ReflectionTestUtils; + +class PollingConfigMapGetterTest { + + @RegisterExtension + static WireMockExtension firstServer = + WireMockExtension.newInstance().options(options().dynamicPort()).build(); + + @RegisterExtension + static WireMockExtension secondServer = + WireMockExtension.newInstance().options(options().dynamicPort()).build(); + + @Test + void cachesAreIsolatedBetweenClients() { + String path = "/api/v1/namespaces/default/configmaps/shared"; + V1ConfigMap firstConfigMap = + new V1ConfigMap() + .metadata(new V1ObjectMeta().namespace("default").name("shared")) + .putDataItem("cluster", "first"); + V1ConfigMap secondConfigMap = + new V1ConfigMap() + .metadata(new V1ObjectMeta().namespace("default").name("shared")) + .putDataItem("cluster", "second"); + firstServer.stubFor( + get(urlPathEqualTo(path)) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody(JSON.serialize(firstConfigMap)))); + secondServer.stubFor( + get(urlPathEqualTo(path)) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody(JSON.serialize(secondConfigMap)))); + + PollingConfigMapGetter firstGetter = new PollingConfigMapGetter(); + PollingConfigMapGetter secondGetter = new PollingConfigMapGetter(); + ReflectionTestUtils.setField( + firstGetter, "apiClient", new ApiClient().setBasePath(firstServer.baseUrl())); + ReflectionTestUtils.setField( + secondGetter, "apiClient", new ApiClient().setBasePath(secondServer.baseUrl())); + + V1ConfigMap firstResult = firstGetter.get("default", "shared"); + V1ConfigMap secondResult = secondGetter.get("default", "shared"); + assertThat(firstResult).isEqualTo(firstConfigMap); + assertThat(secondResult).isEqualTo(secondConfigMap); + assertThat(firstGetter.get("default", "shared")).isSameAs(firstResult); + assertThat(secondGetter.get("default", "shared")).isSameAs(secondResult); + firstServer.verify(1, getRequestedFor(urlPathEqualTo(path))); + secondServer.verify(1, getRequestedFor(urlPathEqualTo(path))); + } +} diff --git a/spring/src/test/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetterTest.java b/spring/src/test/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetterTest.java new file mode 100644 index 0000000000..836c7b320c --- /dev/null +++ b/spring/src/test/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetterTest.java @@ -0,0 +1,84 @@ +/* +Copyright 2026 The Kubernetes Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package io.kubernetes.client.spring.extended.network.endpoints; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.assertj.core.api.Assertions.assertThat; + +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; +import io.kubernetes.client.openapi.ApiClient; +import io.kubernetes.client.openapi.JSON; +import io.kubernetes.client.openapi.models.V1EndpointAddress; +import io.kubernetes.client.openapi.models.V1EndpointSubset; +import io.kubernetes.client.openapi.models.V1Endpoints; +import io.kubernetes.client.openapi.models.V1ObjectMeta; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +class PollingEndpointsGetterTest { + + @RegisterExtension + static WireMockExtension firstServer = + WireMockExtension.newInstance().options(options().dynamicPort()).build(); + + @RegisterExtension + static WireMockExtension secondServer = + WireMockExtension.newInstance().options(options().dynamicPort()).build(); + + @Test + void cachesAreIsolatedBetweenClients() { + String path = "/api/v1/namespaces/default/endpoints/shared"; + V1Endpoints firstEndpoints = + new V1Endpoints() + .metadata(new V1ObjectMeta().namespace("default").name("shared")) + .addSubsetsItem( + new V1EndpointSubset() + .addAddressesItem(new V1EndpointAddress().ip("10.0.0.1"))); + V1Endpoints secondEndpoints = + new V1Endpoints() + .metadata(new V1ObjectMeta().namespace("default").name("shared")) + .addSubsetsItem( + new V1EndpointSubset() + .addAddressesItem(new V1EndpointAddress().ip("10.0.0.2"))); + firstServer.stubFor( + get(urlPathEqualTo(path)) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody(JSON.serialize(firstEndpoints)))); + secondServer.stubFor( + get(urlPathEqualTo(path)) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBody(JSON.serialize(secondEndpoints)))); + + PollingEndpointsGetter firstGetter = + new PollingEndpointsGetter(new ApiClient().setBasePath(firstServer.baseUrl())); + PollingEndpointsGetter secondGetter = + new PollingEndpointsGetter(new ApiClient().setBasePath(secondServer.baseUrl())); + + V1Endpoints firstResult = firstGetter.get("default", "shared"); + V1Endpoints secondResult = secondGetter.get("default", "shared"); + assertThat(firstResult).isEqualTo(firstEndpoints); + assertThat(secondResult).isEqualTo(secondEndpoints); + assertThat(firstGetter.get("default", "shared")).isSameAs(firstResult); + assertThat(secondGetter.get("default", "shared")).isSameAs(secondResult); + firstServer.verify(1, getRequestedFor(urlPathEqualTo(path))); + secondServer.verify(1, getRequestedFor(urlPathEqualTo(path))); + } +}