Skip to content

Commit a051455

Browse files
committed
fix
Signed-off-by: wind57 <[email protected]>
1 parent 11bbd7a commit a051455

File tree

5 files changed

+104
-56
lines changed

5 files changed

+104
-56
lines changed

spring-cloud-kubernetes-client-discovery/src/main/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerReactiveDiscoveryClientAutoConfiguration.java

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
3131
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3233
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3334
import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration;
3435
import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration;
@@ -64,35 +65,78 @@ final class KubernetesClientInformerReactiveDiscoveryClientAutoConfiguration {
6465
private static final LogAccessor LOG = new LogAccessor(
6566
LogFactory.getLog(KubernetesClientInformerReactiveDiscoveryClientAutoConfiguration.class));
6667

67-
// in case blocking is disabled
6868
@Bean
6969
@ConditionalOnMissingBean
70-
KubernetesClientInformerDiscoveryClient kubernetesClientInformerDiscoveryClientForReactiveImplementation(
70+
@ConditionalOnDiscoveryCacheableReactiveDisabled
71+
KubernetesClientInformerReactiveDiscoveryClient kubernetesClientReactiveDiscoveryClient(
7172
List<SharedInformerFactory> sharedInformerFactories, List<Lister<V1Service>> serviceListers,
7273
List<Lister<V1Endpoints>> endpointsListers, List<SharedInformer<V1Service>> serviceInformers,
7374
List<SharedInformer<V1Endpoints>> endpointsInformers, KubernetesDiscoveryProperties properties,
7475
CoreV1Api coreV1Api, Predicate<V1Service> predicate) {
7576

76-
return new KubernetesClientInformerDiscoveryClient(sharedInformerFactories, serviceListers, endpointsListers,
77-
serviceInformers, endpointsInformers, properties, coreV1Api, predicate);
77+
KubernetesClientInformerDiscoveryClient blockingClient = new KubernetesClientInformerDiscoveryClient(
78+
sharedInformerFactories, serviceListers, endpointsListers, serviceInformers, endpointsInformers,
79+
properties, coreV1Api, predicate);
80+
81+
return new KubernetesClientInformerReactiveDiscoveryClient(blockingClient);
7882
}
7983

8084
@Bean
81-
@ConditionalOnMissingBean
82-
@ConditionalOnDiscoveryCacheableReactiveDisabled
83-
KubernetesClientInformerReactiveDiscoveryClient kubernetesClientReactiveDiscoveryClient(
84-
KubernetesClientInformerDiscoveryClient kubernetesClientInformerDiscoveryClient) {
85-
return new KubernetesClientInformerReactiveDiscoveryClient(kubernetesClientInformerDiscoveryClient);
85+
@ConditionalOnBean(KubernetesClientInformerReactiveDiscoveryClient.class)
86+
@ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer
87+
ReactiveDiscoveryClientHealthIndicator kubernetesReactiveDiscoveryClientHealthIndicator(
88+
KubernetesClientInformerReactiveDiscoveryClient reactiveClient,
89+
DiscoveryClientHealthIndicatorProperties properties) {
90+
return new ReactiveDiscoveryClientHealthIndicator(reactiveClient, properties);
8691
}
8792

93+
// Above two beans are created when cacheable is disabled
94+
8895
@Bean
8996
@ConditionalOnMissingBean
9097
@ConditionalOnDiscoveryCacheableReactiveEnabled
9198
KubernetesClientCacheableInformerReactiveDiscoveryClient kubernetesClientCacheableReactiveDiscoveryClient(
92-
KubernetesClientInformerDiscoveryClient kubernetesClientInformerDiscoveryClient) {
93-
return new KubernetesClientCacheableInformerReactiveDiscoveryClient(kubernetesClientInformerDiscoveryClient);
99+
List<SharedInformerFactory> sharedInformerFactories, List<Lister<V1Service>> serviceListers,
100+
List<Lister<V1Endpoints>> endpointsListers, List<SharedInformer<V1Service>> serviceInformers,
101+
List<SharedInformer<V1Endpoints>> endpointsInformers, KubernetesDiscoveryProperties properties,
102+
CoreV1Api coreV1Api, Predicate<V1Service> predicate) {
103+
104+
KubernetesClientInformerDiscoveryClient blockingClient = new KubernetesClientInformerDiscoveryClient(
105+
sharedInformerFactories, serviceListers, endpointsListers, serviceInformers, endpointsInformers,
106+
properties, coreV1Api, predicate);
107+
108+
return new KubernetesClientCacheableInformerReactiveDiscoveryClient(blockingClient);
109+
}
110+
111+
@Bean
112+
@ConditionalOnMissingBean(KubernetesClientInformerReactiveDiscoveryClient.class)
113+
@ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer
114+
ReactiveDiscoveryClientHealthIndicator reactiveDiscoveryClientHealthIndicator(
115+
List<SharedInformerFactory> sharedInformerFactories, List<Lister<V1Service>> serviceListers,
116+
List<Lister<V1Endpoints>> endpointsListers, List<SharedInformer<V1Service>> serviceInformers,
117+
List<SharedInformer<V1Endpoints>> endpointsInformers,
118+
KubernetesDiscoveryProperties kubernetesDiscoveryProperties, CoreV1Api coreV1Api,
119+
Predicate<V1Service> predicate, DiscoveryClientHealthIndicatorProperties properties) {
120+
121+
KubernetesClientInformerDiscoveryClient blockingClient = new KubernetesClientInformerDiscoveryClient(
122+
sharedInformerFactories, serviceListers, endpointsListers, serviceInformers, endpointsInformers,
123+
kubernetesDiscoveryProperties, coreV1Api, predicate);
124+
125+
KubernetesClientInformerReactiveDiscoveryClient reactiveClient = new KubernetesClientInformerReactiveDiscoveryClient(
126+
blockingClient);
127+
128+
return new ReactiveDiscoveryClientHealthIndicator(reactiveClient, properties);
94129
}
95130

131+
// Above two beans are created when cacheable is enabled. In this case, we can't make
132+
// KubernetesClientInformerDiscoveryClient a @Bean, since blocking discovery might be
133+
// disabled and we do
134+
// not want to allow wiring of it. Nevertheless, we still need an instance of
135+
// KubernetesClientInformerDiscoveryClient
136+
// in order to create the ReactiveDiscoveryClientHealthIndicator and
137+
// KubernetesClientCacheableInformerReactiveDiscoveryClient
138+
// As such, we create two of such instances in each bean.
139+
96140
/**
97141
* Post an event so that health indicator is initialized.
98142
*/
@@ -104,15 +148,4 @@ KubernetesDiscoveryClientHealthIndicatorInitializer reactiveIndicatorInitializer
104148
return new KubernetesDiscoveryClientHealthIndicatorInitializer(podUtils, applicationEventPublisher);
105149
}
106150

107-
/**
108-
* unlike the blocking implementation, we need to register the health indicator.
109-
*/
110-
@Bean
111-
@ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer
112-
ReactiveDiscoveryClientHealthIndicator kubernetesReactiveDiscoveryClientHealthIndicator(
113-
KubernetesClientInformerReactiveDiscoveryClient client,
114-
DiscoveryClientHealthIndicatorProperties properties) {
115-
return new ReactiveDiscoveryClientHealthIndicator(client, properties);
116-
}
117-
118151
}

spring-cloud-kubernetes-client-discovery/src/test/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerDiscoveryClientAutoConfigurationApplicationContextTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ void kubernetesDiscoveryBlockingDisabled() {
280280
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
281281
"spring.cloud.discovery.blocking.enabled=false", "spring.cloud.kubernetes.client.namespace=default");
282282
applicationContextRunner.run(context -> {
283-
assertThat(context).hasSingleBean(KubernetesClientInformerDiscoveryClient.class);
284-
// only the implementation for the reactive
285-
assertThat(context).hasBean("kubernetesClientInformerDiscoveryClientForReactiveImplementation");
283+
assertThat(context).doesNotHaveBean(KubernetesClientInformerDiscoveryClient.class);
286284
assertThat(context).hasSingleBean(KubernetesClientInformerReactiveDiscoveryClient.class);
287285

288286
assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class);
@@ -301,9 +299,7 @@ void kubernetesDiscoveryBlockingDisabledWithSelectiveNamespaces() {
301299
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
302300
"spring.cloud.discovery.blocking.enabled=false", "spring.cloud.kubernetes.discovery.namespaces=a,b");
303301
applicationContextRunner.run(context -> {
304-
assertThat(context).hasSingleBean(KubernetesClientInformerDiscoveryClient.class);
305-
// only the implementation for the reactive
306-
assertThat(context).hasBean("kubernetesClientInformerDiscoveryClientForReactiveImplementation");
302+
assertThat(context).doesNotHaveBean(KubernetesClientInformerDiscoveryClient.class);
307303
assertThat(context).hasSingleBean(KubernetesClientInformerReactiveDiscoveryClient.class);
308304

309305
assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class);
@@ -517,7 +513,7 @@ void blockingCacheableEnabled() {
517513
"spring.cloud.kubernetes.discovery.cacheable.blocking.enabled=true",
518514
"spring.cloud.kubernetes.client.namespace=default");
519515
applicationContextRunner.run(context -> {
520-
assertThat(context).hasSingleBean(KubernetesClientInformerDiscoveryClient.class);
516+
assertThat(context).doesNotHaveBean(KubernetesClientInformerDiscoveryClient.class);
521517
assertThat(context).hasSingleBean(KubernetesClientCacheableInformerDiscoveryClient.class);
522518
});
523519
}

spring-cloud-kubernetes-client-discovery/src/test/java/org/springframework/cloud/kubernetes/client/discovery/KubernetesClientInformerReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ void blockingDisabled() {
289289
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
290290
"spring.cloud.discovery.blocking.enabled=false", "spring.cloud.kubernetes.client.namespace=default");
291291
applicationContextRunner.run(context -> {
292-
assertThat(context).hasSingleBean(KubernetesClientInformerDiscoveryClient.class);
293-
// only the implementation for the reactive
294-
assertThat(context).hasBean("kubernetesClientInformerDiscoveryClientForReactiveImplementation");
292+
assertThat(context).doesNotHaveBean(KubernetesClientInformerDiscoveryClient.class);
295293
assertThat(context).hasSingleBean(KubernetesClientInformerReactiveDiscoveryClient.class);
296294

297295
// simple from commons and ours
@@ -310,9 +308,7 @@ void blockingDisabledWithSelectiveNamespaces() {
310308
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
311309
"spring.cloud.discovery.blocking.enabled=false", "spring.cloud.kubernetes.discovery.namespaces=a,b");
312310
applicationContextRunner.run(context -> {
313-
assertThat(context).hasSingleBean(KubernetesClientInformerDiscoveryClient.class);
314-
// only the implementation for the reactive
315-
assertThat(context).hasBean("kubernetesClientInformerDiscoveryClientForReactiveImplementation");
311+
assertThat(context).doesNotHaveBean(KubernetesClientInformerDiscoveryClient.class);
316312
assertThat(context).hasSingleBean(KubernetesClientInformerReactiveDiscoveryClient.class);
317313

318314
// simple from commons and ours
@@ -430,10 +426,11 @@ void reactiveCacheableDisabled() {
430426
* - cacheable in the reactive implementation = true, as such:
431427
* - KubernetesClientInformerReactiveDiscoveryClient is not present
432428
* - KubernetesClientCacheableInformerReactiveDiscoveryClient is present
429+
* - Health indicator is disabled
433430
* </pre>
434431
*/
435432
@Test
436-
void reactiveCacheableEnabled() {
433+
void reactiveCacheableEnabledWithoutHealthIndicator() {
437434
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
438435
"spring.cloud.kubernetes.discovery.cacheable.reactive.enabled=true",
439436
"spring.cloud.discovery.client.health-indicator.enabled=false",
@@ -444,6 +441,26 @@ void reactiveCacheableEnabled() {
444441
});
445442
}
446443

444+
/**
445+
* <pre>
446+
* - cacheable in the reactive implementation = true, as such:
447+
* - KubernetesClientInformerReactiveDiscoveryClient is not present
448+
* - KubernetesClientCacheableInformerReactiveDiscoveryClient is present
449+
* - Health indicator is enabled
450+
* </pre>
451+
*/
452+
@Test
453+
void reactiveCacheableEnabledWithHealthIndicator() {
454+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
455+
"spring.cloud.kubernetes.discovery.cacheable.reactive.enabled=true",
456+
"spring.cloud.discovery.client.health-indicator.enabled=true",
457+
"spring.cloud.kubernetes.client.namespace=default");
458+
applicationContextRunner.run(context -> {
459+
assertThat(context).doesNotHaveBean(KubernetesClientInformerReactiveDiscoveryClient.class);
460+
assertThat(context).hasSingleBean(KubernetesClientCacheableInformerReactiveDiscoveryClient.class);
461+
});
462+
}
463+
447464
private void setup(String... properties) {
448465
applicationContextRunner = new ApplicationContextRunner()
449466
.withConfiguration(AutoConfigurations.of(

spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfiguration.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ Fabric8ReactiveDiscoveryClient fabric8ReactiveDiscoveryClient(KubernetesClient c
8282
@ConditionalOnBean(Fabric8ReactiveDiscoveryClient.class)
8383
@ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer
8484
ReactiveDiscoveryClientHealthIndicator kubernetesReactiveDiscoveryClientHealthIndicator(
85-
Fabric8ReactiveDiscoveryClient client, DiscoveryClientHealthIndicatorProperties properties) {
86-
return new ReactiveDiscoveryClientHealthIndicator(client, properties);
85+
Fabric8ReactiveDiscoveryClient reactiveClient, DiscoveryClientHealthIndicatorProperties properties) {
86+
return new ReactiveDiscoveryClientHealthIndicator(reactiveClient, properties);
8787
}
8888

8989
// Above two beans are created when cacheable is disabled
@@ -95,34 +95,36 @@ Fabric8CacheableReactiveDiscoveryClient fabric8CacheableReactiveDiscoveryClient(
9595
KubernetesDiscoveryProperties properties, Predicate<Service> predicate, Environment environment) {
9696
ServicePortSecureResolver servicePortSecureResolver = new ServicePortSecureResolver(properties);
9797
KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(environment);
98-
Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(client, properties,
98+
Fabric8DiscoveryClient blockingClient = new Fabric8DiscoveryClient(client, properties,
9999
servicePortSecureResolver, namespaceProvider, predicate);
100-
return new Fabric8CacheableReactiveDiscoveryClient(fabric8DiscoveryClient);
100+
return new Fabric8CacheableReactiveDiscoveryClient(blockingClient);
101101
}
102102

103103
@Bean
104104
@ConditionalOnMissingBean(Fabric8ReactiveDiscoveryClient.class)
105105
@ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer
106-
ReactiveDiscoveryClientHealthIndicator kubernetesReactiveClientHealthIndicator(
107-
KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties,
108-
Predicate<Service> predicate, DiscoveryClientHealthIndicatorProperties properties, Environment environment) {
106+
ReactiveDiscoveryClientHealthIndicator kubernetesReactiveClientHealthIndicator(KubernetesClient client,
107+
KubernetesDiscoveryProperties kubernetesDiscoveryProperties, Predicate<Service> predicate,
108+
DiscoveryClientHealthIndicatorProperties properties, Environment environment) {
109109

110110
KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(environment);
111-
ServicePortSecureResolver servicePortSecureResolver = new ServicePortSecureResolver(kubernetesDiscoveryProperties);
112-
Fabric8DiscoveryClient fabric8DiscoveryClient =
113-
new Fabric8DiscoveryClient(client, kubernetesDiscoveryProperties,
114-
servicePortSecureResolver, namespaceProvider, predicate);
111+
ServicePortSecureResolver servicePortSecureResolver = new ServicePortSecureResolver(
112+
kubernetesDiscoveryProperties);
113+
Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(client,
114+
kubernetesDiscoveryProperties, servicePortSecureResolver, namespaceProvider, predicate);
115115

116-
Fabric8ReactiveDiscoveryClient fabric8ReactiveDiscoveryClient =
117-
new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient);
116+
Fabric8ReactiveDiscoveryClient reactiveClient = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient);
118117

119-
return new ReactiveDiscoveryClientHealthIndicator(fabric8ReactiveDiscoveryClient, properties);
118+
return new ReactiveDiscoveryClientHealthIndicator(reactiveClient, properties);
120119
}
121120

122121
// Above two beans are created when cacheable is enabled. In this case, we can't make
123-
// Fabric8DiscoveryClient a @Bean, since blocking discovery might be disabled and we do
124-
// not want to allow wiring of it. Nevertheless, we still need an instance of Fabric8DiscoveryClient
125-
// in order to create the ReactiveDiscoveryClientHealthIndicator and Fabric8CacheableReactiveDiscoveryClient
122+
// Fabric8DiscoveryClient a @Bean, since blocking discovery might be disabled and we
123+
// do
124+
// not want to allow wiring of it. Nevertheless, we still need an instance of
125+
// Fabric8DiscoveryClient
126+
// in order to create the ReactiveDiscoveryClientHealthIndicator and
127+
// Fabric8CacheableReactiveDiscoveryClient
126128
// As such, we create two of such instances in each bean.
127129

128130
/**
@@ -132,7 +134,7 @@ ReactiveDiscoveryClientHealthIndicator kubernetesReactiveClientHealthIndicator(
132134
@ConditionalOnClass(name = "org.springframework.boot.health.contributor.ReactiveHealthIndicator")
133135
@ConditionalOnDiscoveryHealthIndicatorEnabled
134136
KubernetesDiscoveryClientHealthIndicatorInitializer reactiveIndicatorInitializer(
135-
ApplicationEventPublisher applicationEventPublisher, PodUtils<?> podUtils) {
137+
ApplicationEventPublisher applicationEventPublisher, PodUtils<?> podUtils) {
136138
LOG.debug(() -> "Will publish InstanceRegisteredEvent from reactive implementation");
137139
return new KubernetesDiscoveryClientHealthIndicatorInitializer(podUtils, applicationEventPublisher);
138140
}

spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ void reactiveCacheableEnabledWithoutHealthIndicator() {
205205
@Test
206206
void reactiveCacheableEnabledWithHealthIndicator() {
207207
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false",
208-
"spring.cloud.kubernetes.discovery.cacheable.reactive.enabled=true",
209-
"spring.cloud.discovery.client.health-indicator.enabled=true");
208+
"spring.cloud.kubernetes.discovery.cacheable.reactive.enabled=true",
209+
"spring.cloud.discovery.client.health-indicator.enabled=true");
210210
applicationContextRunner.run(context -> {
211211
assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class);
212212
assertThat(context).hasSingleBean(Fabric8CacheableReactiveDiscoveryClient.class);

0 commit comments

Comments
 (0)