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 2bba60b33182 CAMEL-27910: Fix multiple bugs in camel-kubernetes 
(#31201)
2bba60b33182 is described below

commit 2bba60b331829d045e7795b416e4fca013b1495e
Author: Guillaume Nodet <[email protected]>
AuthorDate: Tue Dec 1 23:21:02 2026 +0100

    CAMEL-27910: Fix multiple bugs in camel-kubernetes (#31201)
    
    Fixes three bugs in camel-kubernetes:
    
    - KubernetesJobProducer and KubernetesCronJobProducer threw a bare 
NullPointerException when no operation was set (endpoint option or header). 
They now throw an IllegalArgumentException with a diagnostic message.
    - KubernetesClusterService created with the no-arg constructor discarded 
namespace, masterUrl and connectionTimeout set through their own setters. 
doInit() now applies them to the configuration in both paths.
    - KubernetesCustomResourcesConsumer logged "namespace is not specified" and 
kept retrying with inNamespace(null). It now fails fast with an 
IllegalArgumentException.
    
    Adds tests for the missing operation of both producers, for the cluster 
service namespace with and without an explicit configuration, and for the 
consumer without a namespace.
---
 .../cluster/KubernetesClusterService.java          | 26 ++++--
 .../cronjob/KubernetesCronJobProducer.java         |  9 +-
 .../KubernetesCustomResourcesConsumer.java         |  6 +-
 .../kubernetes/job/KubernetesJobProducer.java      |  9 +-
 .../cluster/KubernetesClusterServiceInitTest.java  | 85 +++++++++++++++++++
 ...ustomResourcesConsumerMissingNamespaceTest.java | 97 ++++++++++++++++++++++
 .../producer/KubernetesCronJobProducerTest.java    | 16 ++++
 .../producer/KubernetesJobProducerTest.java        | 16 ++++
 8 files changed, 239 insertions(+), 25 deletions(-)

diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterService.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterService.java
index 30a0aea8a996..979c01445828 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterService.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterService.java
@@ -249,15 +249,15 @@ public class KubernetesClusterService extends 
AbstractCamelClusterService<Kubern
             configuration = new KubernetesConfiguration();
         } else {
             configuration = configuration.copy();
-            if (masterUrl != null) {
-                configuration.setMasterUrl(masterUrl);
-            }
-            if (namespace != null) {
-                configuration.setNamespace(namespace);
-            }
-            if (connectionTimeout != null) {
-                configuration.setConnectionTimeout(connectionTimeout);
-            }
+        }
+        if (masterUrl != null) {
+            configuration.setMasterUrl(masterUrl);
+        }
+        if (namespace != null) {
+            configuration.setNamespace(namespace);
+        }
+        if (connectionTimeout != null) {
+            configuration.setConnectionTimeout(connectionTimeout);
         }
         if (lockConfiguration == null) {
             lockConfiguration = new KubernetesLockConfiguration();
@@ -398,4 +398,12 @@ public class KubernetesClusterService extends 
AbstractCamelClusterService<Kubern
         this.retryPeriodMillis = retryPeriodMillis;
     }
 
+    /**
+     * Returns the internal {@link KubernetesConfiguration} after {@link 
#doInit()} has run. Package-visible for
+     * testing.
+     */
+    KubernetesConfiguration getConfiguration() {
+        return configuration;
+    }
+
 }
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cronjob/KubernetesCronJobProducer.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cronjob/KubernetesCronJobProducer.java
index 4b78a5991a2e..d3bb8a887336 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cronjob/KubernetesCronJobProducer.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/cronjob/KubernetesCronJobProducer.java
@@ -36,6 +36,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static 
org.apache.camel.component.kubernetes.KubernetesHelper.extractNamespace;
+import static 
org.apache.camel.component.kubernetes.KubernetesHelper.extractOperation;
 import static 
