Skip to content

Commit 9973e9b

Browse files
TYsewynspencergibb
authored andcommitted
Extracted catalog watch auto-configuration. (spring-cloud#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`.
1 parent 3ad8641 commit 9973e9b

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2013-2019 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.discovery;
18+
19+
import io.fabric8.kubernetes.client.KubernetesClient;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24+
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
25+
import org.springframework.cloud.kubernetes.KubernetesAutoConfiguration;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
/**
30+
* Auto configuration for catalog watcher.
31+
*
32+
* @author Tim Ysewyn
33+
*/
34+
@Configuration
35+
@ConditionalOnDiscoveryEnabled
36+
@ConditionalOnProperty(name = "spring.cloud.kubernetes.enabled", matchIfMissing = true)
37+
@AutoConfigureAfter({ KubernetesAutoConfiguration.class })
38+
public class KubernetesCatalogWatchAutoConfiguration {
39+
40+
@Bean
41+
@ConditionalOnMissingBean
42+
@ConditionalOnProperty(name = "spring.cloud.kubernetes.discovery.catalog-services-watch.enabled", matchIfMissing = true)
43+
public KubernetesCatalogWatch kubernetesCatalogWatch(KubernetesClient client) {
44+
return new KubernetesCatalogWatch(client);
45+
}
46+
47+
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2424
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
25+
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
2526
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
2627
import org.springframework.cloud.kubernetes.registry.KubernetesRegistration;
2728
import org.springframework.cloud.kubernetes.registry.KubernetesServiceRegistry;
@@ -32,8 +33,10 @@
3233
* Auto configuration for discovery clients.
3334
*
3435
* @author Mauricio Salatino
36+
* @author Tim Ysewyn
3537
*/
3638
@Configuration
39+
@ConditionalOnDiscoveryEnabled
3740
@ConditionalOnProperty(name = "spring.cloud.kubernetes.enabled", matchIfMissing = true)
3841
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
3942
CommonsClientAutoConfiguration.class })
@@ -83,11 +86,4 @@ public KubernetesDiscoveryProperties getKubernetesDiscoveryProperties() {
8386
return new KubernetesDiscoveryProperties();
8487
}
8588

86-
@Bean
87-
@ConditionalOnMissingBean
88-
@ConditionalOnProperty(name = "spring.cloud.kubernetes.discovery.catalog-services-watch.enabled", matchIfMissing = true)
89-
public KubernetesCatalogWatch kubernetesCatalogWatch(KubernetesClient client) {
90-
return new KubernetesCatalogWatch(client);
91-
}
92-
9389
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
org.springframework.cloud.kubernetes.discovery.KubernetesCatalogWatchAutoConfiguration, \
23
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientAutoConfiguration
34
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
45
org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClientConfigClientBootstrapConfiguration

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
/**
3434
* @author Oleg Vyukov
35+
* @author Tim Ysewyn
3536
*/
3637
public class KubernetesCatalogServicesWatchConfigurationTest {
3738

@@ -56,6 +57,12 @@ public void kubernetesCatalogWatchWhenKubernetesDisabled() throws Exception {
5657
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
5758
}
5859

60+
@Test
61+
public void kubernetesCatalogWatchWhenServiceDiscoveryDisabled() throws Exception {
62+
setup("spring.cloud.discovery.enabled=false");
63+
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
64+
}
65+
5966
@Test
6067
public void kubernetesCatalogWatchDefaultEnabled() throws Exception {
6168
setup();
@@ -66,6 +73,7 @@ private void setup(String... env) {
6673
this.context = new SpringApplicationBuilder(
6774
PropertyPlaceholderAutoConfiguration.class,
6875
KubernetesClientTestConfiguration.class,
76+
KubernetesCatalogWatchAutoConfiguration.class,
6977
KubernetesDiscoveryClientAutoConfiguration.class)
7078
.web(WebApplicationType.NONE).properties(env).run();
7179
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
/**
3333
* @author Ryan Dawson
34+
* @author Tim Ysewyn
3435
*/
3536
public class KubernetesDiscoveryClientAutoConfigurationPropertiesTests {
3637

@@ -58,6 +59,13 @@ public void kubernetesDiscoveryWhenKubernetesDisabled() throws Exception {
5859
.isEmpty();
5960
}
6061

62+
@Test
63+
public void kubernetesDiscoveryWhenDiscoveryDisabled() throws Exception {
64+
setup("spring.cloud.discovery.enabled=false");
65+
assertThat(this.context.getBeanNamesForType(KubernetesDiscoveryClient.class))
66+
.isEmpty();
67+
}
68+
6169
@Test
6270
public void kubernetesDiscoveryDefaultEnabled() throws Exception {
6371
setup("spring.cloud.kubernetes.enabled=true");

0 commit comments

Comments
 (0)