committed by
Ioannis Canellos
parent
a9940c7350
commit
2dc711fa66
@@ -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<String, Properties> 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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 <a href="mailto:cmoullia@redhat.com">Charles Moulliard</a>
|
||||
@@ -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));
|
||||
|
||||
@@ -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 <a href="mailto:cmoullia@redhat.com">Charles Moulliard</a>
|
||||
*/
|
||||
@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<String,String> 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!"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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!"
|
||||
Reference in New Issue
Block a user