Extracted catalog watch auto-configuration. (#434)

Needed in order to have that bean running in the correct application context when SC Config discovery is enabled.
Also fixes a bug where Kubernetes discovery was enabled when discovery was disabled with `spring.cloud.kubernetes.enabled=false`.
This commit is contained in:
Tim Ysewyn
2019-07-24 17:47:31 +02:00
committed by Spencer Gibb
parent 3ad864127b
commit 9973e9b25f
5 changed files with 67 additions and 7 deletions

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2013-2019 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 io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.kubernetes.KubernetesAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Auto configuration for catalog watcher.
*
* @author Tim Ysewyn
*/
@Configuration
@ConditionalOnDiscoveryEnabled
@ConditionalOnProperty(name = "spring.cloud.kubernetes.enabled", matchIfMissing = true)
@AutoConfigureAfter({ KubernetesAutoConfiguration.class })
public class KubernetesCatalogWatchAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "spring.cloud.kubernetes.discovery.catalog-services-watch.enabled", matchIfMissing = true)
public KubernetesCatalogWatch kubernetesCatalogWatch(KubernetesClient client) {
return new KubernetesCatalogWatch(client);
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.cloud.kubernetes.registry.KubernetesRegistration;
import org.springframework.cloud.kubernetes.registry.KubernetesServiceRegistry;
@@ -32,8 +33,10 @@ import org.springframework.context.annotation.Configuration;
* Auto configuration for discovery clients.
*
* @author Mauricio Salatino
* @author Tim Ysewyn
*/
@Configuration
@ConditionalOnDiscoveryEnabled
@ConditionalOnProperty(name = "spring.cloud.kubernetes.enabled", matchIfMissing = true)
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class })
@@ -83,11 +86,4 @@ public class KubernetesDiscoveryClientAutoConfiguration {
return new KubernetesDiscoveryProperties();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "spring.cloud.kubernetes.discovery.catalog-services-watch.enabled", matchIfMissing = true)
public KubernetesCatalogWatch kubernetesCatalogWatch(KubernetesClient client) {
return new KubernetesCatalogWatch(client);
}
}

View File

@@ -1,4 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.kubernetes.discovery.KubernetesCatalogWatchAutoConfiguration, \
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientConfigClientBootstrapConfiguration

View File

@@ -32,6 +32,7 @@ import static org.mockito.Mockito.mock;
/**
* @author Oleg Vyukov
* @author Tim Ysewyn
*/
public class KubernetesCatalogServicesWatchConfigurationTest {
@@ -56,6 +57,12 @@ public class KubernetesCatalogServicesWatchConfigurationTest {
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
}
@Test
public void kubernetesCatalogWatchWhenServiceDiscoveryDisabled() throws Exception {
setup("spring.cloud.discovery.enabled=false");
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
}
@Test
public void kubernetesCatalogWatchDefaultEnabled() throws Exception {
setup();
@@ -66,6 +73,7 @@ public class KubernetesCatalogServicesWatchConfigurationTest {
this.context = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class,
KubernetesCatalogWatchAutoConfiguration.class,
KubernetesDiscoveryClientAutoConfiguration.class)
.web(WebApplicationType.NONE).properties(env).run();
}

View File

@@ -31,6 +31,7 @@ import static org.mockito.Mockito.mock;
/**
* @author Ryan Dawson
* @author Tim Ysewyn
*/
public class KubernetesDiscoveryClientAutoConfigurationPropertiesTests {
@@ -58,6 +59,13 @@ public class KubernetesDiscoveryClientAutoConfigurationPropertiesTests {
.isEmpty();
}
@Test
public void kubernetesDiscoveryWhenDiscoveryDisabled() throws Exception {
setup("spring.cloud.discovery.enabled=false");
assertThat(this.context.getBeanNamesForType(KubernetesDiscoveryClient.class))
.isEmpty();
}
@Test
public void kubernetesDiscoveryDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true");