Skip to content

Commit 0059aeb

Browse files
author
Ryan Baxter
committed
Merge remote-tracking branch 'origin/1.1.x'
2 parents aa68642 + 1043636 commit 0059aeb

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
2525
import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
27+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2729
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2830
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2931
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -36,6 +38,7 @@
3638
import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator;
3739
import org.springframework.context.ConfigurableApplicationContext;
3840
import org.springframework.context.annotation.Bean;
41+
import org.springframework.context.annotation.Conditional;
3942
import org.springframework.context.annotation.Configuration;
4043
import org.springframework.core.env.AbstractEnvironment;
4144
import org.springframework.scheduling.annotation.EnableAsync;
@@ -71,28 +74,24 @@ protected static class ConfigReloadAutoConfigurationBeans {
7174
@Autowired
7275
private KubernetesClient kubernetesClient;
7376

74-
@Autowired
75-
private ConfigMapPropertySourceLocator configMapPropertySourceLocator;
76-
77-
@Autowired
78-
private SecretsPropertySourceLocator secretsPropertySourceLocator;
79-
8077
/**
8178
* @param properties config reload properties
8279
* @param strategy configuration update strategy
8380
* @return a bean that listen to configuration changes and fire a reload.
8481
*/
8582
@Bean
86-
@ConditionalOnMissingBean
83+
@Conditional(OnConfigEnabledOrSecretsEnabled.class)
8784
public ConfigurationChangeDetector propertyChangeWatcher(ConfigReloadProperties properties,
88-
ConfigurationUpdateStrategy strategy) {
85+
ConfigurationUpdateStrategy strategy,
86+
@Autowired(required = false) ConfigMapPropertySourceLocator configMapPropertySourceLocator,
87+
@Autowired(required = false) SecretsPropertySourceLocator secretsPropertySourceLocator) {
8988
switch (properties.getMode()) {
9089
case POLLING:
9190
return new PollingConfigurationChangeDetector(this.environment, properties, this.kubernetesClient,
92-
strategy, this.configMapPropertySourceLocator, this.secretsPropertySourceLocator);
91+
strategy, configMapPropertySourceLocator, secretsPropertySourceLocator);
9392
case EVENT:
9493
return new EventBasedConfigurationChangeDetector(this.environment, properties, this.kubernetesClient,
95-
strategy, this.configMapPropertySourceLocator, this.secretsPropertySourceLocator);
94+
strategy, configMapPropertySourceLocator, secretsPropertySourceLocator);
9695
}
9796
throw new IllegalStateException("Unsupported configuration reload mode: " + properties.getMode());
9897
}
@@ -136,6 +135,24 @@ private static void wait(ConfigReloadProperties properties) {
136135
}
137136
}
138137

138+
private static class OnConfigEnabledOrSecretsEnabled extends AnyNestedCondition {
139+
140+
OnConfigEnabledOrSecretsEnabled() {
141+
super(ConfigurationPhase.REGISTER_BEAN);
142+
}
143+
144+
@ConditionalOnBean(ConfigMapPropertySourceLocator.class)
145+
static class configEnabled {
146+
147+
}
148+
149+
@ConditionalOnBean(SecretsPropertySourceLocator.class)
150+
static class secretsEnabled {
151+
152+
}
153+
154+
}
155+
139156
}
140157

141158
}

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/EventBasedConfigurationChangeDetector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public EventBasedConfigurationChangeDetector(AbstractEnvironment environment, Co
6565
public void watch() {
6666
boolean activated = false;
6767

68-
if (this.properties.isMonitoringConfigMaps()) {
68+
if (this.properties.isMonitoringConfigMaps() && this.configMapPropertySourceLocator != null) {
6969
try {
7070
String name = "config-maps-watch";
7171
this.watches.put(name, this.kubernetesClient.configMaps().watch(new Watcher<ConfigMap>() {
@@ -91,7 +91,7 @@ public void onClose(KubernetesClientException e) {
9191
}
9292
}
9393

94-
if (this.properties.isMonitoringSecrets()) {
94+
if (this.properties.isMonitoringSecrets() && this.secretsPropertySourceLocator != null) {
9595
try {
9696
activated = false;
9797
String name = "secrets-watch";

spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void init() {
6767
public void executeCycle() {
6868

6969
boolean changedConfigMap = false;
70-
if (this.properties.isMonitoringConfigMaps()) {
70+
if (this.properties.isMonitoringConfigMaps() && this.configMapPropertySourceLocator != null) {
7171
List<? extends MapPropertySource> currentConfigMapSources = findPropertySources(
7272
ConfigMapPropertySource.class);
7373

spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/KubernetesConfigConfigurationTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2424
import org.springframework.boot.builder.SpringApplicationBuilder;
25+
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
26+
import org.springframework.cloud.kubernetes.config.reload.ConfigReloadAutoConfiguration;
2527
import org.springframework.context.ConfigurableApplicationContext;
2628
import org.springframework.context.annotation.Bean;
2729
import org.springframework.context.annotation.Configuration;
@@ -57,16 +59,57 @@ public void kubernetesWhenKubernetesConfigDisabled() throws Exception {
5759
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
5860
}
5961

62+
@Test
63+
public void kubernetesWhenKubernetesConfigEnabledButSecretDisabled() throws Exception {
64+
setup("spring.cloud.kubernetes.config.enabled=true", "spring.cloud.kubernetes.secrets.enabled=false");
65+
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
66+
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
67+
}
68+
69+
@Test
70+
public void kubernetesWhenKubernetesConfigDisabledButSecretEnabled() throws Exception {
71+
setup("spring.cloud.kubernetes.config.enabled=false", "spring.cloud.kubernetes.secrets.enabled=true");
72+
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isFalse();
73+
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
74+
}
75+
6076
@Test
6177
public void kubernetesDefaultEnabled() throws Exception {
6278
setup("spring.cloud.kubernetes.enabled=true");
6379
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
6480
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
6581
}
6682

83+
@Test
84+
public void kubernetesReloadEnabled() throws Exception {
85+
setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.reload.enabled=true");
86+
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
87+
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
88+
assertThat(this.context.containsBean("propertyChangeWatcher")).isTrue();
89+
}
90+
91+
@Test
92+
public void kubernetesReloadEnabledButSecretDisabled() throws Exception {
93+
setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.config.enabled=true",
94+
"spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.reload.enabled=true");
95+
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
96+
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
97+
assertThat(this.context.containsBean("propertyChangeWatcher")).isTrue();
98+
}
99+
100+
@Test
101+
public void kubernetesReloadEnabledButSecretAndConfigDisabled() throws Exception {
102+
setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.config.enabled=false",
103+
"spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.reload.enabled=true");
104+
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isFalse();
105+
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
106+
assertThat(this.context.containsBean("propertyChangeWatcher")).isFalse();
107+
}
108+
67109
private void setup(String... env) {
68110
this.context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
69-
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class)
111+
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class,
112+
ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class)
70113
.web(org.springframework.boot.WebApplicationType.NONE).properties(env).run();
71114
}
72115

0 commit comments

Comments
 (0)