org.apache.camel.component.kubernetes.KubernetesHelper.prepareOutboundMessage;
 
 public class KubernetesCronJobProducer extends DefaultProducer {
@@ -53,13 +54,7 @@ public class KubernetesCronJobProducer extends 
DefaultProducer {
 
     @Override
     public void process(Exchange exchange) throws Exception {
-        String operation;
-
-        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
-            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
-        } else {
-            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
-        }
+        String operation = extractOperation(getEndpoint(), exchange);
 
         switch (operation) {
 
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
index cb93edfe5b6b..e02d2923a6b5 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
@@ -84,10 +84,12 @@ public class KubernetesCustomResourcesConsumer extends 
DefaultConsumer {
 
         @Override
         public void run() {
-            if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getNamespace()))
 {
+            String namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
+            if (ObjectHelper.isEmpty(namespace)) {
                 LOG.error("namespace is not specified.");
+                throw new IllegalArgumentException(
+                        "The kubernetes-custom-resources consumer requires a 
namespace to be specified");
             }
-            String namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
             try {
                 watch = getEndpoint().getKubernetesClient()
                         
.genericKubernetesResources(getCRDContext(getEndpoint().getKubernetesConfiguration()))
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/job/KubernetesJobProducer.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/job/KubernetesJobProducer.java
index 776f2b3c1f2d..85cc4fcb42da 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/job/KubernetesJobProducer.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/job/KubernetesJobProducer.java
@@ -36,6 +36,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static 
org.apache.camel.component.kubernetes.KubernetesHelper.extractNamespace;
+import static 
org.apache.camel.component.kubernetes.KubernetesHelper.extractOperation;
 import static 
org.apache.camel.component.kubernetes.KubernetesHelper.prepareOutboundMessage;
 
 public class KubernetesJobProducer extends DefaultProducer {
@@ -53,13 +54,7 @@ public class KubernetesJobProducer extends DefaultProducer {
 
     @Override
     public void process(Exchange exchange) throws Exception {
-        String operation;
-
-        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
-            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
-        } else {
-            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
-        }
+        String operation = extractOperation(getEndpoint(), exchange);
 
         switch (operation) {
 
diff --git 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterServiceInitTest.java
 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterServiceInitTest.java
new file mode 100644
index 000000000000..65f195e4d543
--- /dev/null
+++ 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/KubernetesClusterServiceInitTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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 org.apache.camel.component.kubernetes.KubernetesConfiguration;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests that masterUrl, namespace and connectionTimeout set directly on 
{@link KubernetesClusterService} are applied to
+ * the resulting {@link KubernetesConfiguration} regardless of whether the 
service was constructed with an explicit
+ * configuration object (CAMEL-27910).
+ */
+class KubernetesClusterServiceInitTest {
+
+    @Test
+    void namespaceAppliedWhenNoExplicitConfiguration() throws Exception {
+        // CAMEL-27910: when configuration == null (no-arg constructor), 
masterUrl/namespace/connectionTimeout
+        // were not propagated — a fresh empty KubernetesConfiguration was 
used without picking them up.
+        KubernetesClusterService service = new KubernetesClusterService();
+        service.setNamespace("test-ns");
+        service.setMasterUrl("http://my-master:8080";);
+        service.setConnectionTimeout(5000);
+        service.setPodName("mypod");
+        service.setLeaseDurationMillis(2000);
+        service.setRenewDeadlineMillis(1000);
+        service.setRetryPeriodMillis(200);
+        service.setJitterFactor(1.1);
+
+        try (DefaultCamelContext ctx = new DefaultCamelContext()) {
+            service.setCamelContext(ctx);
+            service.init();
+
+            KubernetesConfiguration cfg = service.getConfiguration();
+            assertEquals("test-ns", cfg.getNamespace(),
+                    "namespace must be propagated into the configuration even 
when no KubernetesConfiguration was set explicitly");
+            assertEquals("http://my-master:8080";, cfg.getMasterUrl(),
+                    "masterUrl must be propagated into the configuration even 
when no KubernetesConfiguration was set explicitly");
+            assertEquals(Integer.valueOf(5000), cfg.getConnectionTimeout(),
+                    "connectionTimeout must be propagated into the 
configuration even when no KubernetesConfiguration was set explicitly");
+        }
+    }
+
+    @Test
+    void namespaceAppliedWhenExplicitConfiguration() throws Exception {
+        // Existing behaviour must not regress: when configuration != null, 
fields are still applied
+        KubernetesConfiguration baseConfig = new KubernetesConfiguration();
+
+        KubernetesClusterService service = new 
KubernetesClusterService(baseConfig);
+        service.setNamespace("test-ns");
+        service.setMasterUrl("http://my-master:8080";);
+        service.setPodName("mypod");
+        service.setLeaseDurationMillis(2000);
+        service.setRenewDeadlineMillis(1000);
+        service.setRetryPeriodMillis(200);
+        service.setJitterFactor(1.1);
+
+        try (DefaultCamelContext ctx = new DefaultCamelContext()) {
+            service.setCamelContext(ctx);
+            service.init();
+
+            KubernetesConfiguration cfg = service.getConfiguration();
+            assertEquals("test-ns", cfg.getNamespace(),
+                    "namespace must be propagated when an explicit 
configuration is set");
+            assertEquals("http://my-master:8080";, cfg.getMasterUrl(),
+                    "masterUrl must be propagated when an explicit 
configuration is set");
+        }
+    }
+}
diff --git 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumerMissingNamespaceTest.java
 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumerMissingNamespaceTest.java
new file mode 100644
index 000000000000..15a346bb9552
--- /dev/null
+++ 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumerMissingNamespaceTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.customresources;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
+import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
+import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.kubernetes.KubernetesTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Tests that the custom-resources consumer fails fast with an {@link 
IllegalArgumentException} when no namespace is
+ * specified, instead of silently continuing and calling {@code 
inNamespace(null)} (CAMEL-27910).
+ */
+@EnableKubernetesMockClient
+public class KubernetesCustomResourcesConsumerMissingNamespaceTest extends 
KubernetesTestSupport {
+
+    KubernetesMockServer server;
+    NamespacedKubernetesClient client;
+
+    @BindToRegistry("kubernetesClient")
+    public KubernetesClient getClient() {
+        return client;
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    void missingNamespaceThrowsIllegalArgumentException() throws Exception {
+        // CAMEL-27910: when no namespace is set the consumer task used to log 
"namespace is not specified"
+        // and then call inNamespace(null), potentially looping forever.
+        // Now it throws IllegalArgumentException immediately.
+
+        // Build a consumer for an endpoint with no namespace configured
+        KubernetesCustomResourcesEndpoint endpoint
+                = (KubernetesCustomResourcesEndpoint) context.getEndpoint(
+                        "kubernetes-custom-resources:foo"
+                                                                          + 
"?crdName=githubsources.sources.knative.dev"
+                                                                          + 
"&crdGroup=sources.knative.dev"
+                                                                          + 
"&crdScope=Namespaced"
+                                                                          + 
"&crdVersion=v1alpha1"
+                                                                          + 
"&crdPlural=githubsources");
+        // namespace is intentionally not set
+        KubernetesCustomResourcesConsumer consumer
+                = new KubernetesCustomResourcesConsumer(endpoint, e -> {
+                });
+
+        KubernetesCustomResourcesConsumer.CustomResourcesConsumerTask task
+                = consumer.new CustomResourcesConsumerTask();
+
+        ExecutorService exec = Executors.newSingleThreadExecutor();
+        Future<?> future = exec.submit(task);
+        exec.shutdown();
+
+        ExecutionException ex = assertThrows(ExecutionException.class, 
future::get,
+                "Expected the task to fail with an exception when namespace is 
not set");
+        assertInstanceOf(IllegalArgumentException.class, ex.getCause(),
+                "Root cause must be IllegalArgumentException, got: " + 
ex.getCause());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+            }
+        };
+    }
+}
diff --git 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesCronJobProducerTest.java
 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesCronJobProducerTest.java
index e12adb5712fe..90806082779e 100644
--- 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesCronJobProducerTest.java
+++ 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesCronJobProducerTest.java
@@ -208,6 +208,20 @@ public class KubernetesCronJobProducerTest extends 
KubernetesTestSupport {
         assertNotNull(ex.getMessage());
     }
 
+    @Test
+    void missingOperationThrowsIllegalArgumentException() {
+        // CAMEL-27910: switch on null caused NPE; now throws 
IllegalArgumentException
+        Exchange ex = template.request("direct:noOperation", exchange -> {
+        });
+        assertNotNull(ex.getException(), "Expected an exception when no 
operation is set");
+        Throwable cause = ex.getException();
+        // Unwrap one level if Camel wrapped it
+        if (cause.getCause() instanceof IllegalArgumentException) {
+            cause = cause.getCause();
+        }
+        assertEquals(IllegalArgumentException.class, cause.getClass());
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
@@ -221,6 +235,8 @@ public class KubernetesCronJobProducerTest extends 
KubernetesTestSupport {
                 
from("direct:create").to("kubernetes-cronjob:foo?operation=createCronJob");
                 
from("direct:update").to("kubernetes-cronjob:foo?operation=updateCronJob");
                 
from("direct:delete").to("kubernetes-cronjob:foo?operation=deleteCronJob");
+                // CAMEL-27910: no operation set — must throw 
IllegalArgumentException, not NPE
+                from("direct:noOperation").to("kubernetes-cronjob:foo");
             }
         };
     }
diff --git 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesJobProducerTest.java
 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesJobProducerTest.java
index c4c5103c40e3..f60b79bdff9b 100644
--- 
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesJobProducerTest.java
+++ 
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesJobProducerTest.java
@@ -206,6 +206,20 @@ public class KubernetesJobProducerTest extends 
KubernetesTestSupport {
         assertNotNull(ex.getMessage());
     }
 
+    @Test
+    void missingOperationThrowsIllegalArgumentException() {
+        // CAMEL-27910: switch on null caused NPE; now throws 
IllegalArgumentException
+        Exchange ex = template.request("direct:noOperation", exchange -> {
+        });
+        assertNotNull(ex.getException(), "Expected an exception when no 
operation is set");
+        Throwable cause = ex.getException();
+        // Unwrap one level if Camel wrapped it
+        if (cause.getCause() instanceof IllegalArgumentException) {
+            cause = cause.getCause();
+        }
+        assertEquals(IllegalArgumentException.class, cause.getClass());
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
@@ -220,6 +234,8 @@ public class KubernetesJobProducerTest extends 
KubernetesTestSupport {
                 
from("direct:createWithAnnotations").to("kubernetes-job:foo?operation=createJob");
                 
from("direct:update").to("kubernetes-job:foo?operation=updateJob");
                 
from("direct:delete").to("kubernetes-job:foo?operation=deleteJob");
+                // CAMEL-27910: no operation set — must throw 
IllegalArgumentException, not NPE
+                from("direct:noOperation").to("kubernetes-job:foo");
             }
         };
     }

Reply via email to