diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/ConditionalOnKubernetesConfigEnabled.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/ConditionalOnKubernetesConfigEnabled.java new file mode 100644 index 00000000..e5366c9e --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/ConditionalOnKubernetesConfigEnabled.java @@ -0,0 +1,40 @@ +/* + * Copyright 2019-2021 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.commons; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * Provides a more succinct conditional spring.cloud.kubernetes.config.enabled. + * + * @author wind57 + */ + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty(value = "spring.cloud.kubernetes.config.enabled", matchIfMissing = true) +public @interface ConditionalOnKubernetesConfigEnabled { +} diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/ConditionalOnKubernetesSecretsEnabled.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/ConditionalOnKubernetesSecretsEnabled.java new file mode 100644 index 00000000..401985ac --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/ConditionalOnKubernetesSecretsEnabled.java @@ -0,0 +1,40 @@ +/* + * Copyright 2019-2021 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.commons; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * Provides a more succinct conditional spring.cloud.kubernetes.secrets.enabled. + * + * @author wind57 + */ + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty(value = "spring.cloud.kubernetes.secrets.enabled", matchIfMissing = true) +public @interface ConditionalOnKubernetesSecretsEnabled { +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java index 6848089d..44eb0ccc 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfiguration.java @@ -20,10 +20,11 @@ import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.client.KubernetesClient; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesConfigEnabled; +import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesEnabled; +import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesSecretsEnabled; import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.config.ConfigMapConfigProperties; import org.springframework.cloud.kubernetes.commons.config.KubernetesBootstrapConfiguration; @@ -39,31 +40,24 @@ import org.springframework.context.annotation.Import; * @author Ioannis Canellos */ @Configuration(proxyBeanMethods = false) -@ConditionalOnProperty(value = "spring.cloud.kubernetes.enabled", matchIfMissing = true) +@ConditionalOnKubernetesEnabled +@Import({ KubernetesCommonsAutoConfiguration.class, Fabric8AutoConfiguration.class }) @ConditionalOnClass({ ConfigMap.class, Secret.class }) @AutoConfigureAfter(KubernetesBootstrapConfiguration.class) public class Fabric8BootstrapConfiguration { - @Configuration(proxyBeanMethods = false) - @Import({ KubernetesCommonsAutoConfiguration.class, Fabric8AutoConfiguration.class }) - protected static class KubernetesPropertySourceConfiguration { - - @Autowired - private KubernetesClient client; - - @Bean - @ConditionalOnProperty(name = "spring.cloud.kubernetes.config.enabled", matchIfMissing = true) - public Fabric8ConfigMapPropertySourceLocator configMapPropertySourceLocator( - ConfigMapConfigProperties properties) { - return new Fabric8ConfigMapPropertySourceLocator(this.client, properties); - } - - @Bean - @ConditionalOnProperty(name = "spring.cloud.kubernetes.secrets.enabled", matchIfMissing = true) - public Fabric8SecretsPropertySourceLocator secretsPropertySourceLocator(SecretsConfigProperties properties) { - return new Fabric8SecretsPropertySourceLocator(this.client, properties); - } + @Bean + @ConditionalOnKubernetesConfigEnabled + public Fabric8ConfigMapPropertySourceLocator configMapPropertySourceLocator(ConfigMapConfigProperties properties, + KubernetesClient client) { + return new Fabric8ConfigMapPropertySourceLocator(client, properties); + } + @Bean + @ConditionalOnKubernetesSecretsEnabled + public Fabric8SecretsPropertySourceLocator secretsPropertySourceLocator(SecretsConfigProperties properties, + KubernetesClient client) { + return new Fabric8SecretsPropertySourceLocator(client, properties); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Application.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Application.java new file mode 100644 index 00000000..48180935 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Application.java @@ -0,0 +1,27 @@ +/* + * Copyright 2013-2021 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.fabric8.config; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author wind57 + */ +@SpringBootApplication +public class Application { + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfigurationTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfigurationTests.java new file mode 100644 index 00000000..71475cd1 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8BootstrapConfigurationTests.java @@ -0,0 +1,128 @@ +/* + * Copyright 2013-2021 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.fabric8.config; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ConfigurableApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author wind57 + */ +public class Fabric8BootstrapConfigurationTests { + + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = "spring.cloud.kubernetes.enabled=false") + @Nested + class KubernetesDisabled { + + @Autowired + ConfigurableApplicationContext context; + + @Test + public void configAndSecretsBeansAreNotPresent() { + assertThat(context.getBeanNamesForType(Fabric8ConfigMapPropertySourceLocator.class)).hasSize(0); + assertThat(context.getBeanNamesForType(Fabric8SecretsPropertySourceLocator.class)).hasSize(0); + } + + } + + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = "spring.cloud.kubernetes.enabled=true") + @Nested + class KubernetesEnabledOnPurpose { + + @Autowired + ConfigurableApplicationContext context; + + @Test + public void configAndSecretsBeansArePresent() { + assertThat(context.getBeanNamesForType(Fabric8ConfigMapPropertySourceLocator.class)).hasSize(1); + assertThat(context.getBeanNamesForType(Fabric8SecretsPropertySourceLocator.class)).hasSize(1); + } + + } + + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class) + @Nested + class KubernetesEnabled { + + @Autowired + ConfigurableApplicationContext context; + + @Test + public void configAndSecretsBeansArePresent() { + assertThat(context.getBeanNamesForType(Fabric8ConfigMapPropertySourceLocator.class)).hasSize(1); + assertThat(context.getBeanNamesForType(Fabric8SecretsPropertySourceLocator.class)).hasSize(1); + } + + } + + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = "spring.cloud.kubernetes.config.enabled=false") + @Nested + class KubernetesEnabledConfigDisabled { + + @Autowired + ConfigurableApplicationContext context; + + @Test + public void secretsOnlyPresent() { + assertThat(context.getBeanNamesForType(Fabric8ConfigMapPropertySourceLocator.class)).hasSize(0); + assertThat(context.getBeanNamesForType(Fabric8SecretsPropertySourceLocator.class)).hasSize(1); + } + + } + + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, + properties = "spring.cloud.kubernetes.secrets.enabled=false") + @Nested + class KubernetesEnabledSecretsDisabled { + + @Autowired + ConfigurableApplicationContext context; + + @Test + public void secretsOnlyPresent() { + assertThat(context.getBeanNamesForType(Fabric8ConfigMapPropertySourceLocator.class)).hasSize(1); + assertThat(context.getBeanNamesForType(Fabric8SecretsPropertySourceLocator.class)).hasSize(0); + } + + } + + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class, properties = { + "spring.cloud.kubernetes.secrets.enabled=false", "spring.cloud.kubernetes.config.enabled=false" }) + @Nested + class KubernetesEnabledSecretsAndConfigDisabled { + + @Autowired + ConfigurableApplicationContext context; + + @Test + public void secretsOnlyPresent() { + assertThat(context.getBeanNamesForType(Fabric8ConfigMapPropertySourceLocator.class)).hasSize(0); + assertThat(context.getBeanNamesForType(Fabric8SecretsPropertySourceLocator.class)).hasSize(0); + } + + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java index 86bdf2ce..01ea8b6f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java @@ -16,29 +16,29 @@ package org.springframework.cloud.kubernetes.fabric8.config; -import io.fabric8.kubernetes.client.server.mock.KubernetesServer; -import org.junit.ClassRule; -import org.junit.Test; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ +@EnableKubernetesMockClient(crud = true, https = false) public class Fabric8ConfigUtilsTests { - @ClassRule - public static final KubernetesServer server = new KubernetesServer(false); + private KubernetesClient client; @Test public void testGetApplicationNamespaceNotPresent() { - String result = Fabric8ConfigUtils.getApplicationNamespace(server.getClient(), "", "target"); + String result = Fabric8ConfigUtils.getApplicationNamespace(client, "", "target"); assertThat(result).isEqualTo("test"); } @Test public void testGetApplicationNamespacePresent() { - String result = Fabric8ConfigUtils.getApplicationNamespace(server.getClient(), "namespace", "target"); + String result = Fabric8ConfigUtils.getApplicationNamespace(client, "namespace", "target"); assertThat(result).isEqualTo("namespace"); }