#516 Adding spring-cloud-starter-loadbalancer to spring-cloud-starter-kubernetes-all and provide integration tests inside module spring-cloud-kubernetes-integration-tests

This commit is contained in:
piomin
2020-06-07 23:14:33 +02:00
parent 7f608b7e8d
commit aa1f9a292c
6 changed files with 218 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>load-balancer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-server-mock</artifactId>
<version>4.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,31 @@
package org.spring.framework.kubernetes.loadbalancer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@RestController
public class SimpleLoadBalancerApp {
public static void main(String[] args) {
SpringApplication.run(SimpleLoadBalancerApp.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplateBuilder().build();
}
@GetMapping("/greeting")
public String greeting() {
return "greeting";
}
}

View File

@@ -0,0 +1,71 @@
package org.springframework.kubernetes.loadbalancer;
import java.util.List;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.spring.framework.kubernetes.loadbalancer.SimpleLoadBalancerApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
@SpringBootTest(classes = {SimpleLoadBalancerApp.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = {"spring.cloud.kubernetes.discovery.all-namespaces=true"})
@RunWith(SpringRunner.class)
public class LoadBalancerAllNamespacesTest {
@Autowired
RestTemplate restTemplate;
@ClassRule
public static KubernetesServer server = new KubernetesServer(true, true);
private static KubernetesClient client;
@BeforeClass
public static void setup() {
client = server.getClient();
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
}
@Test
public void testLoadBalancerDifferentNamespace() {
createTestData("service-b", "b");
String response = restTemplate.getForObject("http://service-b/greeting", String.class);
Assertions.assertNotNull(response);
Assertions.assertEquals("greeting", response);
}
private void createTestData(String name, String namespace) {
client.services().inNamespace(namespace).createNew()
.withNewMetadata().withName(name).withNamespace(namespace).endMetadata()
.withSpec(new ServiceSpecBuilder()
.withPorts(new ServicePortBuilder().withProtocol("TCP").withPort(8080).build())
.build())
.done();
client.endpoints().inNamespace(namespace).createNew()
.withNewMetadata().withName("service-a").withNamespace(namespace).endMetadata()
.addNewSubset()
.addNewAddress().withIp("localhost").endAddress()
.addNewPort().withName("http").withPort(8080).endPort()
.endSubset()
.done();
}
}

View File

@@ -0,0 +1,74 @@
package org.springframework.kubernetes.loadbalancer;
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.spring.framework.kubernetes.loadbalancer.SimpleLoadBalancerApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
@SpringBootTest(classes = {SimpleLoadBalancerApp.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@RunWith(SpringRunner.class)
public class LoadBalancerTest {
@Autowired
RestTemplate restTemplate;
@ClassRule
public static KubernetesServer server = new KubernetesServer(true, true);
private static KubernetesClient client;
@BeforeClass
public static void setup() {
client = server.getClient();
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
}
@Test
public void testLoadBalancerSameNamespace() {
createTestData("service-a", "test");
String response = restTemplate.getForObject("http://service-a/greeting", String.class);
Assertions.assertNotNull(response);
Assertions.assertEquals("greeting", response);
}
@Test
public void testLoadBalancerDifferentNamespace() {
createTestData("service-b", "b");
Assertions.assertThrows(IllegalStateException.class, () -> restTemplate
.getForObject("http://service-b/greeting", String.class));
}
private void createTestData(String name, String namespace) {
client.services().inNamespace(namespace).createNew()
.withNewMetadata().withName(name).endMetadata()
.withSpec(new ServiceSpecBuilder()
.withPorts(new ServicePortBuilder().withProtocol("TCP").withPort(8080).build())
.build())
.done();
client.endpoints().inNamespace(namespace).createNew()
.withNewMetadata().withName("service-a").endMetadata()
.addNewSubset()
.addNewAddress().withIp("localhost").endAddress()
.addNewPort().withName("http").withPort(8080).endPort()
.endSubset()
.done();
}
}

View File

@@ -133,6 +133,7 @@
<module>simple-configmap</module>
<!-- <module>istio</module>-->
<module>discovery</module>
<module>load-balancer</module>
</modules>

View File

@@ -46,6 +46,11 @@
<artifactId>spring-cloud-kubernetes-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>