Skip to content

Commit

Permalink
Cleanup load balancer part 1 (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Mar 10, 2024
1 parent a41000b commit 8cd4177
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa

@Override
public KubernetesServiceInstance map(Service service) {
final ObjectMeta meta = service.getMetadata();
final List<ServicePort> ports = service.getSpec().getPorts();
ObjectMeta meta = service.getMetadata();
List<ServicePort> ports = service.getSpec().getPorts();
ServicePort port = null;
if (ports.size() == 1) {
port = ports.get(0);
Expand All @@ -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<String, String> getServiceMetadata(Service service) {
final Map<String, String> serviceMetadata = new HashMap<>();
Map<String, String> serviceMetadata = new HashMap<>();
KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata();
if (metadataProps.addLabels()) {
Map<String, String> labelMetadata = KubernetesServiceInstanceMapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ public class Fabric8ServicesListSupplier extends KubernetesServicesListSupplier<
public Flux<List<ServiceInstance>> get() {
List<ServiceInstance> result = new ArrayList<>();
if (discoveryProperties.allNamespaces()) {
List<Service> services = this.kubernetesClient.services().inAnyNamespace()
.withField("metadata.name", this.getServiceId()).list().getItems();
List<Service> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Object> greeting() {
return (Map<String, Object>) client().clientConnector(new ReactorClientHttpConnector(HttpClient.create()))
.baseUrl(URL).build().method(HttpMethod.GET).retrieve().bodyToMono(Map.class).block();
Map<String, Object> greeting() {
return (Map<String, Object>) client.baseUrl(URL).build().method(HttpMethod.GET).retrieve().bodyToMono(Map.class)
.block();
}

@GetMapping("/services")
public List<String> services() {
List<String> services() {
return discoveryClient.getServices();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 8cd4177

Please sign in to comment.