Skip to content

Commit 8794572

Browse files
committed
started work
Signed-off-by: wind57 <[email protected]>
1 parent 7cc212b commit 8794572

File tree

11 files changed

+163
-131
lines changed

11 files changed

+163
-131
lines changed

spring-cloud-kubernetes-client-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/client/loadbalancer/KubernetesClientServicesListSupplier.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,34 @@ public KubernetesClientServicesListSupplier(Environment environment,
5757

5858
@Override
5959
public Flux<List<ServiceInstance>> get() {
60-
List<ServiceInstance> result = new ArrayList<>();
61-
String serviceName = getServiceId();
62-
LOG.debug(() -> "serviceID : " + serviceName);
63-
64-
if (discoveryProperties.allNamespaces()) {
65-
LOG.debug(() -> "discovering services in all namespaces");
66-
List<V1Service> services = services(null, serviceName);
67-
services.forEach(service -> addMappedService(mapper, result, service));
68-
}
69-
else if (!discoveryProperties.namespaces().isEmpty()) {
70-
List<String> selectiveNamespaces = discoveryProperties.namespaces().stream().sorted().toList();
71-
LOG.debug(() -> "discovering services in selective namespaces : " + selectiveNamespaces);
72-
selectiveNamespaces.forEach(selectiveNamespace -> {
73-
List<V1Service> services = services(selectiveNamespace, serviceName);
60+
return Flux.defer(() -> {
61+
List<ServiceInstance> result = new ArrayList<>();
62+
String serviceName = getServiceId();
63+
LOG.debug(() -> "serviceID : " + serviceName);
64+
65+
if (discoveryProperties.allNamespaces()) {
66+
LOG.debug(() -> "discovering services in all namespaces");
67+
List<V1Service> services = services(null, serviceName);
7468
services.forEach(service -> addMappedService(mapper, result, service));
75-
});
76-
}
77-
else {
78-
String namespace = getApplicationNamespace(null, "loadbalancer-service", kubernetesNamespaceProvider);
79-
LOG.debug(() -> "discovering services in namespace : " + namespace);
80-
List<V1Service> services = services(namespace, serviceName);
81-
services.forEach(service -> addMappedService(mapper, result, service));
82-
}
69+
}
70+
else if (!discoveryProperties.namespaces().isEmpty()) {
71+
List<String> selectiveNamespaces = discoveryProperties.namespaces().stream().sorted().toList();
72+
LOG.debug(() -> "discovering services in selective namespaces : " + selectiveNamespaces);
73+
selectiveNamespaces.forEach(selectiveNamespace -> {
74+
List<V1Service> services = services(selectiveNamespace, serviceName);
75+
services.forEach(service -> addMappedService(mapper, result, service));
76+
});
77+
}
78+
else {
79+
String namespace = getApplicationNamespace(null, "loadbalancer-service", kubernetesNamespaceProvider);
80+
LOG.debug(() -> "discovering services in namespace : " + namespace);
81+
List<V1Service> services = services(namespace, serviceName);
82+
services.forEach(service -> addMappedService(mapper, result, service));
83+
}
8384

84-
LOG.debug(() -> "found services : " + result);
85-
return Flux.just(result);
85+
LOG.debug(() -> "found services : " + result);
86+
return Flux.just(result);
87+
});
8688
}
8789

8890
private void addMappedService(KubernetesServiceInstanceMapper<V1Service> mapper, List<ServiceInstance> services,

spring-cloud-kubernetes-fabric8-loadbalancer/src/main/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServicesListSupplier.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,51 +57,53 @@ public class Fabric8ServicesListSupplier extends KubernetesServicesListSupplier<
5757

5858
@Override
5959
public Flux<List<ServiceInstance>> get() {
60-
List<ServiceInstance> result = new ArrayList<>();
61-
String serviceName = getServiceId();
62-
LOG.debug(() -> "serviceID : " + serviceName);
60+
return Flux.defer(() -> {
61+
List<ServiceInstance> result = new ArrayList<>();
62+
String serviceName = getServiceId();
63+
LOG.debug(() -> "serviceID : " + serviceName);
6364

64-
if (discoveryProperties.allNamespaces()) {
65-
LOG.debug(() -> "discovering services in all namespaces");
66-
List<Service> services = kubernetesClient.services()
67-
.inAnyNamespace()
68-
.withField("metadata.name", serviceName)
69-
.list()
70-
.getItems();
71-
services.forEach(service -> addMappedService(mapper, result, service));
72-
}
73-
else if (!discoveryProperties.namespaces().isEmpty()) {
74-
List<String> selectiveNamespaces = discoveryProperties.namespaces().stream().sorted().toList();
75-
LOG.debug(() -> "discovering services in selective namespaces : " + selectiveNamespaces);
76-
selectiveNamespaces.forEach(selectiveNamespace -> {
77-
Service service = kubernetesClient.services()
78-
.inNamespace(selectiveNamespace)
79-
.withName(serviceName)
80-
.get();
65+
if (discoveryProperties.allNamespaces()) {
66+
LOG.debug(() -> "discovering services in all namespaces");
67+
List<Service> services = kubernetesClient.services()
68+
.inAnyNamespace()
69+
.withField("metadata.name", serviceName)
70+
.list()
71+
.getItems();
72+
services.forEach(service -> addMappedService(mapper, result, service));
73+
}
74+
else if (!discoveryProperties.namespaces().isEmpty()) {
75+
List<String> selectiveNamespaces = discoveryProperties.namespaces().stream().sorted().toList();
76+
LOG.debug(() -> "discovering services in selective namespaces : " + selectiveNamespaces);
77+
selectiveNamespaces.forEach(selectiveNamespace -> {
78+
Service service = kubernetesClient.services()
79+
.inNamespace(selectiveNamespace)
80+
.withName(serviceName)
81+
.get();
82+
if (service != null) {
83+
addMappedService(mapper, result, service);
84+
}
85+
else {
86+
LOG.debug(() -> "did not find service with name : " + serviceName + " in namespace : "
87+
+ selectiveNamespace);
88+
}
89+
});
90+
}
91+
else {
92+
String namespace = Fabric8Utils.getApplicationNamespace(kubernetesClient, null, "loadbalancer-service",
93+
namespaceProvider);
94+
LOG.debug(() -> "discovering services in namespace : " + namespace);
95+
Service service = kubernetesClient.services().inNamespace(namespace).withName(serviceName).get();
8196
if (service != null) {
8297
addMappedService(mapper, result, service);
8398
}
8499
else {
85-
LOG.debug(() -> "did not find service with name : " + serviceName + " in namespace : "
86-
+ selectiveNamespace);
100+
LOG.debug(() -> "did not find service with name : " + serviceName + " in namespace : " + namespace);
87101
}
88-
});
89-
}
90-
else {
91-
String namespace = Fabric8Utils.getApplicationNamespace(kubernetesClient, null, "loadbalancer-service",
92-
namespaceProvider);
93-
LOG.debug(() -> "discovering services in namespace : " + namespace);
94-
Service service = kubernetesClient.services().inNamespace(namespace).withName(serviceName).get();
95-
if (service != null) {
96-
addMappedService(mapper, result, service);
97-
}
98-
else {
99-
LOG.debug(() -> "did not find service with name : " + serviceName + " in namespace : " + namespace);
100102
}
101-
}
102103

103-
LOG.debug(() -> "found services : " + result);
104-
return Flux.just(result);
104+
LOG.debug(() -> "found services : " + result);
105+
return Flux.just(result);
106+
});
105107
}
106108

107109
private void addMappedService(KubernetesServiceInstanceMapper<Service> mapper, List<ServiceInstance> services,

spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/it/Util.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@
2727
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
2828
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
2929

30-
import org.springframework.boot.SpringApplication;
31-
import org.springframework.boot.autoconfigure.SpringBootApplication;
32-
import org.springframework.boot.test.context.TestConfiguration;
33-
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
34-
import org.springframework.context.annotation.Bean;
35-
import org.springframework.web.reactive.function.client.WebClient;
36-
3730
/**
3831
* @author wind57
3932
*/
@@ -63,24 +56,4 @@ public static Endpoints endpoints(int port, String host, String namespace) {
6356
.build();
6457
}
6558

66-
@TestConfiguration
67-
public static class LoadBalancerConfiguration {
68-
69-
@Bean
70-
@LoadBalanced
71-
WebClient.Builder client() {
72-
return WebClient.builder();
73-
}
74-
75-
}
76-
77-
@SpringBootApplication
78-
public static class Configuration {
79-
80-
public static void main(String[] args) {
81-
SpringApplication.run(Configuration.class);
82-
}
83-
84-
}
85-
8659
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.mode;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class App {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(App.class);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.mode;
18+
19+
import org.springframework.boot.test.context.TestConfiguration;
20+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.web.reactive.function.client.WebClient;
23+
24+
@TestConfiguration
25+
public class LoadBalancerConfiguration {
26+
27+
@Bean
28+
@LoadBalanced
29+
WebClient.Builder client() {
30+
return WebClient.builder();
31+
}
32+
33+
}

spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/it/mode/pod/AllNamespacesTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,11 +30,12 @@
3030
import org.mockito.MockedStatic;
3131
import org.mockito.Mockito;
3232

33-
import org.springframework.beans.factory.ObjectProvider;
3433
import org.springframework.beans.factory.annotation.Autowired;
3534
import org.springframework.boot.test.context.SpringBootTest;
3635
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
3736
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.Util;
37+
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.mode.App;
38+
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.mode.LoadBalancerConfiguration;
3839
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
3940
import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier;
4041
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
@@ -43,16 +44,14 @@
4344
import org.springframework.web.reactive.function.client.WebClient;
4445

4546
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
46-
import static org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.Util.Configuration;
47-
import static org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.Util.LoadBalancerConfiguration;
4847

4948
/**
5049
* @author wind57
5150
*/
5251
@SpringBootTest(
5352
properties = { "spring.cloud.kubernetes.loadbalancer.mode=POD", "spring.main.cloud-platform=KUBERNETES",
5453
"spring.cloud.kubernetes.discovery.all-namespaces=true" },
55-
classes = { LoadBalancerConfiguration.class, Configuration.class })
54+
classes = { LoadBalancerConfiguration.class, App.class })
5655
class AllNamespacesTest {
5756

5857
private static final String SERVICE_A_URL = "http://service-a";
@@ -69,14 +68,15 @@ class AllNamespacesTest {
6968

7069
private static WireMockServer serviceBMockServer;
7170

71+
@SuppressWarnings("rawtypes")
7272
private static final MockedStatic<KubernetesServiceInstanceMapper> MOCKED_STATIC = Mockito
7373
.mockStatic(KubernetesServiceInstanceMapper.class);
7474

7575
@Autowired
7676
private WebClient.Builder builder;
7777

7878
@Autowired
79-
private ObjectProvider<LoadBalancerClientFactory> loadBalancerClientFactory;
79+
private LoadBalancerClientFactory loadBalancerClientFactory;
8080

8181
@BeforeAll
8282
static void beforeAll() {
@@ -177,7 +177,6 @@ void test() {
177177
Assertions.assertThat(serviceBResult).isEqualTo("service-b-reached");
178178

179179
CachingServiceInstanceListSupplier supplier = (CachingServiceInstanceListSupplier) loadBalancerClientFactory
180-
.getIfAvailable()
181180
.getProvider("service-a", ServiceInstanceListSupplier.class)
182181
.getIfAvailable();
183182
Assertions.assertThat(supplier.getDelegate().getClass())

spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/it/mode/pod/SelectiveNamespacesTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2024 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,11 +30,12 @@
3030
import org.mockito.MockedStatic;
3131
import org.mockito.Mockito;
3232

33-
import org.springframework.beans.factory.ObjectProvider;
3433
import org.springframework.beans.factory.annotation.Autowired;
3534
import org.springframework.boot.test.context.SpringBootTest;
3635
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
3736
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.Util;
37+
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.mode.App;
38+
import org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.mode.LoadBalancerConfiguration;
3839
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
3940
import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier;
4041
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
@@ -43,16 +44,14 @@
4344
import org.springframework.web.reactive.function.client.WebClient;
4445

4546
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
46-
import static org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.Util.Configuration;
47-
import static org.springframework.cloud.kubernetes.fabric8.loadbalancer.it.Util.LoadBalancerConfiguration;
4847

4948
/**
5049
* @author wind57
5150
*/
5251
@SpringBootTest(properties = { "spring.cloud.kubernetes.loadbalancer.mode=POD", "spring.main.cloud-platform=KUBERNETES",
5352
"spring.cloud.kubernetes.discovery.all-namespaces=false", "spring.cloud.kubernetes.discovery.namespaces.[0]=a",
5453
"spring.cloud.kubernetes.discovery.namespaces.[1]=b" },
55-
classes = { LoadBalancerConfiguration.class, Configuration.class })
54+
classes = { LoadBalancerConfiguration.class, App.class })
5655
class SelectiveNamespacesTest {
5756

5857
private static final String MY_SERVICE_URL = "http://my-service";
@@ -71,14 +70,15 @@ class SelectiveNamespacesTest {
7170

7271
private static WireMockServer serviceCMockServer;
7372

73+
@SuppressWarnings("rawtypes")
7474
private static final MockedStatic<KubernetesServiceInstanceMapper> MOCKED_STATIC = Mockito
7575
.mockStatic(KubernetesServiceInstanceMapper.class);
7676

7777
@Autowired
7878
private WebClient.Builder builder;
7979

8080
@Autowired
81-
private ObjectProvider<LoadBalancerClientFactory> loadBalancerClientFactory;
81+
private LoadBalancerClientFactory loadBalancerClientFactory;
8282

8383
@BeforeAll
8484
static void beforeAll() {
@@ -213,7 +213,6 @@ void test() {
213213
}
214214

215215
CachingServiceInstanceListSupplier supplier = (CachingServiceInstanceListSupplier) loadBalancerClientFactory
216-
.getIfAvailable()
217216
.getProvider("my-service", ServiceInstanceListSupplier.class)
218217
.getIfAvailable();
219218
Assertions.assertThat(supplier.getDelegate().getClass())

0 commit comments

Comments
 (0)