Cleanup load balancer part 3 (#1593)

This commit is contained in:
erabii
2024-03-13 20:35:31 +02:00
committed by GitHub
parent 0f02596a98
commit fa07ce68d5
2 changed files with 38 additions and 4 deletions

View File

@@ -18,8 +18,12 @@ package org.springframework.cloud.kubernetes.k8s.client.loadbalancer;
import java.util.Map;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -37,8 +41,12 @@ class KubernetesClientLoadBalancerApplication {
private final WebClient.Builder client;
KubernetesClientLoadBalancerApplication(WebClient.Builder client) {
private final ObjectProvider<LoadBalancerClientFactory> loadBalancerClientFactory;
KubernetesClientLoadBalancerApplication(WebClient.Builder client,
ObjectProvider<LoadBalancerClientFactory> loadBalancerClientFactory) {
this.client = client;
this.loadBalancerClientFactory = loadBalancerClientFactory;
}
public static void main(String[] args) {
@@ -52,4 +60,14 @@ class KubernetesClientLoadBalancerApplication {
.block();
}
@GetMapping("/loadbalancer-it/supplier")
String supplier() {
ServiceInstanceListSupplier supplier = loadBalancerClientFactory.getIfAvailable()
.getProvider("service-wiremock", ServiceInstanceListSupplier.class).getIfAvailable();
if (supplier instanceof CachingServiceInstanceListSupplier cachingSupplier) {
return cachingSupplier.getDelegate().getClass().getSimpleName();
}
return supplier.getClass().getSimpleName();
}
}

View File

@@ -34,9 +34,11 @@ import org.testcontainers.k3s.K3sContainer;
import reactor.netty.http.client.HttpClient;
import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.cloud.kubernetes.client.loadbalancer.KubernetesClientServicesListSupplier;
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
import org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util;
import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
@@ -76,6 +78,8 @@ class LoadBalancerIT {
private static final String SERVICE_URL = "http://localhost:80/loadbalancer-it/service";
private static final String SERVICE_INSTANCE_LIST_SUPPLIER_URL = "http://localhost:80/loadbalancer-it/supplier";
private static final String SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME = "spring-cloud-kubernetes-k8s-client-loadbalancer";
private static final String NAMESPACE = "default";
@@ -114,17 +118,17 @@ class LoadBalancerIT {
@Test
@Order(1)
void testLoadBalancerPodMode() {
testLoadBalancer();
testLoadBalancer(true);
}
@Test
@Order(2)
void testLoadBalancerServiceMode() {
patchForServiceMode();
testLoadBalancer();
testLoadBalancer(false);
}
private void testLoadBalancer() {
private void testLoadBalancer(boolean podMode) {
WebClient.Builder builder = builder();
WebClient serviceClient = builder.baseUrl(SERVICE_URL).build();
@@ -133,6 +137,18 @@ class LoadBalancerIT {
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathArrayValue("$.mappings").isEmpty();
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathNumberValue("$.meta.total")
.isEqualTo(0);
WebClient.Builder supplierBuilder = builder();
WebClient supplierServiceClient = supplierBuilder.baseUrl(SERVICE_INSTANCE_LIST_SUPPLIER_URL).build();
String supplierResult = supplierServiceClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.block();
if (podMode) {
Assertions.assertThat(supplierResult)
.isEqualTo(DiscoveryClientServiceInstanceListSupplier.class.getSimpleName());
}
else {
Assertions.assertThat(supplierResult).isEqualTo(KubernetesClientServicesListSupplier.class.getSimpleName());
}
}
private static void loadbalancerIt(Phase phase) {