From 8cd41770165a8ea8f97cc067e2153f713a8d9571 Mon Sep 17 00:00:00 2001 From: erabii Date: Sun, 10 Mar 2024 14:30:38 +0200 Subject: [PATCH] Cleanup load balancer part 1 (#1590) --- .../Fabric8ServiceInstanceMapper.java | 10 +++--- .../Fabric8ServicesListSupplier.java | 11 +++--- ...bernetesClientLoadBalancerApplication.java | 28 ++++++--------- ...rnetesClientLoadBalancerConfiguration.java | 36 +++++++++++++++++++ 4 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerConfiguration.java diff --git a/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapper.java b/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapper.java index 1fb1948e2f..9c4d2eda8d 100644 --- a/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapper.java +++ b/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapper.java @@ -51,8 +51,8 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa @Override public KubernetesServiceInstance map(Service service) { - final ObjectMeta meta = service.getMetadata(); - final List ports = service.getSpec().getPorts(); + ObjectMeta meta = service.getMetadata(); + List ports = service.getSpec().getPorts(); ServicePort port = null; if (ports.size() == 1) { port = ports.get(0); @@ -67,16 +67,16 @@ else if (ports.size() > 1 && Utils.isNotNullOrEmpty(this.properties.getPortName( if (port == null) { return null; } - final String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(), + String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(), service.getMetadata().getNamespace(), properties.getClusterDomain()); - final boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(), + boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(), service.getMetadata().getAnnotations(), port.getName(), port.getPort()); return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(), getServiceMetadata(service), secure); } private Map getServiceMetadata(Service service) { - final Map serviceMetadata = new HashMap<>(); + Map serviceMetadata = new HashMap<>(); KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata(); if (metadataProps.addLabels()) { Map labelMetadata = KubernetesServiceInstanceMapper diff --git a/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServicesListSupplier.java b/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServicesListSupplier.java index 217e15bf67..0972271c56 100644 --- a/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServicesListSupplier.java +++ b/spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServicesListSupplier.java @@ -50,15 +50,14 @@ public class Fabric8ServicesListSupplier extends KubernetesServicesListSupplier< public Flux> get() { List result = new ArrayList<>(); if (discoveryProperties.allNamespaces()) { - List services = this.kubernetesClient.services().inAnyNamespace() - .withField("metadata.name", this.getServiceId()).list().getItems(); + List services = kubernetesClient.services().inAnyNamespace() + .withField("metadata.name", getServiceId()).list().getItems(); services.forEach(service -> result.add(mapper.map(service))); } else { - Service service = StringUtils.hasText(this.kubernetesClient.getNamespace()) - ? this.kubernetesClient.services().inNamespace(this.kubernetesClient.getNamespace()) - .withName(this.getServiceId()).get() - : this.kubernetesClient.services().withName(this.getServiceId()).get(); + Service service = StringUtils.hasText(kubernetesClient.getNamespace()) ? kubernetesClient.services() + .inNamespace(kubernetesClient.getNamespace()).withName(getServiceId()).get() + : kubernetesClient.services().withName(getServiceId()).get(); if (service != null) { result.add(mapper.map(service)); } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerApplication.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerApplication.java index 15983231c4..e6ddeff651 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerApplication.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerApplication.java @@ -19,15 +19,10 @@ import java.util.List; import java.util.Map; -import reactor.netty.http.client.HttpClient; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.cloud.client.loadbalancer.LoadBalanced; -import org.springframework.context.annotation.Bean; import org.springframework.http.HttpMethod; -import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; @@ -38,35 +33,32 @@ @SpringBootApplication @RestController -public class KubernetesClientLoadBalancerApplication { +class KubernetesClientLoadBalancerApplication { private static final String URL = "http://service-wiremock/__admin/mappings"; private final DiscoveryClient discoveryClient; - public KubernetesClientLoadBalancerApplication(DiscoveryClient discoveryClien) { - this.discoveryClient = discoveryClien; + private final WebClient.Builder client; + + KubernetesClientLoadBalancerApplication(DiscoveryClient discoveryClient, WebClient.Builder client) { + this.discoveryClient = discoveryClient; + this.client = client; } public static void main(String[] args) { SpringApplication.run(KubernetesClientLoadBalancerApplication.class, args); } - @Bean - @LoadBalanced - WebClient.Builder client() { - return WebClient.builder(); - } - @GetMapping("/loadbalancer-it/service") @SuppressWarnings("unchecked") - public Map greeting() { - return (Map) client().clientConnector(new ReactorClientHttpConnector(HttpClient.create())) - .baseUrl(URL).build().method(HttpMethod.GET).retrieve().bodyToMono(Map.class).block(); + Map greeting() { + return (Map) client.baseUrl(URL).build().method(HttpMethod.GET).retrieve().bodyToMono(Map.class) + .block(); } @GetMapping("/services") - public List services() { + List services() { return discoveryClient.getServices(); } diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerConfiguration.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerConfiguration.java new file mode 100644 index 0000000000..2c029aa38e --- /dev/null +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/k8s/client/loadbalancer/KubernetesClientLoadBalancerConfiguration.java @@ -0,0 +1,36 @@ +/* + * Copyright 2013-2024 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 + * + * https://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.k8s.client.loadbalancer; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * @author wind57 + */ +@Configuration +class KubernetesClientLoadBalancerConfiguration { + + @Bean + @LoadBalanced + WebClient.Builder client() { + return WebClient.builder(); + } + +}