Merge remote-tracking branch 'origin/1.1.x'

This commit is contained in:
Ryan Baxter
2020-09-23 16:18:51 -04:00
4 changed files with 74 additions and 14 deletions

View File

@@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -36,6 +38,7 @@ import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocato
import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
@@ -71,28 +74,24 @@ public class ConfigReloadAutoConfiguration {
@Autowired
private KubernetesClient kubernetesClient;
@Autowired
private ConfigMapPropertySourceLocator configMapPropertySourceLocator;
@Autowired
private SecretsPropertySourceLocator secretsPropertySourceLocator;
/**
* @param properties config reload properties
* @param strategy configuration update strategy
* @return a bean that listen to configuration changes and fire a reload.
*/
@Bean
@ConditionalOnMissingBean
@Conditional(OnConfigEnabledOrSecretsEnabled.class)
public ConfigurationChangeDetector propertyChangeWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy) {
ConfigurationUpdateStrategy strategy,
@Autowired(required = false) ConfigMapPropertySourceLocator configMapPropertySourceLocator,
@Autowired(required = false) SecretsPropertySourceLocator secretsPropertySourceLocator) {
switch (properties.getMode()) {
case POLLING:
return new PollingConfigurationChangeDetector(this.environment, properties, this.kubernetesClient,
strategy, this.configMapPropertySourceLocator, this.secretsPropertySourceLocator);
strategy, configMapPropertySourceLocator, secretsPropertySourceLocator);
case EVENT:
return new EventBasedConfigurationChangeDetector(this.environment, properties, this.kubernetesClient,
strategy, this.configMapPropertySourceLocator, this.secretsPropertySourceLocator);
strategy, configMapPropertySourceLocator, secretsPropertySourceLocator);
}
throw new IllegalStateException("Unsupported configuration reload mode: " + properties.getMode());
}
@@ -136,6 +135,24 @@ public class ConfigReloadAutoConfiguration {
}
}
private static class OnConfigEnabledOrSecretsEnabled extends AnyNestedCondition {
OnConfigEnabledOrSecretsEnabled() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnBean(ConfigMapPropertySourceLocator.class)
static class configEnabled {
}
@ConditionalOnBean(SecretsPropertySourceLocator.class)
static class secretsEnabled {
}
}
}
}

View File

@@ -65,7 +65,7 @@ public class EventBasedConfigurationChangeDetector extends ConfigurationChangeDe
public void watch() {
boolean activated = false;
if (this.properties.isMonitoringConfigMaps()) {
if (this.properties.isMonitoringConfigMaps() && this.configMapPropertySourceLocator != null) {
try {
String name = "config-maps-watch";
this.watches.put(name, this.kubernetesClient.configMaps().watch(new Watcher<ConfigMap>() {
@@ -91,7 +91,7 @@ public class EventBasedConfigurationChangeDetector extends ConfigurationChangeDe
}
}
if (this.properties.isMonitoringSecrets()) {
if (this.properties.isMonitoringSecrets() && this.secretsPropertySourceLocator != null) {
try {
activated = false;
String name = "secrets-watch";

View File

@@ -67,7 +67,7 @@ public class PollingConfigurationChangeDetector extends ConfigurationChangeDetec
public void executeCycle() {
boolean changedConfigMap = false;
if (this.properties.isMonitoringConfigMaps()) {
if (this.properties.isMonitoringConfigMaps() && this.configMapPropertySourceLocator != null) {
List<? extends MapPropertySource> currentConfigMapSources = findPropertySources(
ConfigMapPropertySource.class);

View File

@@ -22,6 +22,8 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.kubernetes.config.reload.ConfigReloadAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -57,6 +59,20 @@ public class KubernetesConfigConfigurationTest {
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
}
@Test
public void kubernetesWhenKubernetesConfigEnabledButSecretDisabled() throws Exception {
setup("spring.cloud.kubernetes.config.enabled=true", "spring.cloud.kubernetes.secrets.enabled=false");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
}
@Test
public void kubernetesWhenKubernetesConfigDisabledButSecretEnabled() throws Exception {
setup("spring.cloud.kubernetes.config.enabled=false", "spring.cloud.kubernetes.secrets.enabled=true");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isFalse();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
}
@Test
public void kubernetesDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true");
@@ -64,9 +80,36 @@ public class KubernetesConfigConfigurationTest {
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
}
@Test
public void kubernetesReloadEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.reload.enabled=true");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
assertThat(this.context.containsBean("propertyChangeWatcher")).isTrue();
}
@Test
public void kubernetesReloadEnabledButSecretDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.config.enabled=true",
"spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.reload.enabled=true");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
assertThat(this.context.containsBean("propertyChangeWatcher")).isTrue();
}
@Test
public void kubernetesReloadEnabledButSecretAndConfigDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true", "spring.cloud.kubernetes.config.enabled=false",
"spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.reload.enabled=true");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isFalse();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
assertThat(this.context.containsBean("propertyChangeWatcher")).isFalse();
}
private void setup(String... env) {
this.context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class)
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class,
ConfigReloadAutoConfiguration.class, RefreshAutoConfiguration.class)
.web(org.springframework.boot.WebApplicationType.NONE).properties(env).run();
}