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 ff8f3138..6cc407fb 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 @@ -76,29 +76,41 @@ public class ConfigMapPropertySource extends KubernetesPropertySource { : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { - for (Map.Entry 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 configsFromPaths = new HashMap<>(); + putPathConfig(configsFromPaths, config.getPaths()); + result.putAll(processAllEntries(configsFromPaths, profiles)); return result; } + private static Map processAllEntries(Map 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 extractProperties(String resourceName, String content, String[] profiles) { + Map 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 asObjectMap(Map source) { return source.entrySet() .stream() 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 c14af6c5..4e00980e 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,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 { diff --git a/spring-cloud-kubernetes-config/src/test/resources/application.properties b/spring-cloud-kubernetes-config/src/test/resources/application.properties new file mode 100644 index 00000000..09cecf84 --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/resources/application.properties @@ -0,0 +1,3 @@ +dummy.property.string1=a +dummy.property.int1=1 +dummy.property.bool1=true \ No newline at end of file diff --git a/spring-cloud-kubernetes-config/src/test/resources/application.yaml b/spring-cloud-kubernetes-config/src/test/resources/application.yaml new file mode 100644 index 00000000..5a7a342c --- /dev/null +++ b/spring-cloud-kubernetes-config/src/test/resources/application.yaml @@ -0,0 +1,5 @@ +dummy: + property: + string2: "a" + int2: 1 + bool2: true \ No newline at end of file