From a31ae5e8e47ffd933de17e5c1bbaf4be281cc376 Mon Sep 17 00:00:00 2001 From: Charles Moulliard Date: Fri, 23 Jun 2017 09:06:53 +0200 Subject: [PATCH] Pass active profiles to the class extracting from yaml configmap --- .../config/ConfigMapPropertySource.java | 36 ++++++++++++------- .../ConfigMapPropertySourceLocator.java | 2 +- 2 files changed, 25 insertions(+), 13 deletions(-) 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 96e9576a..e106fcd8 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 @@ -30,7 +30,9 @@ 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; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.boot.yaml.SpringProfileDocumentMatcher; import org.springframework.core.env.MapPropertySource; import org.springframework.core.io.ByteArrayResource; @@ -43,12 +45,16 @@ public class ConfigMapPropertySource extends MapPropertySource { private static final String PREFIX = "configmap"; - public ConfigMapPropertySource(KubernetesClient client, String name) { - this(client, name, null); + public ConfigMapPropertySource(KubernetesClient client, String name) { + this(client, name, null); + } + + public ConfigMapPropertySource(KubernetesClient client, String name, String[] profiles) { + this(client, name, null, profiles); } - public ConfigMapPropertySource(KubernetesClient client, String name, String namespace) { - super(getName(client, name, namespace), asObjectMap(getData(client, name, namespace))); + public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, String[] profiles) { + super(getName(client, name, namespace), asObjectMap(getData(client, name, namespace, profiles))); } private static String getName(KubernetesClient client, String name, String namespace) { @@ -61,7 +67,7 @@ public class ConfigMapPropertySource extends MapPropertySource { .toString(); } - private static Map getData(KubernetesClient client, String name, String namespace) { + private static Map getData(KubernetesClient client, String name, String namespace, String[] profiles) { Map result = new HashMap<>(); try { ConfigMap map = namespace == null || namespace.isEmpty() @@ -73,7 +79,7 @@ public class ConfigMapPropertySource extends MapPropertySource { 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)); + 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 { @@ -93,12 +99,18 @@ public class ConfigMapPropertySource extends MapPropertySource { .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 yamlParserGenerator(final String[] profiles) { + return s -> { + YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); + if (profiles == null) { + yamlFactory.setDocumentMatchers(new SpringProfileDocumentMatcher()); + } else { + yamlFactory.setDocumentMatchers(new SpringProfileDocumentMatcher(profiles)); + } + yamlFactory.setResources(new ByteArrayResource(s.getBytes())); + return yamlFactory.getObject(); + }; + } private static final Function KEY_VALUE_TO_PROPERTIES = s -> { Properties properties = new Properties(); diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java index 0bf7c819..ed14bc5c 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java @@ -41,7 +41,7 @@ public class ConfigMapPropertySourceLocator implements PropertySourceLocator { ConfigurableEnvironment env = (ConfigurableEnvironment) environment; String name = getApplicationName(environment, properties); String namespace = getApplicationNamespace(client, env, properties); - return new ConfigMapPropertySource(client, name, namespace); + return new ConfigMapopertySource(client, name, namespace, env.getActiveProfiles()); } return null; }