Repository: camel
Updated Branches:
  refs/heads/master a02940559 -> ba8c3434d


CAMEL-11837: Add spring-boot auto-configuration for kubernetes cluster


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

Branch: refs/heads/master
Commit: ba8c3434df488f29303d4a0d505aa8ebe73a67d8
Parents: a029405
Author: Nicola Ferraro <ni.ferr...@gmail.com>
Authored: Mon Sep 25 16:31:02 2017 +0200
Committer: Nicola Ferraro <ni.ferr...@gmail.com>
Committed: Mon Sep 25 16:31:15 2017 +0200

----------------------------------------------------------------------
 .../kubernetes/ha/KubernetesClusterService.java |  21 +--
 ...bernetesClusterServiceAutoConfiguration.java |  55 ++++++
 .../KubernetesClusterServiceConfiguration.java  | 183 +++++++++++++++++++
 .../main/resources/META-INF/spring.factories    |   1 +
 ...terServiceAutoConfigurationDisabledTest.java |  47 +++++
 ...etesClusterServiceAutoConfigurationTest.java |  83 +++++++++
 6 files changed, 371 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ba8c3434/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterService.java
----------------------------------------------------------------------
diff --git 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterService.java
 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterService.java
index 00cb04d..60b6f5c 100644
--- 
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterService.java
+++ 
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/ha/KubernetesClusterService.java
@@ -19,8 +19,6 @@ package org.apache.camel.component.kubernetes.ha;
 import java.net.InetAddress;
 import java.util.Map;
 
-import io.fabric8.kubernetes.client.KubernetesClient;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.kubernetes.KubernetesConfiguration;
@@ -127,14 +125,14 @@ public class KubernetesClusterService extends 
AbstractCamelClusterService<Kubern
         configuration.setMasterUrl(masterUrl);
     }
 
