added support for ConfigMap build from files

This commit is contained in:
Raianu, Mihaela
2018-03-06 11:18:47 +02:00
committed by Ioannis Canellos
parent 8b87c908df
commit 1f8009e8bc
4 changed files with 61 additions and 16 deletions

View File

@@ -76,29 +76,41 @@ public class ConfigMapPropertySource extends KubernetesPropertySource {
: client.configMaps().inNamespace(namespace).withName(name).get();
if (map != null) {
for (Map.Entry<String, String> entry : map.getData().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.equals(APPLICATION_YAML) || key.equals(APPLICATION_YML)) {
result.putAll(yamlParserGenerator(profiles).andThen(PROPERTIES_TO_MAP).apply(value));
} else if (key.equals(APPLICATION_PROPERTIES)) {
result.putAll(KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(value));
} else {
result.put(key, value);
}
}
result.putAll(processAllEntries(map.getData(), profiles));
}
} catch (Exception e) {
LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring", e);
}
}
// read for secrets mount
putPathConfig(result, config.getPaths());
Map<String, String> configsFromPaths = new HashMap<>();
putPathConfig(configsFromPaths, config.getPaths());
result.putAll(processAllEntries(configsFromPaths, profiles));
return result;
}
private static Map<String, String> processAllEntries(Map<String, String> input,
String[] profiles) {
return input.entrySet().stream()
.map(e -> extractProperties(e.getKey(), e.getValue(), profiles))
.filter(m -> !m.isEmpty())
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
}
private static Map<String, String> extractProperties(String resourceName, String content, String[] profiles) {
Map<String, String> result = new HashMap<>();
if (resourceName.equals(APPLICATION_YAML) || resourceName.equals(APPLICATION_YML)) {
result.putAll(yamlParserGenerator(profiles).andThen(PROPERTIES_TO_MAP).apply(content));
} else if (resourceName.equals(APPLICATION_PROPERTIES)) {
result.putAll(KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(content));
} else {
result.put(resourceName, content);
}
return result;
}
private static Map<String, Object> asObjectMap(Map<String, String> source) {
return source.entrySet()
.stream()

View File

@@ -17,6 +17,7 @@
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;
@@ -88,8 +89,13 @@ public class ConfigMapsTest {
createConfigMapFile(apiPath, "api.url", "http://localhost/api");
createConfigMapFile(apiPath, "foo.bar", "42");
final Path filesPath = tmp.resolve("cm/files");
createConfigMapFile(filesPath, "application.yaml", readResourceFile("application.yaml"));
createConfigMapFile(filesPath, "application.properties", readResourceFile("application.properties"));
// parse ConfigMaps
cmConfProperties.setPaths(Arrays.asList(dbPath.toString(), apiPath.toString()));
cmConfProperties.setPaths(Arrays.asList(dbPath.toString(), apiPath.toString(), filesPath.toString()));
ConfigMapPropertySource cmps = new ConfigMapPropertySource(client, "testapp", cmConfProperties);
// assert as expected
@@ -98,7 +104,26 @@ public class ConfigMapsTest {
assertEquals("http://localhost/api", cmps.getProperty("api.url"));
assertFalse(cmps.containsProperty("no.such.property"));
FileSystemUtils.deleteRecursively(tmp.toFile());
assertEquals("a", cmps.getProperty("dummy.property.string1"));
assertEquals("1", cmps.getProperty("dummy.property.int1"));
assertEquals("true", cmps.getProperty("dummy.property.bool1"));
assertEquals("a", cmps.getProperty("dummy.property.string2"));
assertEquals("1", cmps.getProperty("dummy.property.int2"));
assertEquals("true", cmps.getProperty("dummy.property.bool2"));
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 {

View File

@@ -0,0 +1,3 @@
dummy.property.string1=a
dummy.property.int1=1
dummy.property.bool1=true

View File

@@ -0,0 +1,5 @@
dummy:
property:
string2: "a"
int2: 1
bool2: true