diff --git a/.circleci/config.yml b/.circleci/config.yml index 420d17af..124ebb68 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -72,7 +72,7 @@ jobs: # Allow intra-pod communication sudo iptables -P FORWARD ACCEPT - /snap/bin/microk8s.enable dns registry + echo n |/snap/bin/microk8s.enable dns registry istio # wait until the registry is up and running sleep 10 @@ -85,6 +85,21 @@ jobs: done echo "Kubernetes Container Registry enabled" + + # wait until istio is up and running + sleep 10 + n=0 + until [ $n -ge 10 ] + do + (/snap/bin/microk8s.kubectl get pod -l istio=sidecar-injector --namespace=istio-system | grep -z "Running") && break + n=$[$n+1] + sleep 10 + done + + echo "Istio enabled" + + # create the namespace where the istio integration test will run + /snap/bin/microk8s.kubectl create -f .circleci/istio-test-namespace.yml - run: name: Run integration tests command: | diff --git a/.circleci/istio-test-namespace.yml b/.circleci/istio-test-namespace.yml new file mode 100644 index 00000000..a9421811 --- /dev/null +++ b/.circleci/istio-test-namespace.yml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: istio-test + labels: + istio-injection: enabled \ No newline at end of file diff --git a/docs/src/main/asciidoc/kubernetes-awareness.adoc b/docs/src/main/asciidoc/kubernetes-awareness.adoc index 27c3a075..c723cef0 100644 --- a/docs/src/main/asciidoc/kubernetes-awareness.adoc +++ b/docs/src/main/asciidoc/kubernetes-awareness.adoc @@ -1,11 +1,11 @@ -== Kubernetes Awareness +== Kubernetes Ecosystem Awareness All of the features described above will work equally well regardless of whether your application is running inside Kubernetes or not. This is really helpful for development and troubleshooting. From a development point of view, this is really helpful as you can start your Spring Boot application and debug one of the modules part of this project. It is not required to deploy it in Kubernetes as the code of the project relies on the -[Fabric8 Kubernetes Java client](https://github.com/fabric8io/kubernetes-client) which is a fluent DSL able to +https://github.com/fabric8io/kubernetes-client[Fabric8 Kubernetes Java client] which is a fluent DSL able to communicate using `http` protocol to the REST API of Kubernetes Server. === Kubernetes Profile Autoconfiguration @@ -13,3 +13,12 @@ communicate using `http` protocol to the REST API of Kubernetes Server. When the application runs as a pod inside Kubernetes a Spring profile named `kubernetes` will automatically get activated. This allows the developer to customize the configuration, to define beans that will be applied when the Spring Boot application is deployed within the Kubernetes platform *(e.g. different dev and prod configuration)*. + +=== Istio Awareness + +When including the **spring-cloud-kubernetes-istio** module into the application classpath a new profile will be added to the application, +if the application is running inside a Kubernetes Cluster with http://istio.io[Istio] installed. Then you can use +spring **@Profile("istio")** annotations into your Beans and **@Configuration**'s. + +The Istio awareness module uses the **me.snowdrop:istio-client** to interact with Istio APIs enabling us to discover traffic rules, circuit breakers, etc. +Making it easy for our Spring Boot applications to consume this data to dynamically configure themselves according the environment. diff --git a/pom.xml b/pom.xml index f9553412..8a08368c 100644 --- a/pom.xml +++ b/pom.xml @@ -88,13 +88,14 @@ spring-cloud-starter-kubernetes-all spring-cloud-kubernetes-examples spring-cloud-kubernetes-leader + spring-cloud-kubernetes-istio spring-cloud-kubernetes-integration-tests docs - + org.springframework.cloud spring-cloud-kubernetes-dependencies diff --git a/spring-cloud-kubernetes-dependencies/pom.xml b/spring-cloud-kubernetes-dependencies/pom.xml index daecc71b..cea31e4e 100644 --- a/spring-cloud-kubernetes-dependencies/pom.xml +++ b/spring-cloud-kubernetes-dependencies/pom.xml @@ -34,6 +34,7 @@ 1.4.0.Final 1.15.2 4.1.0 + 1.0.0 0.1.0 @@ -46,6 +47,12 @@ import + + me.snowdrop + istio-client + ${istio-client.version} + + org.springframework.cloud @@ -65,6 +72,11 @@ ${project.version} + + org.springframework.cloud + spring-cloud-kubernetes-istio + ${project.version} + org.springframework.cloud diff --git a/spring-cloud-kubernetes-integration-tests/README.md b/spring-cloud-kubernetes-integration-tests/README.md index df4984cc..23d87394 100644 --- a/spring-cloud-kubernetes-integration-tests/README.md +++ b/spring-cloud-kubernetes-integration-tests/README.md @@ -27,7 +27,7 @@ tests against that microk8s cluster ```bash sudo snap install microk8s --classic --channel=1.11/stable sleep 10 -microk8s.enable dns registry +echo n | microk8s.enable dns registry istio ``` Ensure everything is running by inspecting the output of: @@ -66,6 +66,13 @@ Export the kube config file that will be used to access this cluster microk8s.kubectl config view --raw > /tmp/kubeconfig ``` +For the istio tests to work, we need a namespace that is properly configured for Istio injection. +Such a namespace called `istio-test` can easily be created using: + +```bash +microk8s.kubectl create -f .circleci/istio-test-namespace.yml +``` + ## Launch tests diff --git a/spring-cloud-kubernetes-integration-tests/istio/pom.xml b/spring-cloud-kubernetes-integration-tests/istio/pom.xml new file mode 100644 index 00000000..20e33baf --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + + org.springframework.cloud + spring-cloud-kubernetes-integration-tests + 1.0.0.BUILD-SNAPSHOT + + + Spring Cloud Kubernetes :: Integration Tests :: Istio + istio + jar + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-actuator + + + org.springframework.boot + spring-boot-actuator-autoconfigure + + + org.springframework.cloud + spring-cloud-kubernetes-istio + + + org.springframework.boot + spring-boot-starter-test + test + + + org.arquillian.cube + arquillian-cube-kubernetes + ${arquillian-cube.version} + test + + + undertow-core + io.undertow + + + + + org.jboss.arquillian.junit + arquillian-junit-standalone + test + + + io.rest-assured + rest-assured + test + + + + + diff --git a/spring-cloud-kubernetes-integration-tests/istio/src/main/fabric8/deployment.yml b/spring-cloud-kubernetes-integration-tests/istio/src/main/fabric8/deployment.yml new file mode 100644 index 00000000..3c8b53a3 --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/src/main/fabric8/deployment.yml @@ -0,0 +1,10 @@ +# We need this fragment in order for the kubernetes client to talk to +# the Kubernetes API without caring about proper certificates +spec: + template: + spec: + containers: + - env: + - name: KUBERNETES_TRUST_CERTIFICATES + value: true + diff --git a/spring-cloud-kubernetes-integration-tests/istio/src/main/fabric8/svc.yml b/spring-cloud-kubernetes-integration-tests/istio/src/main/fabric8/svc.yml new file mode 100644 index 00000000..945f430d --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/src/main/fabric8/svc.yml @@ -0,0 +1,15 @@ +# we are using an FMP fragment to ensure that NodePort is used correctly +kind: Service +apiVersion: v1 +metadata: + name: ${project.artifactId} + labels: + app: ${project.artifactId} +spec: + selector: + app: ${project.artifactId} + ports: + - protocol: TCP + port: 8080 + nodePort: ${nodeport.value} + type: NodePort diff --git a/spring-cloud-kubernetes-integration-tests/istio/src/main/java/org/springframework/cloud/kubernetes/it/IstioApplication.java b/spring-cloud-kubernetes-integration-tests/istio/src/main/java/org/springframework/cloud/kubernetes/it/IstioApplication.java new file mode 100644 index 00000000..e1545358 --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/src/main/java/org/springframework/cloud/kubernetes/it/IstioApplication.java @@ -0,0 +1,34 @@ +package org.springframework.cloud.kubernetes.it; + +import me.snowdrop.istio.client.IstioClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@SpringBootApplication +@RestController +public class IstioApplication { + + @Autowired + private Environment environment; + + // used just to ensure that the IstioClient is properly injected into the context + @Autowired + private IstioClient istioClient; + + @GetMapping("/profiles") + public List profiles() { + return Arrays.asList(environment.getActiveProfiles()); + } + + public static void main(String[] args) { + SpringApplication.run(IstioApplication.class, args); + } + +} diff --git a/spring-cloud-kubernetes-integration-tests/istio/src/main/resources/bootstrap.yaml b/spring-cloud-kubernetes-integration-tests/istio/src/main/resources/bootstrap.yaml new file mode 100644 index 00000000..00280e18 --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/src/main/resources/bootstrap.yaml @@ -0,0 +1,5 @@ +spring: + cloud: + istio: + client: + envoyPort: 15000 diff --git a/spring-cloud-kubernetes-integration-tests/istio/src/test/java/org/springframework/cloud/kubernetes/it/ProfilesIT.java b/spring-cloud-kubernetes-integration-tests/istio/src/test/java/org/springframework/cloud/kubernetes/it/ProfilesIT.java new file mode 100644 index 00000000..1575893a --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/src/test/java/org/springframework/cloud/kubernetes/it/ProfilesIT.java @@ -0,0 +1,28 @@ +package org.springframework.cloud.kubernetes.it; + +import static io.restassured.RestAssured.given; + +import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes; +import org.hamcrest.core.StringContains; +import org.jboss.arquillian.junit.Arquillian; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RequiresKubernetes +@RunWith(Arquillian.class) +public class ProfilesIT { + + private static final String HOST = System.getProperty("service.host"); + private static final Integer PORT = Integer.valueOf(System.getProperty("service.port")); + + @Test + public void testProfileEndpoint() { + given() + .baseUri(String.format("http://%s:%d", HOST, PORT)) + .get("profiles") + .then() + .statusCode(200) + .body(new StringContains("istio")); + } + +} diff --git a/spring-cloud-kubernetes-integration-tests/istio/src/test/resources/arquillian.xml b/spring-cloud-kubernetes-integration-tests/istio/src/test/resources/arquillian.xml new file mode 100644 index 00000000..315c6d10 --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/istio/src/test/resources/arquillian.xml @@ -0,0 +1,9 @@ + + + + istio-test + + + diff --git a/spring-cloud-kubernetes-integration-tests/pom.xml b/spring-cloud-kubernetes-integration-tests/pom.xml index 427aab48..7d02ab5f 100644 --- a/spring-cloud-kubernetes-integration-tests/pom.xml +++ b/spring-cloud-kubernetes-integration-tests/pom.xml @@ -128,6 +128,7 @@ simple-core simple-configmap + istio discovery diff --git a/spring-cloud-kubernetes-istio/pom.xml b/spring-cloud-kubernetes-istio/pom.xml new file mode 100644 index 00000000..b3b30c7b --- /dev/null +++ b/spring-cloud-kubernetes-istio/pom.xml @@ -0,0 +1,41 @@ + + + + spring-cloud-kubernetes + org.springframework.cloud + 1.0.0.BUILD-SNAPSHOT + + 4.0.0 + + spring-cloud-kubernetes-istio + Spring Cloud Kubernetes :: Istio + + + + org.springframework.cloud + spring-cloud-kubernetes-core + + + org.springframework + spring-web + + + me.snowdrop + istio-client + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + test + + + + diff --git a/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioAutoConfiguration.java b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioAutoConfiguration.java new file mode 100644 index 00000000..b248124a --- /dev/null +++ b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioAutoConfiguration.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed 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.springframework.cloud.kubernetes.istio; + +import io.fabric8.kubernetes.client.Config; +import me.snowdrop.istio.client.DefaultIstioClient; +import me.snowdrop.istio.client.IstioClient; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(value = "spring.cloud.istio.enabled", matchIfMissing = true) +public class IstioAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public IstioClient istioClient(Config config) { + return new DefaultIstioClient(config); + } +} diff --git a/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioBootstrapConfiguration.java b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioBootstrapConfiguration.java new file mode 100644 index 00000000..e09ece03 --- /dev/null +++ b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioBootstrapConfiguration.java @@ -0,0 +1,90 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed 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.springframework.cloud.kubernetes.istio; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.kubernetes.istio.utils.MeshUtils; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import javax.annotation.PostConstruct; +import java.util.Arrays; + +@Configuration +@ConditionalOnProperty(value = "spring.cloud.istio.enabled", matchIfMissing = true) +@EnableConfigurationProperties(IstioClientProperties.class) +public class IstioBootstrapConfiguration { + + private static final Log LOG = LogFactory.getLog(IstioBootstrapConfiguration.class); + + private static final String ISTIO_PROFILE = "istio"; + + @Bean + @ConditionalOnMissingBean + public MeshUtils istioMeshUtils(IstioClientProperties istioClientProperties) { + return new MeshUtils(istioClientProperties); + } + + @EnableConfigurationProperties(IstioClientProperties.class) + protected static class IstioDetectionConfiguration { + + private final MeshUtils utils; + + + private final ConfigurableEnvironment environment; + + public IstioDetectionConfiguration(MeshUtils utils, ConfigurableEnvironment environment) { + this.utils = utils; + this.environment = environment; + } + + @PostConstruct + public void detectIstio() { + addIstioProfile(environment); + } + + void addIstioProfile(ConfigurableEnvironment environment) { + if (utils.isIstioEnabled()) { + if (hasIstioProfile(environment)) { + if (LOG.isDebugEnabled()) { + LOG.debug("'istio' already in list of active profiles"); + } + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("Adding 'istio' to list of active profiles"); + } + environment.addActiveProfile(ISTIO_PROFILE); + } + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("Not running inside kubernetes with istio enabled. Skipping 'istio' profile activation."); + } + } + } + + private boolean hasIstioProfile(Environment environment) { + return Arrays.stream(environment.getActiveProfiles()).anyMatch(ISTIO_PROFILE::equalsIgnoreCase); + } + } + + +} diff --git a/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioClientProperties.java b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioClientProperties.java new file mode 100644 index 00000000..14b513fa --- /dev/null +++ b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/IstioClientProperties.java @@ -0,0 +1,45 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed 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.springframework.cloud.kubernetes.istio; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties("spring.cloud.istio.client") +public class IstioClientProperties { + + private Integer envoyPort = 15090; + private String testPath = "stats/prometheus"; + + public Integer getEnvoyPort() { + return envoyPort; + } + + public void setEnvoyPort(Integer envoyPort) { + this.envoyPort = envoyPort; + } + + public String getTestPath() { + return testPath; + } + + public void setTestPath(String testPath) { + this.testPath = testPath; + } +} diff --git a/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/utils/MeshUtils.java b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/utils/MeshUtils.java new file mode 100644 index 00000000..740ffa07 --- /dev/null +++ b/spring-cloud-kubernetes-istio/src/main/java/org/springframework/cloud/kubernetes/istio/utils/MeshUtils.java @@ -0,0 +1,66 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed 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.springframework.cloud.kubernetes.istio.utils; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.cloud.kubernetes.istio.IstioClientProperties; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +public class MeshUtils { + + + private static final Log LOG = LogFactory.getLog(MeshUtils.class); + + private final IstioClientProperties istioClientProperties; + + private RestTemplate restTemplate = new RestTemplateBuilder().build(); + + public MeshUtils(IstioClientProperties istioClientProperties) { + this.istioClientProperties = istioClientProperties; + } + + public Boolean isIstioEnabled() { + return checkIstioServices(); + } + + private synchronized boolean checkIstioServices() { + try { + //Check if Istio Envoy proxy is installed. Notice that the check is done to localhost. + // TODO: We can improve this initial detection if better methods are found. + String resource = "http://localhost:" + istioClientProperties.getEnvoyPort(); + ResponseEntity response = restTemplate.getForEntity(resource + "/" + istioClientProperties.getTestPath(), String.class); + if (response.getStatusCode().is2xxSuccessful()) { + LOG.info("Istio Resources Found."); + return true; + } + LOG.warn("Although Envoy proxy did respond at port" + istioClientProperties.getEnvoyPort() + + ", it did not respond with HTTP 200 to path: " + istioClientProperties.getTestPath() + + ". You may need to tweak the test path in order to get proper Istio support"); + return false; + } catch (Throwable t) { + if (LOG.isDebugEnabled()) { + LOG.debug("Envoy proxy could not be located at port: " + istioClientProperties.getEnvoyPort() + + ". Assuming that the application is not running inside the Istio Service Mesh"); + } + return false; + } + } +} diff --git a/spring-cloud-kubernetes-istio/src/main/resources/META-INF/spring.factories b/spring-cloud-kubernetes-istio/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..d3d14ce3 --- /dev/null +++ b/spring-cloud-kubernetes-istio/src/main/resources/META-INF/spring.factories @@ -0,0 +1,5 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.cloud.kubernetes.istio.IstioAutoConfiguration + +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ +org.springframework.cloud.kubernetes.istio.IstioBootstrapConfiguration