-    public Integer getConnectionTimeout() {
+    public Integer getConnectionTimeoutMillis() {
         return configuration.getConnectionTimeout();
     }
 
     /**
      * Connection timeout in milliseconds to use when making requests to the 
Kubernetes API server.
      */
-    public void setConnectionTimeout(Integer connectionTimeout) {
+    public void setConnectionTimeoutMillis(Integer connectionTimeout) {
         configuration.setConnectionTimeout(connectionTimeout);
     }
 
@@ -186,21 +184,6 @@ public class KubernetesClusterService extends 
AbstractCamelClusterService<Kubern
         lockConfiguration.addToClusterLabels(key, value);
     }
 
-    public String getKubernetesResourcesNamespace() {
-        return lockConfiguration.getKubernetesResourcesNamespace();
-    }
-
-    /**
-     * Kubernetes namespace containing the pods and the ConfigMap used for 
locking.
-     */
-    public void setKubernetesResourcesNamespace(String 
kubernetesResourcesNamespace) {
-        
lockConfiguration.setKubernetesResourcesNamespace(kubernetesResourcesNamespace);
-    }
-
-    public String getKubernetesResourcesNamespaceOrDefault(KubernetesClient 
kubernetesClient) {
-        return 
lockConfiguration.getKubernetesResourcesNamespaceOrDefault(kubernetesClient);
-    }
-
     public double getJitterFactor() {
         return lockConfiguration.getJitterFactor();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/ba8c3434/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceAutoConfiguration.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceAutoConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceAutoConfiguration.java
new file mode 100644
index 0000000..f55b84e
--- /dev/null
+++ 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceAutoConfiguration.java
@@ -0,0 +1,55 @@
+/**
+ * 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.springboot.ha;
+
+import org.apache.camel.component.kubernetes.ha.KubernetesClusterService;
+import org.apache.camel.ha.CamelClusterService;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import 
org.apache.camel.spring.boot.ha.ClusteredRouteControllerAutoConfiguration;
+import org.apache.camel.util.IntrospectionSupport;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import 
org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Scope;
+
+@Configuration
+@AutoConfigureBefore({ ClusteredRouteControllerAutoConfiguration.class, 
CamelAutoConfiguration.class })
+@ConditionalOnProperty(prefix = "camel.component.kubernetes.cluster.service", 
name = "enabled")
+@EnableConfigurationProperties(KubernetesClusterServiceConfiguration.class)
+public class KubernetesClusterServiceAutoConfiguration {
+    @Autowired
+    private KubernetesClusterServiceConfiguration configuration;
+
+    @Bean(initMethod = "start", destroyMethod = "stop")
+    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
+    @ConditionalOnMissingBean
+    public CamelClusterService kubernetesClusterService() throws Exception {
+        KubernetesClusterService service = new KubernetesClusterService();
+
+        IntrospectionSupport.setProperties(
+            service,
+            IntrospectionSupport.getNonNullProperties(configuration)
+        );
+
+        return service;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ba8c3434/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceConfiguration.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceConfiguration.java
new file mode 100644
index 0000000..69580b7
--- /dev/null
+++ 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/java/org/apache/camel/component/kubernetes/springboot/ha/KubernetesClusterServiceConfiguration.java
@@ -0,0 +1,183 @@
+/**
+ * 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.springboot.ha;
+
+import java.util.Map;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@ConfigurationProperties(prefix = "camel.component.kubernetes.cluster.service")
+public class KubernetesClusterServiceConfiguration {
+
+    /**
+     * Sets if the Kubernetes cluster service should be enabled or not, 
default is false.
+     */
+    private boolean enabled;
+
+    /**
+     * Cluster Service ID
+     */
+    private String id;
+
+    /**
+     * Set the URL of the Kubernetes master (read from Kubernetes client 
properties by default).
+     */
+    private String masterUrl;
+
+    /**
+     * Connection timeout in milliseconds to use when making requests to the 
Kubernetes API server.
+     */
+    private Integer connectionTimeoutMillis;
+
+    /**
+     * Set the name of the Kubernetes namespace containing the pods and the 
configmap (autodetected by default)
+     */
+    private String kubernetesNamespace;
+
+    /**
+     * Set the name of the ConfigMap used to do optimistic locking (defaults 
to 'leaders').
+     */
+    private String configMapName;
+
+    /**
+     * Set the name of the current pod (autodetected from container host name 
by default).
+     */
+    private String podName;
+
+    /**
+     * Set the labels used to identify the pods composing the cluster.
+     */
+    private Map<String, String> clusterLabels;
+
+    /**
+     * A jitter factor to apply in order to prevent all pods to call 
Kubernetes APIs in the same instant.
+     */
+    private Double jitterFactor;
+
+    /**
+     * The default duration of the lease for the current leader.
+     */
+    private Long leaseDurationMillis;
+
+    /**
+     * The deadline after which the leader must stop its services because it 
may have lost the leadership.
+     */
+    private Long renewDeadlineMillis;
+
+    /**
+     * The time between two subsequent attempts to check and acquire the 
leadership.
+     * It is randomized using the jitter factor.
+     */
+    private Long retryPeriodMillis;
+
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getMasterUrl() {
+        return masterUrl;
+    }
+
+    public void setMasterUrl(String masterUrl) {
+        this.masterUrl = masterUrl;
+    }
+
+    public Integer getConnectionTimeoutMillis() {
+        return connectionTimeoutMillis;
+    }
+
+    public void setConnectionTimeoutMillis(Integer connectionTimeoutMillis) {
+        this.connectionTimeoutMillis = connectionTimeoutMillis;
+    }
+
+    public String getKubernetesNamespace() {
+        return kubernetesNamespace;
+    }
+
+    public void setKubernetesNamespace(String kubernetesNamespace) {
+        this.kubernetesNamespace = kubernetesNamespace;
+    }
+
+    public String getConfigMapName() {
+        return configMapName;
+    }
+
+    public void setConfigMapName(String configMapName) {
+        this.configMapName = configMapName;
+    }
+
+    public String getPodName() {
+        return podName;
+    }
+
+    public void setPodName(String podName) {
+        this.podName = podName;
+    }
+
+    public Map<String, String> getClusterLabels() {
+        return clusterLabels;
+    }
+
+    public void setClusterLabels(Map<String, String> clusterLabels) {
+        this.clusterLabels = clusterLabels;
+    }
+
+    public Double getJitterFactor() {
+        return jitterFactor;
+    }
+
+    public void setJitterFactor(Double jitterFactor) {
+        this.jitterFactor = jitterFactor;
+    }
+
+    public Long getLeaseDurationMillis() {
+        return leaseDurationMillis;
+    }
+
+    public void setLeaseDurationMillis(Long leaseDurationMillis) {
+        this.leaseDurationMillis = leaseDurationMillis;
+    }
+
+    public Long getRenewDeadlineMillis() {
+        return renewDeadlineMillis;
+    }
+
+    public void setRenewDeadlineMillis(Long renewDeadlineMillis) {
+        this.renewDeadlineMillis = renewDeadlineMillis;
+    }
+
+    public Long getRetryPeriodMillis() {
+        return retryPeriodMillis;
+    }
+
+    public void setRetryPeriodMillis(Long retryPeriodMillis) {
+        this.retryPeriodMillis = retryPeriodMillis;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ba8c3434/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories
 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories
index c132eab..9a5a25a 100644
--- 
a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories
+++ 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/main/resources/META-INF/spring.factories
@@ -24,6 +24,7 @@ 
org.apache.camel.component.kubernetes.builds.springboot.KubernetesBuildsComponen
 
org.apache.camel.component.kubernetes.pods.springboot.KubernetesPodsComponentAutoConfiguration,\
 
org.apache.camel.component.kubernetes.secrets.springboot.KubernetesSecretsComponentAutoConfiguration,\
 
org.apache.camel.component.kubernetes.springboot.KubernetesComponentAutoConfiguration,\
+org.apache.camel.component.kubernetes.springboot.ha.KubernetesClusterServiceAutoConfiguration,\
 
org.apache.camel.component.kubernetes.replication_controllers.springboot.KubernetesReplicationControllersComponentAutoConfiguration,\
 
org.apache.camel.component.kubernetes.persistent_volumes.springboot.KubernetesPersistentVolumesComponentAutoConfiguration,\
 
org.apache.camel.component.kubernetes.build_configs.springboot.KubernetesBuildConfigsComponentAutoConfiguration,\

http://git-wip-us.apache.org/repos/asf/camel/blob/ba8c3434/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationDisabledTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationDisabledTest.java
 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationDisabledTest.java
new file mode 100644
index 0000000..8e304d6
--- /dev/null
+++ 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationDisabledTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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.springboot.test.ha;
+
+import org.apache.camel.component.kubernetes.ha.KubernetesClusterService;
+import 
org.apache.camel.component.kubernetes.springboot.ha.KubernetesClusterServiceAutoConfiguration;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.junit.Assert.assertNull;
+
+/**
+ * Testing that the service is not enabled by default.
+ */
+@RunWith(SpringRunner.class)
+@SpringBootApplication
+@ContextConfiguration(classes = 
KubernetesClusterServiceAutoConfiguration.class)
+public class KubernetesClusterServiceAutoConfigurationDisabledTest {
+
+    @Autowired(required = false)
+    private KubernetesClusterService clusterService;
+
+    @Test
+    public void testPropertiesMapped() {
+        assertNull(clusterService);
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/ba8c3434/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationTest.java
 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationTest.java
new file mode 100644
index 0000000..982540b
--- /dev/null
+++ 
b/platforms/spring-boot/components-starter/camel-kubernetes-starter/src/test/java/org/apache/camel/component/kubernetes/springboot/test/ha/KubernetesClusterServiceAutoConfigurationTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.springboot.test.ha;
+
+import org.apache.camel.component.kubernetes.ha.KubernetesClusterService;
+import 
org.apache.camel.component.kubernetes.springboot.ha.KubernetesClusterServiceAutoConfiguration;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Testing that the service can be enabled and configured completely.
+ */
+@RunWith(SpringRunner.class)
+@SpringBootApplication
+@DirtiesContext
+@ContextConfiguration(classes = 
KubernetesClusterServiceAutoConfiguration.class)
+@SpringBootTest(properties = {
+        "camel.component.kubernetes.cluster.service.enabled=true",
+        "camel.component.kubernetes.cluster.service.id=myid1",
+        
"camel.component.kubernetes.cluster.service.master-url=http://myurl:9000";,
+        
"camel.component.kubernetes.cluster.service.connection-timeout-millis=1234",
+        "camel.component.kubernetes.cluster.service.kubernetes-namespace=ns1",
+        "camel.component.kubernetes.cluster.service.config-map-name=cm",
+        "camel.component.kubernetes.cluster.service.pod-name=mypod1",
+        
"camel.component.kubernetes.cluster.service.cluster-labels['app']=myapp",
+        
"camel.component.kubernetes.cluster.service.cluster-labels['provider']=myprovider",
+        
"camel.component.kubernetes.cluster.service.lease-duration-millis=10000",
+        
"camel.component.kubernetes.cluster.service.renew-deadline-millis=8000",
+        "camel.component.kubernetes.cluster.service.retry-period-millis=4000",
+})
+public class KubernetesClusterServiceAutoConfigurationTest {
+
+    @Autowired
+    private KubernetesClusterService clusterService;
+
+    @Test
+    public void testPropertiesMapped() {
+        assertEquals("myid1", clusterService.getId());
+        assertEquals("http://myurl:9000";, clusterService.getMasterUrl());
+        assertEquals(Integer.valueOf(1234), 
clusterService.getConnectionTimeoutMillis());
+        assertEquals("ns1", clusterService.getKubernetesNamespace());
+        assertEquals("cm", clusterService.getConfigMapName());
+        assertEquals("mypod1", clusterService.getPodName());
+
+        assertNotNull(clusterService.getClusterLabels());
+        assertEquals(2, clusterService.getClusterLabels().size());
+        assertEquals("myapp", clusterService.getClusterLabels().get("app"));
+        assertEquals("myprovider", 
clusterService.getClusterLabels().get("provider"));
+
+        assertEquals(1.2, clusterService.getJitterFactor(), 1e-10);
+        assertEquals(10000, clusterService.getLeaseDurationMillis());
+        assertEquals(8000, clusterService.getRenewDeadlineMillis());
+        assertEquals(4000, clusterService.getRetryPeriodMillis());
+
+
+
+    }
+
+}
+

Reply via email to