Pass active profiles to the class extracting from yaml configmap
This commit is contained in:
@@ -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<String, String> getData(KubernetesClient client, String name, String namespace) {
|
||||
private static Map<String, String> getData(KubernetesClient client, String name, String namespace, String[] profiles) {
|
||||
Map<String, String> 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<String, Properties> YAML_TO_PROPETIES = s -> {
|
||||
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
|
||||
yamlFactory.setResources(new ByteArrayResource(s.getBytes()));
|
||||
return yamlFactory.getObject();
|
||||
};
|
||||
private static final 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));
|
||||
}
|
||||
yamlFactory.setResources(new ByteArrayResource(s.getBytes()));
|
||||
return yamlFactory.getObject();
|
||||
};
|
||||
}
|
||||
|
||||
private static final Function<String, Properties> KEY_VALUE_TO_PROPERTIES = s -> {
|
||||
Properties properties = new Properties();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user