disable and enable k8s configuration feature with a relevant property fixes gh-585

This commit is contained in:
Haytham Mohamed
2020-07-28 13:56:01 -05:00
parent 06f4b217b1
commit 6f9190164d
5 changed files with 206 additions and 48 deletions

1
.gitignore vendored
View File

@@ -63,6 +63,7 @@ hs_err_pid*
# IntelliJ
/out/
.DS_Store
# mpeltonen/sbt-idea plugin
.idea_modules/

View File

@@ -48,7 +48,8 @@ import org.springframework.util.Assert;
* @author Nicolla Ferraro
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.kubernetes.enabled", matchIfMissing = true)
@ConditionalOnProperty(name = "spring.cloud.kubernetes.config.enabled",
havingValue = "true")
@ConditionalOnClass(EndpointAutoConfiguration.class)
@AutoConfigureAfter({ InfoEndpointAutoConfiguration.class,
RefreshEndpointAutoConfiguration.class, RefreshAutoConfiguration.class })
@@ -59,7 +60,8 @@ public class ConfigReloadAutoConfiguration {
/**
* Configuration reload must be enabled explicitly.
*/
@ConditionalOnProperty("spring.cloud.kubernetes.reload.enabled")
@ConditionalOnProperty(name = "spring.cloud.kubernetes.reload.enabled",
havingValue = "true")
@ConditionalOnClass({ RestartEndpoint.class, ContextRefresher.class })
@EnableScheduling
@EnableAsync

View File

@@ -16,71 +16,60 @@
package org.springframework.cloud.kubernetes.config;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* @author Ryan Dawson
*/
public class KubernetesConfigConfigurationTest {
public class KubernetesConfigConfigurationTest extends KubernetesConfigTestBase {
private ConfigurableApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
@Test
public void kubernetesDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true");
assertThat(this.getContext().containsBean("configMapPropertySourceLocator"))
.isTrue();
assertThat(this.getContext().containsBean("secretsPropertySourceLocator"))
.isTrue();
}
@Test
public void kubernetesWhenKubernetesDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=false");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isFalse();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
assertThat(this.getContext().containsBean("configMapPropertySourceLocator"))
.isFalse();
assertThat(this.getContext().containsBean("secretsPropertySourceLocator"))
.isFalse();
}
@Test
public void kubernetesConfigDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.config.enabled=true",
"spring.cloud.kubernetes.secrets.enabled=true");
assertThat(this.getContext().containsBean("configMapPropertySourceLocator"))
.isTrue();
assertThat(this.getContext().containsBean("secretsPropertySourceLocator"))
.isTrue();
}
@Test
public void kubernetesConfigwhenKubenretesEnabledAndKubernetsConfigDisabled()
throws Exception {
setup("spring.cloud.kubernetes.enabled=true",
"spring.cloud.kubernetes.config.enabled=false");
assertThat(this.getContext().containsBean("configMapPropertySourceLocator"))
.isFalse();
}
@Test
public void kubernetesWhenKubernetesConfigDisabled() throws Exception {
setup("spring.cloud.kubernetes.config.enabled=false",
"spring.cloud.kubernetes.secrets.enabled=false");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isFalse();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isFalse();
}
@Test
public void kubernetesDefaultEnabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=true");
assertThat(this.context.containsBean("configMapPropertySourceLocator")).isTrue();
assertThat(this.context.containsBean("secretsPropertySourceLocator")).isTrue();
}
private void setup(String... env) {
this.context = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class)
.web(org.springframework.boot.WebApplicationType.NONE)
.properties(env).run();
}
@Configuration(proxyBeanMethods = false)
static class KubernetesClientTestConfiguration {
@Bean
KubernetesClient kubernetesClient() {
return mock(KubernetesClient.class);
}
assertThat(this.getContext().containsBean("configMapPropertySourceLocator"))
.isFalse();
assertThat(this.getContext().containsBean("secretsPropertySourceLocator"))
.isFalse();
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2012-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.config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import org.junit.After;
import org.junit.ClassRule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Haytham Mohamed
**/
public class KubernetesConfigTestBase {
private static ConfigurableApplicationContext context;
@ClassRule
public static KubernetesServer server = new KubernetesServer();
protected static ConfigurableApplicationContext getContext() {
return context;
}
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
protected static void setup(String... env) {
context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class, BootstrapConfiguration.class)
.web(org.springframework.boot.WebApplicationType.NONE)
.properties(env).run();
}
@Configuration(proxyBeanMethods = false)
private static class KubernetesClientTestConfiguration {
@ConditionalOnMissingBean(KubernetesClient.class)
@Bean
KubernetesClient kubernetesClient() {
return server.getClient();
}
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2012-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.config.reload;
import java.util.HashMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cloud.kubernetes.config.KubernetesConfigTestBase;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Haytham Mohamed
*
* To test if either kubernetes, kubernetes.configmap, or reload is disabled, then the
* detector and update strategy beans won't be available.
**/
public class ConfigReloadAutoConfigurationTest extends KubernetesConfigTestBase {
private static final String APPLICATION_NAME = "application";
@Test
public void kubernetesConfigReloadDisabled() throws Exception {
setup("spring.cloud.kubernetes.reload.enabled=false");
assertThat(this.getContext().containsBean("configurationChangeDetector"))
.isFalse();
assertThat(this.getContext().containsBean("configurationUpdateStrategy"))
.isFalse();
}
@Test
public void kubernetesConfigReloadWhenKubernetesConfigDisabled() throws Exception {
setup("spring.cloud.kubernetes.config.enabled=false");
assertThat(this.getContext().containsBean("configurationChangeDetector"))
.isFalse();
assertThat(this.getContext().containsBean("configurationUpdateStrategy"))
.isFalse();
}
@Test
public void kubernetesConfigReloadWhenKubernetesDisabled() throws Exception {
setup("spring.cloud.kubernetes.enabled=false");
assertThat(this.getContext().containsBean("configurationChangeDetector"))
.isFalse();
assertThat(this.getContext().containsBean("configurationUpdateStrategy"))
.isFalse();
}
@BeforeClass
public static void setUpBeforeClass() {
setup();
KubernetesClient mockClient = getContext().getBean(KubernetesClient.class);
// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY,
mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY,
"false");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
HashMap<String, String> data = new HashMap<>();
data.put("bean.greeting", "Hello ConfigMap, %s!");
server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME)
.andReturn(200, new ConfigMapBuilder().withNewMetadata()
.withName(APPLICATION_NAME).endMetadata().addToData(data).build())
.always();
server.expect()
.withPath("/api/v1/namespaces/spring/configmaps/" + APPLICATION_NAME)
.andReturn(200, new ConfigMapBuilder().withNewMetadata()
.withName(APPLICATION_NAME).endMetadata().addToData(data).build())
.always();
}
}