From 7845009ea6ebadf496ebe71be980ad8b39e3903a Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Wed, 20 Apr 2016 15:39:26 +0300 Subject: [PATCH] Automatic expand "application.yml" and "application.properties" found in a configmap. --- .../config/ConfigMapPropertySource.java | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/spring-cloud-kubernetes-core/src/main/java/io/fabric8/spring/cloud/kubernetes/config/ConfigMapPropertySource.java b/spring-cloud-kubernetes-core/src/main/java/io/fabric8/spring/cloud/kubernetes/config/ConfigMapPropertySource.java index 74a2df7f..8dcdaeff 100644 --- a/spring-cloud-kubernetes-core/src/main/java/io/fabric8/spring/cloud/kubernetes/config/ConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-core/src/main/java/io/fabric8/spring/cloud/kubernetes/config/ConfigMapPropertySource.java @@ -19,14 +19,24 @@ package io.fabric8.spring.cloud.kubernetes.config; import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.client.KubernetesClient; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.env.MapPropertySource; +import org.springframework.core.io.ByteArrayResource; -import java.util.Collections; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.HashMap; import java.util.Map; +import java.util.Properties; +import java.util.function.Function; import java.util.stream.Collectors; public class ConfigMapPropertySource extends MapPropertySource { + private static final String APPLICATION_YML = "application.yml"; + private static final String APPLICATION_YAML = "application.yaml"; + private static final String APPLICATION_PROPERTIES = "application.properties"; + private static final String PREFIX = "configmap"; private static final String SEPARATOR = "."; @@ -52,11 +62,25 @@ public class ConfigMapPropertySource extends MapPropertySource { } private static Map getData(KubernetesClient client, String name, String namespace) { + Map result = new HashMap<>(); ConfigMap map = namespace == null || namespace.isEmpty() ? client.configMaps().withName(name).get() : client.configMaps().inNamespace(namespace).withName(name).get(); - return map != null ? map.getData() : Collections.emptyMap(); + 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(YAML_TO_PROPETIES.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); + } + } + } + return result; } private static Map asObjectMap(Map source) { @@ -64,4 +88,28 @@ public class ConfigMapPropertySource extends MapPropertySource { .stream() .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } + + + private static final Function YAML_TO_PROPETIES = s -> { + YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); + yamlFactory.setResources(new ByteArrayResource(s.getBytes())); + return yamlFactory.getObject(); + }; + + private static final Function KEY_VALUE_TO_PROPERTIES = s -> { + Properties properties = new Properties(); + try { + properties.load(new ByteArrayInputStream(s.getBytes())); + return properties; + } catch (IOException e) { + throw new IllegalArgumentException(); + } + }; + + private static final Function> PROPERTIES_TO_MAP = p -> p.entrySet().stream() + .collect(Collectors.toMap( + e -> String.valueOf(e.getKey()), + e -> String.valueOf(e.getValue()))); + + }