camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [3/5] camel git commit: [CAMEL-9946] Camel-Kubernetes: Add support for ConfigMap
Date Thu, 05 May 2016 08:26:00 GMT
[CAMEL-9946] Camel-Kubernetes: Add support for ConfigMap


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/913c8fa6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/913c8fa6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/913c8fa6

Branch: refs/heads/master
Commit: 913c8fa6e78b11c90b39a15cb6981e6d7dc60c12
Parents: 330eff3
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Thu May 5 10:09:01 2016 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Thu May 5 10:18:01 2016 +0200

----------------------------------------------------------------------
 .../kubernetes/KubernetesCategory.java          |   2 +
 .../kubernetes/KubernetesConstants.java         |   3 +
 .../kubernetes/KubernetesEndpoint.java          |   4 +
 .../producer/KubernetesConfigMapsProducer.java  | 167 ++++++++++++++++
 .../producer/KubernetesOperations.java          |   7 +
 .../KubernetesConfigMapsProducerTest.java       | 197 +++++++++++++++++++
 6 files changed, 380 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/913c8fa6/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java
----------------------------------------------------------------------
diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java
index faf4a89..23610ee 100644
--- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java
+++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesCategory.java
@@ -38,6 +38,8 @@ public interface KubernetesCategory {
     
     String NODES = "nodes";
     
+    String CONFIGMAPS = "configMaps";
+    
     String BUILDS = "builds";
     
     String BUILD_CONFIGS = "buildConfigs";

http://git-wip-us.apache.org/repos/asf/camel/blob/913c8fa6/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java
index 6613f19..3d78497 100644
--- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java
+++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesConstants.java
@@ -46,6 +46,9 @@ public interface KubernetesConstants {
     String KUBERNETES_SERVICE_ACCOUNT = "CamelKubernetesServiceAccount";
     String KUBERNETES_NODES_LABELS = "CamelKubernetesNodesLabels";
     String KUBERNETES_NODE_NAME = "CamelKubernetesNodeName";
+    String KUBERNETES_CONFIGMAPS_LABELS = "CamelKubernetesConfigMapsLabels";
+    String KUBERNETES_CONFIGMAP_NAME = "CamelKubernetesConfigMapName";
+    String KUBERNETES_CONFIGMAP_DATA = "CamelKubernetesConfigData";
     String KUBERNETES_BUILDS_LABELS = "CamelKubernetesBuildsLabels";
     String KUBERNETES_BUILD_NAME = "CamelKubernetesBuildName";
     String KUBERNETES_BUILD_CONFIGS_LABELS = "CamelKubernetesBuildConfigsLabels";

http://git-wip-us.apache.org/repos/asf/camel/blob/913c8fa6/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java
index 86dd081..c0312a3 100644
--- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java
+++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/KubernetesEndpoint.java
@@ -32,6 +32,7 @@ import org.apache.camel.component.kubernetes.consumer.KubernetesSecretsConsumer;
 import org.apache.camel.component.kubernetes.consumer.KubernetesServicesConsumer;
 import org.apache.camel.component.kubernetes.producer.KubernetesBuildConfigsProducer;
 import org.apache.camel.component.kubernetes.producer.KubernetesBuildsProducer;
+import org.apache.camel.component.kubernetes.producer.KubernetesConfigMapsProducer;
 import org.apache.camel.component.kubernetes.producer.KubernetesNamespacesProducer;
 import org.apache.camel.component.kubernetes.producer.KubernetesNodesProducer;
 import org.apache.camel.component.kubernetes.producer.KubernetesPersistentVolumesClaimsProducer;
@@ -105,6 +106,9 @@ public class KubernetesEndpoint extends DefaultEndpoint {
 
             case KubernetesCategory.NODES:
                 return new KubernetesNodesProducer(this);
+                
+            case KubernetesCategory.CONFIGMAPS:
+                return new KubernetesConfigMapsProducer(this);
 
             case KubernetesCategory.BUILDS:
                 return new KubernetesBuildsProducer(this);

http://git-wip-us.apache.org/repos/asf/camel/blob/913c8fa6/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducer.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducer.java
new file mode 100644
index 0000000..b0b389c
--- /dev/null
+++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducer.java
@@ -0,0 +1,167 @@
+/**
+ * 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.producer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
+import io.fabric8.kubernetes.api.model.ConfigMapList;
+import io.fabric8.kubernetes.api.model.DoneableConfigMap;
+import io.fabric8.kubernetes.api.model.EditableConfigMap;
+import io.fabric8.kubernetes.client.dsl.ClientNonNamespaceOperation;
+import io.fabric8.kubernetes.client.dsl.ClientResource;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesEndpoint;
+import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KubernetesConfigMapsProducer extends DefaultProducer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(KubernetesConfigMapsProducer.class);
+
+    public KubernetesConfigMapsProducer(KubernetesEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public KubernetesEndpoint getEndpoint() {
+        return (KubernetesEndpoint) 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_CONFIGMAPS:
+            doList(exchange, operation);
+            break;
+
+        case KubernetesOperations.LIST_CONFIGMAPS_BY_LABELS_OPERATION:
+            doListConfigMapsByLabels(exchange, operation);
+            break;
+
+        case KubernetesOperations.GET_CONFIGMAP_OPERATION:
+            doGetConfigMap(exchange, operation);
+            break;
+            
+        case KubernetesOperations.CREATE_CONFIGMAP_OPERATION:
+            doCreateConfigMap(exchange, operation);
+            break;
+            
+        case KubernetesOperations.DELETE_CONFIGMAP_OPERATION:
+            doDeleteConfigMap(exchange, operation);
+            break;
+
+        default:
+            throw new IllegalArgumentException("Unsupported operation " + operation);
+        }
+    }
+
+    protected void doList(Exchange exchange, String operation) throws Exception {
+        ConfigMapList configMapsList = getEndpoint().getKubernetesClient().configMaps().list();
+        exchange.getOut().setBody(configMapsList.getItems());
+    }
+
+    protected void doListConfigMapsByLabels(Exchange exchange, String operation) throws Exception
{
+        ConfigMapList configMapsList = null;
+        Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS,
Map.class);
+        ClientNonNamespaceOperation<ConfigMap, ConfigMapList, DoneableConfigMap, ClientResource<ConfigMap,
DoneableConfigMap>> configMaps = getEndpoint().getKubernetesClient().configMaps();
+        for (Map.Entry<String, String> entry : labels.entrySet()) {
+            configMaps.withLabel(entry.getKey(), entry.getValue());
+        }
+        configMapsList = configMaps.list();
+        exchange.getOut().setBody(configMapsList.getItems());
+    }
+
+    protected void doGetConfigMap(Exchange exchange, String operation) throws Exception {
+        ConfigMap configMap = null;
+        String cfMapName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_CONFIGMAP_NAME,
String.class);
+        if (ObjectHelper.isEmpty(cfMapName)) {
+            LOG.error("Get a specific ConfigMap require specify a ConfigMap name");
+            throw new IllegalArgumentException("Get a specific ConfigMap require specify
a ConfigMap name");
+        }
+        configMap = getEndpoint().getKubernetesClient().configMaps().withName(cfMapName).get();
+
+        exchange.getOut().setBody(configMap);
+    }
+    
+    protected void doCreateConfigMap(Exchange exchange, String operation) throws Exception
{
+        ConfigMap configMap = null;
+        String cfMapName = exchange.getIn().getHeader(
+                KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, String.class);
+        String namespaceName = exchange.getIn().getHeader(
+                KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
+        HashMap<String, String> configMapData = exchange.getIn().getHeader(
+                KubernetesConstants.KUBERNETES_CONFIGMAP_DATA, HashMap.class);
+        if (ObjectHelper.isEmpty(cfMapName)) {
+            LOG.error("Create a specific configMap require specify a configMap name");
+            throw new IllegalArgumentException(
+                    "Create a specific configMap require specify a configMap name");
+        }
+        if (ObjectHelper.isEmpty(namespaceName)) {
+            LOG.error("Create a specific configMap require specify a namespace name");
+            throw new IllegalArgumentException(
+                    "Create a specific configMap require specify a namespace name");
+        }
+        if (ObjectHelper.isEmpty(configMapData)) {
+            LOG.error("Create a specific configMap require specify a data map");
+            throw new IllegalArgumentException(
+                    "Create a specific configMap require specify a data map");
+        }
+        Map<String, String> labels = exchange.getIn().getHeader(
+                KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS, Map.class);
+        EditableConfigMap cfMapCreating = new ConfigMapBuilder().withNewMetadata().withName(cfMapName)
+                .withLabels(labels).endMetadata().withData(configMapData).build();
+        configMap = getEndpoint().getKubernetesClient().configMaps()
+                .inNamespace(namespaceName).create(cfMapCreating);
+        exchange.getOut().setBody(configMap);
+    }
+    
+    protected void doDeleteConfigMap(Exchange exchange, String operation) throws Exception
{
+        String configMapName = exchange.getIn().getHeader(
+                KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, String.class);
+        String namespaceName = exchange.getIn().getHeader(
+                KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
+        if (ObjectHelper.isEmpty(configMapName)) {
+            LOG.error("Delete a specific config map require specify a config map name");
+            throw new IllegalArgumentException(
+                    "Delete a specific config map require specify a config map name");
+        }
+        if (ObjectHelper.isEmpty(namespaceName)) {
+            LOG.error("Delete a specific config map require specify a namespace name");
+            throw new IllegalArgumentException(
+                    "Delete a specific config map require specify a namespace name");
+        }
+        boolean cfMapDeleted = getEndpoint().getKubernetesClient().configMaps()
+                .inNamespace(namespaceName).withName(configMapName).delete();
+        exchange.getOut().setBody(cfMapDeleted);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/913c8fa6/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java
----------------------------------------------------------------------
diff --git a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java
index c926cbf..e6dec94 100644
--- a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java
+++ b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/producer/KubernetesOperations.java
@@ -84,6 +84,13 @@ public interface KubernetesOperations {
     String LIST_NODES_BY_LABELS_OPERATION = "listNodesByLabels";
     String GET_NODE_OPERATION = "getNode";
     
+    // Config Maps
+    String LIST_CONFIGMAPS = "listConfigMaps";
+    String LIST_CONFIGMAPS_BY_LABELS_OPERATION = "listConfigMapsByLabels";
+    String GET_CONFIGMAP_OPERATION = "getConfigMap";
+    String CREATE_CONFIGMAP_OPERATION = "createConfigMap";
+    String DELETE_CONFIGMAP_OPERATION = "deleteConfigMap";
+    
     // Builds
     String LIST_BUILD = "listBuilds";
     String LIST_BUILD_BY_LABELS_OPERATION = "listBuildsByLabels";

http://git-wip-us.apache.org/repos/asf/camel/blob/913c8fa6/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducerTest.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducerTest.java
new file mode 100644
index 0000000..d4a997c
--- /dev/null
+++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/producer/KubernetesConfigMapsProducerTest.java
@@ -0,0 +1,197 @@
+/**
+ * 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.producer;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerPort;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.PodSpec;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.kubernetes.KubernetesConstants;
+import org.apache.camel.component.kubernetes.KubernetesTestSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.junit.Test;
+
+public class KubernetesConfigMapsProducerTest extends KubernetesTestSupport {
+
+    @Test
+    public void listTest() throws Exception {
+        if (ObjectHelper.isEmpty(authToken)) {
+            return;
+        }
+        List<ConfigMap> result = template.requestBody("direct:list", "", List.class);
+
+        assertEquals(0, result.size());
+    }
+
+    @Test
+    public void listByLabelsTest() throws Exception {
+        if (ObjectHelper.isEmpty(authToken)) {
+            return;
+        }
+        Exchange ex = template.request("direct:listConfigMapsByLabels", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_NAMESPACE_NAME,
+                        "default");
+                Map<String, String> labels = new HashMap<String, String>();
+                labels.put("component", "elasticsearch");
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS, labels);
+            }
+        });
+
+        List<ConfigMap> result = ex.getOut().getBody(List.class);
+
+        boolean configMapExists = false;
+        Iterator<ConfigMap> it = result.iterator();
+        while (it.hasNext()) {
+        	ConfigMap cfMap = it.next();
+            if (cfMap.getMetadata().getLabels().containsValue("elasticsearch")) {
+            	configMapExists = true;
+            }
+        }
+
+        assertFalse(configMapExists);
+    }
+
+    @Test
+    public void getConfigMapTest() throws Exception {
+        if (ObjectHelper.isEmpty(authToken)) {
+            return;
+        }
+        Exchange ex = template.request("direct:getConfigMap", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_NAMESPACE_NAME,
+                        "default");
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAP_NAME,
+                        "elasticsearch-7015o");
+            }
+        });
+
+        ConfigMap result = ex.getOut().getBody(ConfigMap.class);
+
+        assertNull(result);
+    }
+
+    @Test
+    public void createGetAndDeleteConfigMap() throws Exception {
+        if (ObjectHelper.isEmpty(authToken)) {
+            return;
+        }
+        Exchange ex = template.request("direct:createConfigMap", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_NAMESPACE_NAME,
+                        "default");
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, "test");
+                Map<String, String> labels = new HashMap<String, String>();
+                labels.put("this", "rocks");
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS, labels);
+
+                Map<String, String> data = new HashMap<String, String>();
+                
+                data.put("test", "test1");
+                data.put("test1", "test2");
+
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAP_DATA, data);
+            }
+        });
+        
+        ex = template.request("direct:getConfigMap", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_NAMESPACE_NAME,
+                        "default");
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAP_NAME,
+                        "test");
+            }
+        });
+
+        ConfigMap result = ex.getOut().getBody(ConfigMap.class);
+        
+        assertNotNull(result);
+        assertTrue(result.getData().containsKey("test"));
+        assertTrue(result.getData().containsKey("test1"));
+        assertEquals("test1",result.getData().get("test"));
+        assertEquals("test2",result.getData().get("test1"));
+
+        ex = template.request("direct:deleteConfigMap", new Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_NAMESPACE_NAME,
+                        "default");
+                exchange.getIn().setHeader(
+                        KubernetesConstants.KUBERNETES_CONFIGMAP_NAME, "test");
+            }
+        });
+
+        boolean configMapDeleted = ex.getOut().getBody(Boolean.class);
+
+        assertTrue(configMapDeleted);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:list")
+                        .toF("kubernetes://%s?oauthToken=%s&category=configMaps&operation=listConfigMaps",
+                                host, authToken);
+                from("direct:listConfigMapsByLabels")
+                        .toF("kubernetes://%s?oauthToken=%s&category=configMaps&operation=listConfigMapsByLabels",
+                                host, authToken);
+                from("direct:getConfigMap")
+                        .toF("kubernetes://%s?oauthToken=%s&category=configMaps&operation=getConfigMap",
+                                host, authToken);
+                from("direct:createConfigMap")
+                        .toF("kubernetes://%s?oauthToken=%s&category=configMaps&operation=createConfigMap",
+                                host, authToken);
+                from("direct:deleteConfigMap")
+                        .toF("kubernetes://%s?oauthToken=%s&category=configMaps&operation=deleteConfigMap",
+                                host, authToken);
+            }
+        };
+    }
+}


Mime
View raw message