diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerAutoConfiguration.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerAutoConfiguration.java index 1028151d1d..31fe95a590 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerAutoConfiguration.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerAutoConfiguration.java @@ -18,12 +18,17 @@ import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; import io.kubernetes.client.openapi.apis.CoreV1Api; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -57,13 +62,26 @@ @EnableConfigurationProperties(KubernetesConfigServerProperties.class) public class KubernetesConfigServerAutoConfiguration { + private static final Logger LOG = Logger.getLogger(KubernetesConfigServerAutoConfiguration.class.getName()); + + @Bean + @ConditionalOnBean(KubernetesEnvironmentRepository.class) + @ConditionalOnMissingBean + public KubernetesEnvironmentRepositoryFactory kubernetesEnvironmentRepositoryFactory( + ObjectProvider kubernetesEnvironmentRepositoryProvider) { + LOG.info("Creating KubernetesEnvironmentRepositoryFactory bean..."); + return new KubernetesEnvironmentRepositoryFactory(kubernetesEnvironmentRepositoryProvider); + } + @Bean @Profile("kubernetes") + @ConditionalOnMissingBean public EnvironmentRepository kubernetesEnvironmentRepository(CoreV1Api coreV1Api, - List kubernetesPropertySourceSuppliers, - KubernetesNamespaceProvider kubernetesNamespaceProvider) { + List kubernetesPropertySourceSuppliers, + KubernetesNamespaceProvider kubernetesNamespaceProvider) { + LOG.info("Creating KubernetesEnvironmentRepository bean..."); return new KubernetesEnvironmentRepository(coreV1Api, kubernetesPropertySourceSuppliers, - kubernetesNamespaceProvider.getNamespace()); + kubernetesNamespaceProvider.getNamespace()); } @Bean @@ -92,20 +110,28 @@ public KubernetesPropertySourceSupplier configMapPropertySourceSupplier( @ConditionalOnKubernetesSecretsEnabled @ConditionalOnProperty("spring.cloud.kubernetes.secrets.enableApi") public KubernetesPropertySourceSupplier secretsPropertySourceSupplier(KubernetesConfigServerProperties properties) { + LOG.info("Creating secretsPropertySourceSupplier bean..."); return (coreApi, applicationName, namespace, springEnv) -> { List namespaces = namespaceSplitter(properties.getSecretsNamespaces(), namespace); List propertySources = new ArrayList<>(); + LOG.info("Processing namespaces for secrets: " + namespaces); + namespaces.forEach(space -> { + LOG.info("Fetching secrets for namespace: " + space); NormalizedSource source = new NamedSecretNormalizedSource(applicationName, space, false, - ConfigUtils.Prefix.DEFAULT, true, true); + ConfigUtils.Prefix.DEFAULT, true, true); KubernetesClientConfigContext context = new KubernetesClientConfigContext(coreApi, source, space, - springEnv, false); - propertySources.add(new KubernetesClientSecretsPropertySource(context)); + springEnv, false); + MapPropertySource propertySource = new KubernetesClientSecretsPropertySource(context); + propertySources.add(propertySource); + LOG.info("Added property source for namespace: " + space); }); + LOG.info("Total property sources created: " + propertySources.size()); return propertySources; }; } + } diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerProperties.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerProperties.java index 1e753994ba..ab8eb42bb9 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerProperties.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesConfigServerProperties.java @@ -17,13 +17,16 @@ package org.springframework.cloud.kubernetes.configserver; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties; /** * @author Ryan Baxter */ @ConfigurationProperties("spring.cloud.kubernetes.configserver") -public class KubernetesConfigServerProperties { +public class KubernetesConfigServerProperties implements EnvironmentRepositoryProperties { + + private int order = DEFAULT_ORDER; private String configMapNamespaces = ""; @@ -45,4 +48,12 @@ public void setSecretsNamespaces(String secretsNamespaces) { this.secretsNamespaces = secretsNamespaces; } + public int getOrder() { + return this.order; + } + + public void setOrder(int order) { + this.order = order; + } + } diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java index aa377751d4..8dd73c8c36 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java @@ -16,15 +16,13 @@ package org.springframework.cloud.kubernetes.configserver; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import io.kubernetes.client.openapi.apis.CoreV1Api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.PropertySource; import org.springframework.cloud.config.server.environment.EnvironmentRepository; @@ -46,11 +44,14 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository { private final String namespace; + public KubernetesEnvironmentRepository(CoreV1Api coreApi, - List kubernetesPropertySourceSuppliers, String namespace) { + List kubernetesPropertySourceSuppliers, + String namespace) { this.coreApi = coreApi; this.kubernetesPropertySourceSuppliers = kubernetesPropertySourceSuppliers; this.namespace = namespace; + LOG.info("Initialized KubernetesEnvironmentRepository with namespace: " + namespace); } @Override @@ -60,62 +61,77 @@ public Environment findOne(String application, String profile, String label) { @Override public Environment findOne(String application, String profile, String label, boolean includeOrigin) { + LOG.info("Finding environment for application: " + application + ", profile: " + profile + ", label: " + label); + if (!StringUtils.hasText(profile)) { profile = "default"; + LOG.debug("No profile provided, using default profile"); } - List profiles = new ArrayList<>(List.of(StringUtils.commaDelimitedListToStringArray(profile))); + List profiles = new ArrayList<>(List.of(StringUtils.commaDelimitedListToStringArray(profile))); Collections.reverse(profiles); + if (!profiles.contains("default")) { profiles.add("default"); + LOG.debug("Added 'default' profile to the list of active profiles"); } - Environment environment = new Environment(application, profiles.toArray(profiles.toArray(new String[0])), label, - null, null); - LOG.info("Profiles: " + profile); - LOG.info("Application: " + application); - LOG.info("Label: " + label); + + Environment environment = new Environment(application, profiles.toArray(new String[0]), label, null, null); + LOG.info("Created Environment with profiles: " + StringUtils.collectionToCommaDelimitedString(profiles)); + for (String activeProfile : profiles) { try { - // This is needed so that when we get the application name in - // SourceDataProcessor.sorted that it actually - // exists in the Environment - StandardEnvironment springEnv = new KubernetesConfigServerEnvironment( - createPropertySources(application)); + LOG.debug("Processing profile: " + activeProfile); + StandardEnvironment springEnv = new KubernetesConfigServerEnvironment(createPropertySources(application)); springEnv.setActiveProfiles(activeProfile); + LOG.debug("Set active profile in Spring Environment: " + activeProfile); + if (!"application".equalsIgnoreCase(application)) { + LOG.debug("Adding application configuration for: " + application); addApplicationConfiguration(environment, springEnv, application); } - } - catch (Exception e) { - LOG.warn(e); + } catch (Exception e) { + LOG.warn("Error processing profile: " + activeProfile, e); } } + + LOG.debug("Adding default application configuration for 'application'"); StandardEnvironment springEnv = new KubernetesConfigServerEnvironment(createPropertySources("application")); addApplicationConfiguration(environment, springEnv, "application"); + + LOG.info("Final Environment: " + environment); return environment; } private MutablePropertySources createPropertySources(String application) { + LOG.debug("Creating property sources for application: " + application); Map applicationProperties = Map.of("spring.application.name", application); MapPropertySource propertySource = new MapPropertySource("kubernetes-config-server", applicationProperties); MutablePropertySources mutablePropertySources = new MutablePropertySources(); mutablePropertySources.addFirst(propertySource); + LOG.debug("Added property source: " + propertySource.getName()); return mutablePropertySources; } private void addApplicationConfiguration(Environment environment, StandardEnvironment springEnv, - String applicationName) { + String applicationName) { + LOG.debug("Adding application configuration for: " + applicationName); + kubernetesPropertySourceSuppliers.forEach(supplier -> { + LOG.debug("Processing property source supplier: " + supplier.getClass().getSimpleName()); List propertySources = supplier.get(coreApi, applicationName, namespace, springEnv); + LOG.debug("Found " + propertySources.size() + " property sources for application: " + applicationName); + propertySources.forEach(propertySource -> { if (propertySource.getPropertyNames().length > 0) { - LOG.debug("Adding PropertySource " + propertySource.getName()); + LOG.debug("Adding PropertySource: " + propertySource.getName()); LOG.debug("PropertySource Names: " - + StringUtils.arrayToCommaDelimitedString(propertySource.getPropertyNames())); + + StringUtils.arrayToCommaDelimitedString(propertySource.getPropertyNames())); environment.add(new PropertySource(propertySource.getName(), propertySource.getSource())); + } else { + LOG.debug("Skipping empty PropertySource: " + propertySource.getName()); } }); }); } - } diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryFactory.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryFactory.java new file mode 100644 index 0000000000..b7e3501b4b --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryFactory.java @@ -0,0 +1,40 @@ +/* + * 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.configserver; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.cloud.config.server.environment.EnvironmentRepositoryFactory; + +/** + * Factory class for creating instances of {@link KubernetesEnvironmentRepository}. + */ +public class KubernetesEnvironmentRepositoryFactory + implements EnvironmentRepositoryFactory { + + private final ObjectProvider kubernetesEnvironmentRepositoryProvider; + + public KubernetesEnvironmentRepositoryFactory( + ObjectProvider kubernetesEnvironmentRepositoryProvider) { + this.kubernetesEnvironmentRepositoryProvider = kubernetesEnvironmentRepositoryProvider; + } + + @Override + public KubernetesEnvironmentRepository build(KubernetesConfigServerProperties environmentProperties) { + return kubernetesEnvironmentRepositoryProvider.getIfAvailable(); + } + +} diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithGitAndKubernetesConfigSourcesTests.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithGitAndKubernetesConfigSourcesTests.java new file mode 100644 index 0000000000..e6ecd07fa4 --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithGitAndKubernetesConfigSourcesTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013-2022 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.configserver; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository; +import org.springframework.context.ConfigurableApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = { KubernetesConfigServerApplication.class }, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes", + "spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite", + "spring.cloud.config.server.composite[0].type=git", + "spring.cloud.config.server.composite[0].uri=https://github.com/spring-cloud-samples/config-repo", + "spring.cloud.config.server.composite[1].type=kubernetes", + "spring.cloud.config.server.composite[1].config-map-namespace=default", + "spring.cloud.config.server.composite[1].secrets-namespace=default" }) +class CompositeProfileWithGitAndKubernetesConfigSourcesTests { + + @Autowired + private ConfigurableApplicationContext context; + + @Test + void kubernetesEnvironmentRepositoryIsLoaded() { + assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(2); + } + + @Test + void kubernetesPropertySourceSuppliersAreLoaded() { + assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty(); + } + + @Test + void gitEnvironmentRepositoryIsLoaded() { + assertThat(context.getBeanNamesForType(JGitEnvironmentRepository.class)).hasSize(1); + } + +} diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithMultipleKubernetesConfigSourcesTests.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithMultipleKubernetesConfigSourcesTests.java new file mode 100644 index 0000000000..e9744b3de1 --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithMultipleKubernetesConfigSourcesTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013-2022 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.configserver; + +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; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = { KubernetesConfigServerApplication.class }, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes", + "spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite", + "spring.cloud.config.server.composite[0].type=kubernetes", + "spring.cloud.config.server.composite[0].config-map-namespace=default", + "spring.cloud.config.server.composite[0].secrets-namespace=default", + "spring.cloud.config.server.composite[1].type=kubernetes", + "spring.cloud.config.server.composite[1].config-map-namespace=another-namespace", + "spring.cloud.config.server.composite[1].secrets-namespace=another-namespace" }) +class CompositeProfileWithMultipleKubernetesConfigSourcesTests { + + @Autowired + private ConfigurableApplicationContext context; + + @Test + void kubernetesEnvironmentRepositoriesAreLoaded() { + assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(3); + } + + @Test + void kubernetesPropertySourceSuppliersAreLoaded() { + assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty(); + } + +} diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithOnlyKubernetesConfigSourceTests.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithOnlyKubernetesConfigSourceTests.java new file mode 100644 index 0000000000..78a304a99b --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/CompositeProfileWithOnlyKubernetesConfigSourceTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2013-2022 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.configserver; + +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; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = { KubernetesConfigServerApplication.class }, + properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes", + "spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite", + "spring.cloud.config.server.composite[0].type=kubernetes", + "spring.cloud.config.server.composite[0].config-map-namespace=default", + "spring.cloud.config.server.composite[0].secrets-namespace=default" }) +class CompositeProfileWithOnlyKubernetesConfigSourceTests { + + @Autowired + private ConfigurableApplicationContext context; + + @Test + void kubernetesEnvironmentRepositoryIsLoaded() { + assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(2); + } + + @Test + void kubernetesPropertySourceSuppliersAreLoaded() { + assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty(); + } + +} diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryFactoryTests.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryFactoryTests.java new file mode 100644 index 0000000000..d47417aa4a --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryFactoryTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2013-2022 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.configserver; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.cloud.config.server.environment.EnvironmentRepository; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@SpringJUnitConfig +@SpringBootTest +class KubernetesEnvironmentRepositoryFactoryTests { + + @MockBean + private ObjectProvider kubernetesEnvironmentRepositoryProvider; + + @Test + void testBuild() { + KubernetesEnvironmentRepository mockRepository = mock(KubernetesEnvironmentRepository.class); + when(kubernetesEnvironmentRepositoryProvider.getIfAvailable()).thenReturn(mockRepository); + + KubernetesEnvironmentRepositoryFactory factory = new KubernetesEnvironmentRepositoryFactory( + kubernetesEnvironmentRepositoryProvider); + KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties(); + + EnvironmentRepository repository = factory.build(properties); + + assertThat(repository).isNotNull(); + assertThat(repository).isInstanceOf(KubernetesEnvironmentRepository.class); + assertThat(repository).isEqualTo(mockRepository); + } + + @Configuration + static class TestConfig { + + @Bean + public KubernetesEnvironmentRepositoryFactory kubernetesEnvironmentRepositoryFactory( + ObjectProvider kubernetesEnvironmentRepositoryProvider) { + return new KubernetesEnvironmentRepositoryFactory(kubernetesEnvironmentRepositoryProvider); + } + + } + +}