omarsmak commented on a change in pull request #4541:
URL: https://github.com/apache/camel/pull/4541#discussion_r515829456



##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.ExecutorService;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.Watch;
+import io.fabric8.kubernetes.client.Watcher;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import 
io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConfiguration;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesConsumer.class);
+
+    private final Processor processor;
+    private ExecutorService executor;
+    private CustomResourcesConsumerTask customResourcesWatcher;
+
+    public KubernetesCustomResourcesConsumer(AbstractKubernetesEndpoint 
endpoint, Processor processor) {
+        super(endpoint, processor);
+        this.processor = processor;
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        executor = getEndpoint().createExecutor();
+
+        customResourcesWatcher = new CustomResourcesConsumerTask();
+        executor.submit(customResourcesWatcher);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();

Review comment:
       INHO I'd stop the camel context last after stopping the executor

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @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();
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);

Review comment:
       The namespace here is essential for the operation, isn't? If so, can we 
add checks for the required options (in this operation and the other operation) 
that being obtained from the headers and throw an exception with meaningful 
message to the user?  

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.ExecutorService;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.Watch;
+import io.fabric8.kubernetes.client.Watcher;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import 
io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConfiguration;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesConsumer.class);
+
+    private final Processor processor;
+    private ExecutorService executor;
+    private CustomResourcesConsumerTask customResourcesWatcher;
+
+    public KubernetesCustomResourcesConsumer(AbstractKubernetesEndpoint 
endpoint, Processor processor) {
+        super(endpoint, processor);
+        this.processor = processor;
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        executor = getEndpoint().createExecutor();
+
+        customResourcesWatcher = new CustomResourcesConsumerTask();
+        executor.submit(customResourcesWatcher);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+
+        LOG.debug("Stopping Kubernetes Custom Resources Consumer");
+        if (executor != null) {
+            if (getEndpoint() != null && getEndpoint().getCamelContext() != 
null) {
+                if (customResourcesWatcher != null) {
+                    customResourcesWatcher.getWatch().close();
+                }
+                
getEndpoint().getCamelContext().getExecutorServiceManager().shutdownNow(executor);
+            } else {
+                if (customResourcesWatcher != null) {
+                    customResourcesWatcher.getWatch().close();
+                }
+                executor.shutdownNow();
+            }
+        }
+        executor = null;
+    }
+
+    class CustomResourcesConsumerTask implements Runnable {
+
+        private Watch watch;
+
+        @Override
+        public void run() {
+            RawCustomResourceOperationsImpl w = 
getEndpoint().getKubernetesClient()

Review comment:
       Can we have more meaningful name instead of `w`? 

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @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();
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);

Review comment:
       Apart from my other comment, I think these configurations can be 
obtained either from the endpoint configurations or the exchange headers, not 
only the exchange headers, isn't? If so, IMHO, I'd add a logic to first check 
if these options being set in the exchange headers first and fall back to the 
endpoint options aka `KubernetesConfiguration`.

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @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();
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        Map<String, String> labels = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_LABELS, 
Map.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doGet(Exchange exchange, String operation) throws Exception 
{
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        if (ObjectHelper.isEmpty(customResourceName)) {
+            LOG.error("Get a specific Deployment require specify a Deployment 
name");
+            throw new IllegalArgumentException("Get a specific Deployment 
require specify a Deployment name");
+        }
+        JsonObject customResourceJSON = new JsonObject();
+        try {
+            customResourceJSON = new JsonObject(
+                    
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).get(namespaceName,
+                            customResourceName));
+        } catch (KubernetesClientException e) {
+            if (e.getCode() == 404) {
+                LOG.info("Custom resource instance not found", e);
+            } else {
+                throw e;
+            }
+        }
+        LOG.info(customResourceJSON.toString());
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourceJSON);
+    }
+
+    protected void doDelete(Exchange exchange, String operation) throws 
Exception {
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        if (ObjectHelper.isEmpty(customResourceName)) {
+            LOG.error("Delete a specific deployment require specify a 
deployment name");
+            throw new IllegalArgumentException("Delete a specific deployment 
require specify a deployment name");
+        }
+        if (ObjectHelper.isEmpty(namespaceName)) {
+            LOG.error("Delete a specific deployment require specify a 
namespace name");
+            throw new IllegalArgumentException("Delete a specific deployment 
require specify a namespace name");
+        }
+
+        JsonObject customResourceJSON = new JsonObject();
+        try {
+            customResourceJSON = new JsonObject(
+                    
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).delete(namespaceName,
+                            customResourceName));
+        } catch (KubernetesClientException e) {
+            if (e.getCode() == 404) {
+                LOG.info("Custom resource instance not found", e);
+            } else {
+                throw e;
+            }
+        }
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourceJSON);
+    }
+
+    protected void doCreate(Exchange exchange, String operation) throws 
Exception {
+        String customResourceInstance = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+
+        JsonObject gitHubSourceJSON = new JsonObject();
+        try {
+            gitHubSourceJSON = new JsonObject(
+                    
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).create(namespaceName,
+                            customResourceInstance));
+        } catch (KubernetesClientException e) {
+            if (e.getCode() == 409) {
+                LOG.info("Custom resoure instance already exists", e);
+            } else {
+                throw e;
+            }
+        }
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(gitHubSourceJSON);
+    }
+
+    private CustomResourceDefinitionContext getCRDContext(Message message) {
+        CustomResourceDefinitionContext cRDContext = new 
CustomResourceDefinitionContext.Builder()
+                
.withName(message.getHeader(KubernetesConstants.KUBERNETES_CRD_NAME, 
String.class))       // example: "githubsources.sources.knative.dev"

Review comment:
       Also here, if not mistaken, all these options are required for the CRD 
context, isn't? Then checks here would help if this option is set or not and 
throw an exception with meaningful message to the user.

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesConsumer.java
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.ExecutorService;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.Watch;
+import io.fabric8.kubernetes.client.Watcher;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import 
io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConfiguration;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesConsumer extends DefaultConsumer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesConsumer.class);
+
+    private final Processor processor;
+    private ExecutorService executor;
+    private CustomResourcesConsumerTask customResourcesWatcher;
+
+    public KubernetesCustomResourcesConsumer(AbstractKubernetesEndpoint 
endpoint, Processor processor) {
+        super(endpoint, processor);
+        this.processor = processor;
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        executor = getEndpoint().createExecutor();
+
+        customResourcesWatcher = new CustomResourcesConsumerTask();
+        executor.submit(customResourcesWatcher);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();

Review comment:
       IMHO I'd stop the camel context last after stopping the executor

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String operation;
+        String namespace;
+
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
+            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
+        } else {
+            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
+        }
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getNamespace()))
 {
+            namespace = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        } else {
+            namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
+        }
+        if (ObjectHelper.isEmpty(namespace)) {
+            throw new IllegalArgumentException("Custom Resource producer 
requires a namespace argument");
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+
+        JsonArray customResourcesListItems;
+        if (customResourcesListJSON.getCollection("items") != null) {
+            customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+        } else {
+            customResourcesListItems = new JsonArray();
+        }
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        Map<String, String> labels = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_LABELS, 
Map.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doGet(Exchange exchange, String operation) throws Exception 
{
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        if (ObjectHelper.isEmpty(customResourceName)) {
+            LOG.error("Get a specific Deployment require specify a Deployment 
name");

Review comment:
       You can remove `LOG.error` instances since you are already throwing an 
exception.

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String operation;
+        String namespace;
+
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
+            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
+        } else {
+            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
+        }
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getNamespace()))
 {
+            namespace = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        } else {
+            namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
+        }
+        if (ObjectHelper.isEmpty(namespace)) {
+            throw new IllegalArgumentException("Custom Resource producer 
requires a namespace argument");
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+
+        JsonArray customResourcesListItems;
+        if (customResourcesListJSON.getCollection("items") != null) {
+            customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+        } else {
+            customResourcesListItems = new JsonArray();
+        }
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        Map<String, String> labels = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_LABELS, 
Map.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doGet(Exchange exchange, String operation) throws Exception 
{
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);

Review comment:
       Since you are already obtaining the namespace 
[here](https://github.com/apache/camel/pull/4541/files#diff-6ca11b2d4ce0280f5835a826f21758273973e7cbf43218bdcf87740b3c079990R60,
 would it be more efficient to just pass the obtained namespace to the function 
instead of re-obtaining the namespace again?

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String operation;
+        String namespace;
+
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
+            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
+        } else {
+            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
+        }
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getNamespace()))
 {
+            namespace = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        } else {
+            namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
+        }
+        if (ObjectHelper.isEmpty(namespace)) {
+            throw new IllegalArgumentException("Custom Resource producer 
requires a namespace argument");
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+
+        JsonArray customResourcesListItems;
+        if (customResourcesListJSON.getCollection("items") != null) {
+            customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+        } else {
+            customResourcesListItems = new JsonArray();
+        }
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        Map<String, String> labels = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_LABELS, 
Map.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doGet(Exchange exchange, String operation) throws Exception 
{
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);

Review comment:
       Since you are already obtaining the namespace 
[here](https://github.com/apache/camel/pull/4541/files#diff-6ca11b2d4ce0280f5835a826f21758273973e7cbf43218bdcf87740b3c079990R60),
 would it be more efficient to just pass the obtained namespace to the function 
instead of re-obtaining the namespace again?

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String operation;
+        String namespace;
+
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
+            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
+        } else {
+            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
+        }
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getNamespace()))
 {
+            namespace = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        } else {
+            namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
+        }
+        if (ObjectHelper.isEmpty(namespace)) {
+            throw new IllegalArgumentException("Custom Resource producer 
requires a namespace argument");
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+
+        JsonArray customResourcesListItems;
+        if (customResourcesListJSON.getCollection("items") != null) {
+            customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+        } else {
+            customResourcesListItems = new JsonArray();
+        }
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        Map<String, String> labels = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_LABELS, 
Map.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doGet(Exchange exchange, String operation) throws Exception 
{
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);

Review comment:
       Since you are already obtaining the namespace 
[here](https://github.com/apache/camel/pull/4541/files#diff-6ca11b2d4ce0280f5835a826f21758273973e7cbf43218bdcf87740b3c079990R60),
 would it be more efficient to just pass the obtained namespace to the 
functions instead of re-obtaining the namespace again?

##########
File path: 
components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/customresources/KubernetesCustomResourcesProducer.java
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.Map;
+
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kubernetes.AbstractKubernetesEndpoint;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesOperations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesCustomResourcesProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KubernetesCustomResourcesProducer.class);
+
+    public KubernetesCustomResourcesProducer(AbstractKubernetesEndpoint 
endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public AbstractKubernetesEndpoint getEndpoint() {
+        return (AbstractKubernetesEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        String operation;
+        String namespace;
+
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getOperation()))
 {
+            operation = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_OPERATION, 
String.class);
+        } else {
+            operation = 
getEndpoint().getKubernetesConfiguration().getOperation();
+        }
+        if 
(ObjectHelper.isEmpty(getEndpoint().getKubernetesConfiguration().getNamespace()))
 {
+            namespace = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        } else {
+            namespace = 
getEndpoint().getKubernetesConfiguration().getNamespace();
+        }
+        if (ObjectHelper.isEmpty(namespace)) {
+            throw new IllegalArgumentException("Custom Resource producer 
requires a namespace argument");
+        }
+
+        switch (operation) {
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES:
+                doList(exchange, operation);
+                break;
+
+            case KubernetesOperations.LIST_CUSTOMRESOURCES_BY_LABELS_OPERATION:
+                doListByLabels(exchange, operation);
+                break;
+
+            case KubernetesOperations.GET_CUSTOMRESOURCE:
+                doGet(exchange, operation);
+                break;
+
+            case KubernetesOperations.DELETE_CUSTOMRESOURCE:
+                doDelete(exchange, operation);
+                break;
+
+            case KubernetesOperations.CREATE_CUSTOMRESOURCE:
+                doCreate(exchange, operation);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation " + 
operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+
+        JsonArray customResourcesListItems;
+        if (customResourcesListJSON.getCollection("items") != null) {
+            customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+        } else {
+            customResourcesListItems = new JsonArray();
+        }
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doListByLabels(Exchange exchange, String operation) throws 
Exception {
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        Map<String, String> labels = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_LABELS, 
Map.class);
+        JsonObject customResourcesListJSON = new JsonObject(
+                
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).list(namespaceName));
+        LOG.info(customResourcesListJSON.toString());
+        JsonArray customResourcesListItems = new 
JsonArray(customResourcesListJSON.getCollection("items"));
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourcesListItems);
+    }
+
+    protected void doGet(Exchange exchange, String operation) throws Exception 
{
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        if (ObjectHelper.isEmpty(customResourceName)) {
+            LOG.error("Get a specific Deployment require specify a Deployment 
name");
+            throw new IllegalArgumentException("Get a specific Deployment 
require specify a Deployment name");
+        }
+        JsonObject customResourceJSON = new JsonObject();
+        try {
+            customResourceJSON = new JsonObject(
+                    
getEndpoint().getKubernetesClient().customResource(getCRDContext(exchange.getIn())).get(namespaceName,
+                            customResourceName));
+        } catch (KubernetesClientException e) {
+            if (e.getCode() == 404) {
+                LOG.info("Custom resource instance not found", e);
+            } else {
+                throw e;
+            }
+        }
+        LOG.info(customResourceJSON.toString());
+
+        MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
+        exchange.getOut().setBody(customResourceJSON);
+    }
+
+    protected void doDelete(Exchange exchange, String operation) throws 
Exception {
+        String customResourceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CRD_INSTANCE_NAME, 
String.class);
+        String namespaceName = 
exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, 
String.class);
+        if (ObjectHelper.isEmpty(customResourceName)) {
+            LOG.error("Delete a specific deployment require specify a 
deployment name");
+            throw new IllegalArgumentException("Delete a specific deployment 
require specify a deployment name");
+        }
+        if (ObjectHelper.isEmpty(namespaceName)) {

Review comment:
       I think this check here is not needed since you are already checking for 
the namespace in the `process` method




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to