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



##########
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:
       Thanks!  I move the call.

##########
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:
       I change it to operations. 

##########
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:
       All operations require the namespace argument so I add check the check 
in the `process` mentod.

##########
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:
       Thanks!  I'll add it.

##########
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:
       Thanks!  I add check for these arguments.

##########
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:
       OK.  I take it out.

##########
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:
       OK. I take it out.

##########
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:
       OK.  I make changes.  Thanks!




----------------------------------------------------------------
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:
[email protected]


Reply via email to