This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 5776ae01d10e CAMEL-27353: camel-kubernetes - a stopped cluster view no
longer closes the client set on the configuration (#30695)
5776ae01d10e is described below
commit 5776ae01d10e8077cdfdc93d968480c4d3a6b642
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 24 13:16:54 2026 +0530
CAMEL-27353: camel-kubernetes - a stopped cluster view no longer closes the
client set on the configuration (#30695)
KubernetesClusterView.doStop always closed its KubernetesClient, also the
one set with KubernetesConfiguration.setKubernetesClient. That client
belongs to the user and is shared by all the views (groups) of the
cluster service: when one view stopped, the other groups could no longer
renew their lease (Client is closed) and lost the leadership for good,
and a view used again ran on the closed client.
The view now closes only a client it created itself, as
AbstractKubernetesEndpoint does. The test LockTestServer closes the
clients it created, which the views no longer close.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../kubernetes/cluster/KubernetesClusterView.java | 8 +-
.../cluster/KubernetesClusterViewStopTest.java | 3 +-
.../KubernetesClusterViewSuppliedClientTest.java | 165 +++++++++++++++++++++
.../kubernetes/cluster/utils/LockTestServer.java | 13 ++
4 files changed, 186 insertions(+), 3 deletions(-)
diff --git
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterView.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterView.java
index 87a702154a51..e9aae935cbad 100644
---
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterView.java
+++
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterView.java
@@ -47,6 +47,9 @@ public class KubernetesClusterView extends
AbstractCamelClusterView implements C
private CamelContext camelContext;
private final KubernetesClusterService kubernetesCluster;
private KubernetesClient kubernetesClient;
+ // whether the view created kubernetesClient (closed when the view stops),
or the client was set on the
+ // configuration (owned by the user, and shared with the other views of
the cluster service)
+ private boolean closeClient;
private KubernetesConfiguration configuration;
private KubernetesLockConfiguration lockConfiguration;
private KubernetesClusterMember localMember;
@@ -103,6 +106,7 @@ public class KubernetesClusterView extends
AbstractCamelClusterView implements C
@Override
protected void doStart() throws Exception {
if (controller == null) {
+ this.closeClient = configuration.getKubernetesClient() == null;
this.kubernetesClient =
KubernetesHelper.getKubernetesClient(configuration);
final long gen;
synchronized (leaderLock) {
@@ -188,7 +192,9 @@ public class KubernetesClusterView extends
AbstractCamelClusterView implements C
}
stopping.releaseLeadership();
} finally {
- kubernetesClient.close();
+ if (closeClient) {
+ kubernetesClient.close();
+ }
kubernetesClient = null;
if (sharedTimeLeft != null) {
kubernetesCluster.sharedStopTimeSpent(System.nanoTime() -
stopStart);
diff --git
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewStopTest.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewStopTest.java
index 2993436ae5ef..cfcc89c00710 100644
---
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewStopTest.java
+++
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewStopTest.java
@@ -297,8 +297,7 @@ class KubernetesClusterViewStopTest extends
CamelTestSupport {
@Override
protected KubernetesConfiguration setConfigDefaults(
KubernetesConfiguration configuration,
KubernetesLockConfiguration lockConfiguration) {
- // a client for each view, as when the views create their
clients from the master URL (a client set on
- // the configuration is shared by the views, and closed by the
first view that stops)
+ // a client for each view, as when the views create their
clients from the master URL
configuration.setKubernetesClient(server.createClient());
return super.setConfigDefaults(configuration,
lockConfiguration);
}
diff --git
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewSuppliedClientTest.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewSuppliedClientTest.java
new file mode 100644
index 000000000000..27827e344e2e
--- /dev/null
+++
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterViewSuppliedClientTest.java
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.camel.component.kubernetes.cluster;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.fabric8.kubernetes.api.model.coordination.v1.Lease;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import org.apache.camel.cluster.CamelClusterView;
+import org.apache.camel.component.kubernetes.KubernetesConfiguration;
+import
org.apache.camel.component.kubernetes.cluster.utils.ConfigMapLockSimulator;
+import org.apache.camel.component.kubernetes.cluster.utils.LeaseLockSimulator;
+import org.apache.camel.component.kubernetes.cluster.utils.LockTestServer;
+import
org.apache.camel.component.kubernetes.cluster.utils.ResourceLockSimulator;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A {@link KubernetesClient} set on the {@link KubernetesConfiguration} of
the cluster service belongs to the user, and
+ * is shared by all the views of the service: a view that stops must not close
it.
+ */
+class KubernetesClusterViewSuppliedClientTest extends CamelTestSupport {
+
+ private static final int LEASE_TIME_MILLIS = 2000;
+ private static final int RENEW_DEADLINE_MILLIS = 1000;
+ private static final int RETRY_PERIOD_MILLIS = 200;
+ private static final String POD = "mypod1";
+ private static final List<String> GROUPS = List.of("group-a", "group-b");
+
+ private final Map<String, ResourceLockSimulator<?>> simulators = new
HashMap<>();
+ private LockTestServer<?> server;
+ private KubernetesClient client;
+ private KubernetesClusterService service;
+
+ @AfterEach
+ void shutdownLock() {
+ context.stop();
+ if (server != null) {
+ server.destroy();
+ }
+ }
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ @ParameterizedTest
+ @EnumSource(LeaseResourceType.class)
+ void testStoppedViewKeepsTheClientOfTheOtherGroups(LeaseResourceType type)
throws Exception {
+ startGroups(type);
+
+ // the clustered route of group-a is removed while the CamelContext
runs, which stops the view of group-a
+ CamelClusterView stopped = stopView("group-a");
+ assertFalse(stopped.getLocalMember().isLeader());
+
+ // group-b runs on the same client: it keeps the leadership and renews
its lease
+ await().during(2L * LEASE_TIME_MILLIS, TimeUnit.MILLISECONDS)
+ .atMost(3L * LEASE_TIME_MILLIS, TimeUnit.MILLISECONDS)
+ .until(() -> service.isLeader("group-b"));
+ assertEquals(POD, getHolder(type, "group-b"));
+ // and the client of the user still works
+ assertNotNull(client.pods().inNamespace("test").list());
+ }
+
+ @ParameterizedTest
+ @EnumSource(LeaseResourceType.class)
+ void testRestartedViewWorksWithTheSuppliedClient(LeaseResourceType type)
throws Exception {
+ startGroups(type);
+ stopView("group-a");
+
+ // the view is used again (such as the clustered route added again):
it takes the leadership again
+ KubernetesClusterView restarted = service.getView("group-a");
+ assertTrue(restarted.isStarted());
+ await().atMost(3L * LEASE_TIME_MILLIS, TimeUnit.MILLISECONDS).until(()
-> service.isLeader("group-a"));
+ assertEquals(POD, getHolder(type, "group-a"));
+ }
+
+ /**
+ * Starts one pod that leads all the {@link #GROUPS}, with a client set on
the configuration of its cluster service.
+ */
+ private void startGroups(LeaseResourceType type) throws Exception {
+ server = new LockTestServer<>();
+ server.addPod(POD);
+ if (type == LeaseResourceType.Lease) {
+ for (String group : GROUPS) {
+ simulators.put(group, new LeaseLockSimulator("leaders-" +
group));
+ }
+ } else {
+ ResourceLockSimulator<?> configMap = new
ConfigMapLockSimulator("leaders");
+ GROUPS.forEach(group -> simulators.put(group, configMap));
+ }
+ simulators.values().stream().distinct().forEach(server::addSimulator);
+
+ client = server.createClient();
+ KubernetesConfiguration configuration = new KubernetesConfiguration();
+ configuration.setKubernetesClient(client);
+ service = new KubernetesClusterService(configuration);
+ service.setNamespace("test");
+ service.setPodName(POD);
+ service.setLeaseDurationMillis(LEASE_TIME_MILLIS);
+ service.setRenewDeadlineMillis(RENEW_DEADLINE_MILLIS);
+ service.setRetryPeriodMillis(RETRY_PERIOD_MILLIS);
+ service.setJitterFactor(1.1);
+ service.setLeaseResourceType(type);
+ context().addService(service);
+ for (String group : GROUPS) {
+ service.getView(group);
+ }
+ context.start();
+
+ await().atMost(5, TimeUnit.SECONDS).until(() ->
GROUPS.stream().allMatch(service::isLeader));
+ for (String group : GROUPS) {
+ assertEquals(POD, getHolder(type, group));
+ }
+ }
+
+ /**
+ * Releases the view of the group as its last user does (MasterConsumer,
ClusteredRoutePolicy), which stops it.
+ */
+ private CamelClusterView stopView(String group) throws Exception {
+ CamelClusterView view = service.getView(group);
+ // the view is retained twice: by the getView of startGroups, and by
the one above
+ service.releaseView(view);
+ service.releaseView(view);
+ assertTrue(((KubernetesClusterView) view).isStopped(), "view should be
stopped");
+ return view;
+ }
+
+ private String getHolder(LeaseResourceType type, String group) {
+ Object resource = simulators.get(group).getResource();
+ if (type == LeaseResourceType.Lease) {
+ Lease lease = (Lease) resource;
+ return lease != null && lease.getSpec() != null ?
lease.getSpec().getHolderIdentity() : null;
+ }
+ ConfigMap configMap = (ConfigMap) resource;
+ return configMap != null && configMap.getData() != null ?
configMap.getData().get("leader.pod." + group) : null;
+ }
+}
diff --git
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LockTestServer.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LockTestServer.java
index 49f9917d51d9..d661ecff923b 100644
---
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LockTestServer.java
+++
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LockTestServer.java
@@ -19,9 +19,11 @@ package org.apache.camel.component.kubernetes.cluster.utils;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@@ -66,6 +68,9 @@ public class LockTestServer<T extends HasMetadata> extends
KubernetesMockServer
private Map<String, ResourceLockSimulator<T>> simulators;
+ // the clients created by createClient: a cluster view does not close a
client set on its configuration
+ private final List<NamespacedKubernetesClient> clients = new
CopyOnWriteArrayList<>();
+
public LockTestServer() {
this(Collections.emptySet());
}
@@ -109,9 +114,17 @@ public class LockTestServer<T extends HasMetadata> extends
KubernetesMockServer
RequestConfig requestConfig =
namespacedKubernetesClient.getConfiguration().getRequestConfig();
requestConfig.setRequestRetryBackoffInterval(1000);
requestConfig.setRequestRetryBackoffLimit(0);
+ clients.add(namespacedKubernetesClient);
return namespacedKubernetesClient;
}
+ @Override
+ public void destroy() {
+ clients.forEach(NamespacedKubernetesClient::close);
+ clients.clear();
+ super.destroy();
+ }
+
public void addSimulator(ResourceLockSimulator<?> paramLockSimulator) {
ResourceLockSimulator<T> lockSimulator = (ResourceLockSimulator<T>)
paramLockSimulator;
if (this.simulators.containsKey(lockSimulator.getResourceName())) {