some refactoring in config maps functionality (#834)
* some refactor * some change in k8s-client * trigger * re-trigger * lazy compute sources * re-trigger * re-trigger again
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.kubernetes.client.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -38,7 +39,7 @@ public class KubernetesClientConfigMapPropertySource extends ConfigMapPropertySo
|
||||
|
||||
public KubernetesClientConfigMapPropertySource(CoreV1Api coreV1Api, String name, String namespace,
|
||||
Environment environment) {
|
||||
super(getName(name, namespace), asObjectMap(getData(coreV1Api, name, namespace, environment)));
|
||||
super(getName(name, namespace), getData(coreV1Api, name, namespace, environment));
|
||||
}
|
||||
|
||||
private static Map<String, Object> getData(CoreV1Api coreV1Api, String name, String namespace,
|
||||
@@ -62,7 +63,7 @@ public class KubernetesClientConfigMapPropertySource extends ConfigMapPropertySo
|
||||
catch (ApiException e) {
|
||||
LOG.warn("Unable to get ConfigMap " + name + " in namespace " + namespace, e);
|
||||
}
|
||||
return new LinkedHashMap<>();
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.commons.config;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -29,6 +29,11 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.APPLICATION_PROPERTIES;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.APPLICATION_YAML;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.APPLICATION_YML;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.PREFIX;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.KEY_VALUE_TO_PROPERTIES;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.PROPERTIES_TO_MAP;
|
||||
import static org.springframework.cloud.kubernetes.commons.config.PropertySourceUtils.throwingMerger;
|
||||
@@ -45,14 +50,6 @@ public abstract class ConfigMapPropertySource extends MapPropertySource {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(ConfigMapPropertySource.class);
|
||||
|
||||
protected static final String APPLICATION_YML = "application.yml";
|
||||
|
||||
protected static final String APPLICATION_YAML = "application.yaml";
|
||||
|
||||
protected static final String APPLICATION_PROPERTIES = "application.properties";
|
||||
|
||||
protected static final String PREFIX = "configmap";
|
||||
|
||||
public ConfigMapPropertySource(String name, Map<String, Object> source) {
|
||||
super(name, source);
|
||||
}
|
||||
@@ -64,37 +61,26 @@ public abstract class ConfigMapPropertySource extends MapPropertySource {
|
||||
}
|
||||
|
||||
protected static String getName(String name, String namespace) {
|
||||
return new StringBuilder().append(PREFIX).append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(name)
|
||||
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(namespace).toString();
|
||||
return PREFIX + PROPERTY_SOURCE_NAME_SEPARATOR + name + PROPERTY_SOURCE_NAME_SEPARATOR + namespace;
|
||||
}
|
||||
|
||||
protected static Map<String, Object> processAllEntries(Map<String, String> input, Environment environment) {
|
||||
|
||||
Set<Entry<String, String>> entrySet = input.entrySet();
|
||||
Set<Map.Entry<String, String>> entrySet = input.entrySet();
|
||||
if (entrySet.size() == 1) {
|
||||
// we handle the case where the configmap contains a single "file"
|
||||
// in this case we don't care what the name of t he file is
|
||||
Entry<String, String> singleEntry = entrySet.iterator().next();
|
||||
// in this case we don't care what the name of the file is
|
||||
Map.Entry<String, String> singleEntry = entrySet.iterator().next();
|
||||
String propertyName = singleEntry.getKey();
|
||||
String propertyValue = singleEntry.getValue();
|
||||
if (propertyName.endsWith(".yml") || propertyName.endsWith(".yaml")) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("The single property with name: [" + propertyName + "] will be treated as a yaml file");
|
||||
}
|
||||
|
||||
LOG.debug("The single property with name: [" + propertyName + "] will be treated as a yaml file");
|
||||
return yamlParserGenerator(environment).andThen(PROPERTIES_TO_MAP).apply(propertyValue);
|
||||
}
|
||||
else if (propertyName.endsWith(".properties")) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("The single property with name: [" + propertyName
|
||||
+ "] will be treated as a properties file");
|
||||
}
|
||||
|
||||
LOG.debug("The single property with name: [" + propertyName + "] will be treated as a properties file");
|
||||
return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(propertyValue);
|
||||
}
|
||||
else {
|
||||
return defaultProcessAllEntries(input, environment);
|
||||
}
|
||||
}
|
||||
|
||||
return defaultProcessAllEntries(input, environment);
|
||||
@@ -104,7 +90,7 @@ public abstract class ConfigMapPropertySource extends MapPropertySource {
|
||||
|
||||
return input.entrySet().stream().map(e -> extractProperties(e.getKey(), e.getValue(), environment))
|
||||
.filter(m -> !m.isEmpty()).flatMap(m -> m.entrySet().stream())
|
||||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, throwingMerger(), LinkedHashMap::new));
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, throwingMerger(), HashMap::new));
|
||||
}
|
||||
|
||||
protected static Map<String, Object> extractProperties(String resourceName, String content,
|
||||
@@ -117,16 +103,7 @@ public abstract class ConfigMapPropertySource extends MapPropertySource {
|
||||
return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(content);
|
||||
}
|
||||
|
||||
return new LinkedHashMap<String, Object>() {
|
||||
{
|
||||
put(resourceName, content);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected static Map<String, Object> asObjectMap(Map<String, Object> source) {
|
||||
return source.entrySet().stream()
|
||||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, throwingMerger(), LinkedHashMap::new));
|
||||
return Collections.singletonMap(resourceName, content);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
protected abstract MapPropertySource getMapPropertySource(String name, NormalizedSource normalizedSource,
|
||||
protected abstract MapPropertySource getMapPropertySource(String applicationName, NormalizedSource normalizedSource,
|
||||
String configurationTarget, ConfigurableEnvironment environment);
|
||||
|
||||
@Override
|
||||
@@ -65,9 +65,9 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo
|
||||
if (environment instanceof ConfigurableEnvironment) {
|
||||
ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
|
||||
|
||||
List<NormalizedSource> sources = this.properties.determineSources();
|
||||
CompositePropertySource composite = new CompositePropertySource("composite-configmap");
|
||||
if (this.properties.isEnableApi()) {
|
||||
List<NormalizedSource> sources = this.properties.determineSources();
|
||||
sources.forEach(s -> composite.addFirstPropertySource(getMapPropertySourceForSingleConfigMap(env, s)));
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,26 @@ public final class Constants {
|
||||
*/
|
||||
public static final String SPRING_CONFIG_ACTIVATE_ON_PROFILE = "spring.config.activate.on-profile";
|
||||
|
||||
/**
|
||||
* application.yml property.
|
||||
*/
|
||||
public static final String APPLICATION_YML = "application.yml";
|
||||
|
||||
/**
|
||||
* application.yaml property.
|
||||
*/
|
||||
public static final String APPLICATION_YAML = "application.yaml";
|
||||
|
||||
/**
|
||||
* application.properties property.
|
||||
*/
|
||||
public static final String APPLICATION_PROPERTIES = "application.properties";
|
||||
|
||||
/**
|
||||
* prefix of the configMap.
|
||||
*/
|
||||
public static final String PREFIX = "configmap";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.fabric8.config;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||
@@ -29,6 +30,8 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigUtils.getNamespace;
|
||||
|
||||
/**
|
||||
* A {@link MapPropertySource} that uses Kubernetes config maps.
|
||||
*
|
||||
@@ -41,25 +44,21 @@ public class Fabric8ConfigMapPropertySource extends ConfigMapPropertySource {
|
||||
private static final Log LOG = LogFactory.getLog(Fabric8ConfigMapPropertySource.class);
|
||||
|
||||
public Fabric8ConfigMapPropertySource(KubernetesClient client, String name) {
|
||||
this(client, name, null, (Environment) null);
|
||||
this(client, name, null, null);
|
||||
}
|
||||
|
||||
public Fabric8ConfigMapPropertySource(KubernetesClient client, String name, String namespace,
|
||||
public Fabric8ConfigMapPropertySource(KubernetesClient client, String applicationName, String namespace,
|
||||
Environment environment) {
|
||||
super(getName(name, getNamespace(client, namespace)),
|
||||
asObjectMap(getData(client, name, getNamespace(client, namespace), environment)));
|
||||
super(getName(applicationName, getNamespace(client, namespace)),
|
||||
getData(client, applicationName, getNamespace(client, namespace), environment));
|
||||
}
|
||||
|
||||
private static String getNamespace(KubernetesClient client, String namespace) {
|
||||
return StringUtils.isEmpty(namespace) ? client.getNamespace() : namespace;
|
||||
}
|
||||
|
||||
private static Map<String, Object> getData(KubernetesClient client, String name, String namespace,
|
||||
private static Map<String, Object> getData(KubernetesClient client, String applicationName, String namespace,
|
||||
Environment environment) {
|
||||
try {
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
ConfigMap map = StringUtils.isEmpty(namespace) ? client.configMaps().withName(name).get()
|
||||
: client.configMaps().inNamespace(namespace).withName(name).get();
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
ConfigMap map = !StringUtils.hasLength(namespace) ? client.configMaps().withName(applicationName).get()
|
||||
: client.configMaps().inNamespace(namespace).withName(applicationName).get();
|
||||
|
||||
if (map != null) {
|
||||
result.putAll(processAllEntries(map.getData(), environment));
|
||||
@@ -68,9 +67,9 @@ public class Fabric8ConfigMapPropertySource extends ConfigMapPropertySource {
|
||||
if (environment != null) {
|
||||
for (String activeProfile : environment.getActiveProfiles()) {
|
||||
|
||||
String mapNameWithProfile = name + "-" + activeProfile;
|
||||
String mapNameWithProfile = applicationName + "-" + activeProfile;
|
||||
|
||||
ConfigMap mapWithProfile = StringUtils.isEmpty(namespace)
|
||||
ConfigMap mapWithProfile = !StringUtils.hasLength(namespace)
|
||||
? client.configMaps().withName(mapNameWithProfile).get()
|
||||
: client.configMaps().inNamespace(namespace).withName(mapNameWithProfile).get();
|
||||
|
||||
@@ -85,10 +84,11 @@ public class Fabric8ConfigMapPropertySource extends ConfigMapPropertySource {
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring.", e);
|
||||
LOG.warn("Can't read configMap with name: [" + applicationName + "] in namespace:[" + namespace
|
||||
+ "]. Ignoring.", e);
|
||||
}
|
||||
|
||||
return new LinkedHashMap<>();
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,11 +45,11 @@ public class Fabric8ConfigMapPropertySourceLocator extends ConfigMapPropertySour
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MapPropertySource getMapPropertySource(String name, NormalizedSource normalizedSource,
|
||||
protected MapPropertySource getMapPropertySource(String applicationName, NormalizedSource normalizedSource,
|
||||
String configurationTarget, ConfigurableEnvironment environment) {
|
||||
return new Fabric8ConfigMapPropertySource(this.client, name,
|
||||
getApplicationNamespace(this.client, normalizedSource.getNamespace(), configurationTarget),
|
||||
environment);
|
||||
String namespaceName = getApplicationNamespace(this.client, normalizedSource.getNamespace(),
|
||||
configurationTarget);
|
||||
return new Fabric8ConfigMapPropertySource(this.client, applicationName, namespaceName, environment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,4 +45,8 @@ public final class Fabric8ConfigUtils {
|
||||
return configNamespace;
|
||||
}
|
||||
|
||||
static String getNamespace(KubernetesClient client, String namespace) {
|
||||
return !StringUtils.hasLength(namespace) ? client.getNamespace() : namespace;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user