From 2dc711fa66cb548775f768f7704d57fbd618880d Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 21 May 2018 15:56:32 +0300 Subject: [PATCH] Fix ConfigMap with profiles functionality Fixes: #173 --- .../config/ConfigMapPropertySource.java | 21 ++-- .../kubernetes/config/ConfigMapTestUtil.java | 23 ++++ .../kubernetes/config/ConfigMapsTest.java | 13 +-- .../ConfigMapsWithProfilesSpringBootTest.java | 103 ++++++++++++++++++ .../resources/application-with-profiles.yaml | 12 ++ 5 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java create mode 100644 spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java create mode 100644 spring-cloud-kubernetes-config/src/test/resources/application-with-profiles.yaml diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java index 6cc407fb..2f20e830 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java @@ -17,6 +17,12 @@ package org.springframework.cloud.kubernetes.config; +import static java.util.Arrays.asList; +import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.FOUND; +import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.NOT_FOUND; + +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.client.KubernetesClient; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.HashMap; @@ -24,12 +30,9 @@ import java.util.Map; import java.util.Properties; import java.util.function.Function; import java.util.stream.Collectors; - -import io.fabric8.kubernetes.api.model.ConfigMap; -import io.fabric8.kubernetes.client.KubernetesClient; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.config.YamlProcessor.DocumentMatcher; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.boot.yaml.SpringProfileDocumentMatcher; import org.springframework.core.io.ByteArrayResource; @@ -120,10 +123,12 @@ public class ConfigMapPropertySource extends KubernetesPropertySource { private static Function yamlParserGenerator(final String[] profiles) { return s -> { YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); - if (profiles == null) { - yamlFactory.setDocumentMatchers(new SpringProfileDocumentMatcher()); - } else { - yamlFactory.setDocumentMatchers(new SpringProfileDocumentMatcher(profiles)); + if (profiles != null) { + yamlFactory.setDocumentMatchers( + (DocumentMatcher) properties -> + (asList(profiles).contains(properties.getProperty("spring.profiles")) ? + FOUND : NOT_FOUND) + ); } yamlFactory.setResources(new ByteArrayResource(s.getBytes())); return yamlFactory.getObject(); diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java new file mode 100644 index 00000000..c2c2450d --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java @@ -0,0 +1,23 @@ +package org.springframework.cloud.kubernetes.config; + +import io.fabric8.kubernetes.client.utils.IOHelpers; +import java.io.IOException; + +final class ConfigMapTestUtil { + + private ConfigMapTestUtil() { + } + + + static String readResourceFile(String file) { + String resource; + try { + resource = IOHelpers.readFully( + ConfigMapTestUtil.class.getClassLoader().getResourceAsStream(file)); + } + catch (IOException e) { + resource = ""; + } + return resource; + } +} diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java index 4e00980e..8d8773b3 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java @@ -17,7 +17,6 @@ package org.springframework.cloud.kubernetes.config; -import io.fabric8.kubernetes.client.utils.IOHelpers; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -37,6 +36,7 @@ import org.springframework.util.FileSystemUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.*; /** * @author Charles Moulliard @@ -115,17 +115,6 @@ public class ConfigMapsTest { FileSystemUtils.deleteRecursively(tmp.toFile()); } - private String readResourceFile(String file) { - String resource; - try { - resource = IOHelpers.readFully(getClass().getClassLoader().getResourceAsStream(file)); - } - catch (IOException e) { - resource = ""; - } - return resource; - } - private void createConfigMapFile(Path basePath, String key, String value) throws IOException { Files.createDirectories(basePath); final Path apiUrlFile = Files.createFile(basePath.resolve(key)); diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java new file mode 100644 index 00000000..64dbd26c --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2016 to the original 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 + * + * http://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 static io.restassured.RestAssured.when; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.*; + +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.KubernetesServer; +import io.restassured.RestAssured; +import java.util.HashMap; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.config.example.App; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author Charles Moulliard + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = App.class, + properties = { "spring.application.name=configmap-with-profile-example", + "spring.cloud.kubernetes.reload.enabled=false"} + ) +@ActiveProfiles("development") +public class ConfigMapsWithProfilesSpringBootTest { + + @ClassRule + public static KubernetesServer server = new KubernetesServer(); + + private static KubernetesClient mockClient; + + @Autowired(required = false) + Config config; + + private static final String APPLICATION_NAME = "configmap-with-profile-example"; + + @Value("${local.server.port}") + private int port; + + @BeforeClass + public static void setUpBeforeClass() { + mockClient = server.getClient(); + + //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"); + + HashMap data = new HashMap<>(); + data.put("application.yml", readResourceFile("application-with-profiles.yaml")); + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME).andReturn(200, new ConfigMapBuilder() + .withNewMetadata().withName(APPLICATION_NAME).endMetadata() + .addToData(data) + .build()) + .always(); + } + + @Before + public void setUp() { + RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port); + } + + @Test + public void testGreetingEndpoint() { + when().get() + .then() + .statusCode(200) + .body("content", is("Hello ConfigMap dev, World!")); + } + +} diff --git a/spring-cloud-kubernetes-config/src/test/resources/application-with-profiles.yaml b/spring-cloud-kubernetes-config/src/test/resources/application-with-profiles.yaml new file mode 100644 index 00000000..ea00f59a --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/resources/application-with-profiles.yaml @@ -0,0 +1,12 @@ +bean: + message: "Hello ConfigMap default, %s!" +--- +spring: + profiles: development +bean: + message: "Hello ConfigMap dev, %s!" +--- +spring: + profiles: production +bean: + message: "Hello ConfigMap prod, %s!"