Simplify auto-configurations for kubernetes discovery (#1508)
This commit is contained in:
@@ -43,6 +43,8 @@ import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
* @deprecated in favor of {@link KubernetesDiscoveryClientBlockingAutoConfiguration} and
|
||||
* {@link KubernetesDiscoveryClientReactiveAutoConfiguration}
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
@@ -50,6 +52,7 @@ import org.springframework.web.reactive.function.client.WebClient;
|
||||
@ConditionalOnKubernetesDiscoveryEnabled
|
||||
@EnableConfigurationProperties({ DiscoveryClientHealthIndicatorProperties.class,
|
||||
KubernetesDiscoveryClientProperties.class })
|
||||
@Deprecated(forRemoval = true)
|
||||
public class KubernetesDiscoveryClientAutoConfiguration {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2013-2023 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.discovery;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.boot.cloud.CloudPlatform;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.ConditionalOnDiscoveryHealthIndicatorEnabled;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIndicatorProperties;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnKubernetesDiscoveryEnabled;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
@ConditionalOnKubernetesDiscoveryEnabled
|
||||
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
|
||||
@EnableConfigurationProperties({ DiscoveryClientHealthIndicatorProperties.class,
|
||||
KubernetesDiscoveryClientProperties.class })
|
||||
class KubernetesDiscoveryClientBlockingAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingClass("org.springframework.web.reactive.function.client.WebClient")
|
||||
@ConditionalOnMissingBean(RestTemplate.class)
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplateBuilder().build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingClass("org.springframework.web.reactive.function.client.WebClient")
|
||||
KubernetesDiscoveryClient kubernetesDiscoveryClient(RestTemplate restTemplate,
|
||||
KubernetesDiscoveryClientProperties properties) {
|
||||
return new KubernetesDiscoveryClient(restTemplate, properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass({ HealthIndicator.class })
|
||||
@ConditionalOnDiscoveryHealthIndicatorEnabled
|
||||
InitializingBean indicatorInitializer(ApplicationEventPublisher applicationEventPublisher,
|
||||
ApplicationContext applicationContext) {
|
||||
InitializingBean bean = () -> applicationEventPublisher
|
||||
.publishEvent(new InstanceRegisteredEvent<>(applicationContext.getId(), null));
|
||||
return bean;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2013-2023 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.discovery;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.cloud.CloudPlatform;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.ConditionalOnDiscoveryHealthIndicatorEnabled;
|
||||
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIndicatorProperties;
|
||||
import org.springframework.cloud.client.discovery.health.reactive.ReactiveDiscoveryClientHealthIndicator;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnKubernetesDiscoveryEnabled;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
@ConditionalOnKubernetesDiscoveryEnabled
|
||||
@ConditionalOnReactiveDiscoveryEnabled
|
||||
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
|
||||
@EnableConfigurationProperties({ DiscoveryClientHealthIndicatorProperties.class,
|
||||
KubernetesDiscoveryClientProperties.class })
|
||||
class KubernetesDiscoveryClientReactiveAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(name = { "org.springframework.web.reactive.function.client.WebClient" })
|
||||
@ConditionalOnMissingBean(WebClient.Builder.class)
|
||||
WebClient.Builder webClientBuilder() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(name = { "org.springframework.web.reactive.function.client.WebClient" })
|
||||
KubernetesReactiveDiscoveryClient kubernetesReactiveDiscoveryClient(WebClient.Builder webClientBuilder,
|
||||
KubernetesDiscoveryClientProperties properties) {
|
||||
return new KubernetesReactiveDiscoveryClient(webClientBuilder, properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(name = "org.springframework.boot.actuate.health.ReactiveHealthIndicator")
|
||||
@ConditionalOnDiscoveryHealthIndicatorEnabled
|
||||
ReactiveDiscoveryClientHealthIndicator kubernetesReactiveDiscoveryClientHealthIndicator(
|
||||
KubernetesReactiveDiscoveryClient client, DiscoveryClientHealthIndicatorProperties properties,
|
||||
ApplicationContext applicationContext) {
|
||||
ReactiveDiscoveryClientHealthIndicator healthIndicator = new ReactiveDiscoveryClientHealthIndicator(client,
|
||||
properties);
|
||||
InstanceRegisteredEvent event = new InstanceRegisteredEvent(applicationContext.getId(), null);
|
||||
healthIndicator.onApplicationEvent(event);
|
||||
return healthIndicator;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientAutoConfiguration
|
||||
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientBlockingAutoConfiguration
|
||||
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientReactiveAutoConfiguration
|
||||
|
||||
@@ -83,8 +83,6 @@ class ConfigServerBootstrapperTests {
|
||||
}]
|
||||
""".formatted(wireMockServer.port(), wireMockServer.baseUrl());
|
||||
|
||||
System.out.println(APPS_NAME);
|
||||
|
||||
stubFor(get("/apps/spring-cloud-kubernetes-configserver").willReturn(
|
||||
aResponse().withStatus(200).withBody(APPS_NAME).withHeader("content-type", "application/json")));
|
||||
Environment environment = new Environment("test", "default");
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* Copyright 2013-2023 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.discovery;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
class KubernetesDiscoveryAutoConfigurationTests {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* '@ConditionalOnDiscoveryEnabled' is not matched, thus no beans are created
|
||||
* from either blocking or reactive configurations.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void discoveryDisabled() {
|
||||
setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.enabled=false");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* '@ConditionalOnKubernetesDiscoveryEnabled' is not matched, thus no beans are created
|
||||
* from either blocking or reactive configurations.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void kubernetesDiscoveryDisabled() {
|
||||
setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.kubernetes.discovery.enabled=false");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* '@ConditionalOnCloudPlatform' does not match 'KUBERNETES', thus no beans are created
|
||||
* from either blocking or reactive configurations.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void cloudPlatformDisabled() {
|
||||
setupWithFilteredClassLoader(null, "spring.main.cloud-platform=none");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - reactive config is disabled, blocking is defaulted.
|
||||
* - WebClient class is not present
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void reactiveDisabledBlockingEnabledWebClientPresent() {
|
||||
setupWithFilteredClassLoader(WebClient.class, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.reactive.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(RestTemplate.class);
|
||||
assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class);
|
||||
assertThat(context).getBean("indicatorInitializer").isNotNull();
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - reactive config is disabled, blocking is defaulted.
|
||||
* - WebClient class is present
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void reactiveDisabledBlockingEnabledWebClientMissing() {
|
||||
setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.reactive.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
assertThat(context).getBean("indicatorInitializer").isNotNull();
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - reactive config is disabled, blocking is defaulted.
|
||||
* - WebClient class is present
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void reactiveDisabledBlockingEnabledWebClientMissingHealthIndicatorMissing() {
|
||||
setupWithFilteredClassLoader(HealthIndicator.class, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.reactive.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
assertThat(context).doesNotHaveBean("indicatorInitializer");
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* '@ConditionalOnDiscoveryHealthIndicatorEnabled' not matched.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void blockingHealthIndicatorDisabled() {
|
||||
setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.reactive.enabled=true",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver",
|
||||
"spring.cloud.discovery.client.health-indicator.enabled=false");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
assertThat(context).doesNotHaveBean("indicatorInitializer");
|
||||
|
||||
assertThat(context).hasSingleBean(WebClient.Builder.class);
|
||||
assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - reactive config is enabled, blocking is defaulted.
|
||||
* - WebClient class is present
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void reactiveEnabledBlockingEnabledWebClientPresent() {
|
||||
setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.reactive.enabled=true",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(RestTemplate.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class);
|
||||
assertThat(context).getBean("indicatorInitializer").isNotNull();
|
||||
|
||||
assertThat(context).hasSingleBean(WebClient.Builder.class);
|
||||
assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class);
|
||||
assertThat(context).getBean("kubernetesReactiveDiscoveryClientHealthIndicator").isNotNull();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - reactive config is enabled, blocking is defaulted.
|
||||
* - WebClient class is missing
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void reactiveEnabledBlockingEnabledWebClientMissing() {
|
||||
setupWithFilteredClassLoader(WebClient.class, "spring.main.cloud-platform=KUBERNETES",
|
||||
"spring.cloud.discovery.reactive.enabled=true",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver");
|
||||
applicationContextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(RestTemplate.class);
|
||||
assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class);
|
||||
assertThat(context).getBean("indicatorInitializer").isNotNull();
|
||||
|
||||
assertThat(context).doesNotHaveBean(WebClient.Builder.class);
|
||||
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
|
||||
assertThat(context).getBean("kubernetesReactiveDiscoveryClientHealthIndicator").isNull();
|
||||
});
|
||||
}
|
||||
|
||||
private ApplicationContextRunner applicationContextRunner;
|
||||
|
||||
private void setupWithFilteredClassLoader(Class<?> cls, String... properties) {
|
||||
|
||||
if (cls != null) {
|
||||
applicationContextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(KubernetesDiscoveryClientBlockingAutoConfiguration.class,
|
||||
KubernetesDiscoveryClientReactiveAutoConfiguration.class))
|
||||
.withClassLoader(new FilteredClassLoader(cls)).withPropertyValues(properties);
|
||||
}
|
||||
else {
|
||||
applicationContextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(KubernetesDiscoveryClientBlockingAutoConfiguration.class,
|
||||
KubernetesDiscoveryClientReactiveAutoConfiguration.class))
|
||||
.withPropertyValues(properties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -34,9 +34,10 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
|
||||
ReactiveCommonsClientAutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class));
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
|
||||
AutoConfigurations.of(UtilAutoConfiguration.class, ReactiveCommonsClientAutoConfiguration.class,
|
||||
KubernetesDiscoveryClientReactiveAutoConfiguration.class,
|
||||
KubernetesDiscoveryClientBlockingAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void shouldWorkWithDefaults() {
|
||||
|
||||
Reference in New Issue
Block a user