Merge branch '2.0.x' into main

This commit is contained in:
Ryan Baxter
2021-07-27 11:03:50 -04:00
5 changed files with 41 additions and 40 deletions

View File

@@ -62,12 +62,12 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties {
}
/**
* @return A list of Source to use If the user has not specified any Source
* @return A list of Source to use. If the user has not specified any Source
* properties, then a single Source is constructed based on the supplied name and
* namespace
* namespace.
*
* These are the actual name/namespace pairs that are used to create a
* ConfigMapPropertySource
* ConfigMapPropertySource.
*/
public List<NormalizedSource> determineSources() {
if (this.sources.isEmpty()) {
@@ -153,6 +153,11 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties {
return this.namespace;
}
@Override
public String toString() {
return "{ config-map name : '" + name + "', namespace : '" + namespace + "' }";
}
}
}

View File

@@ -27,7 +27,6 @@ import org.apache.commons.logging.LogFactory;
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;
@@ -54,14 +53,8 @@ public abstract class ConfigMapPropertySource extends MapPropertySource {
super(name, source);
}
protected static Environment createEnvironmentWithActiveProfiles(String[] activeProfiles) {
StandardEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles(activeProfiles);
return environment;
}
protected static String getName(String name, String namespace) {
return PREFIX + PROPERTY_SOURCE_NAME_SEPARATOR + name + PROPERTY_SOURCE_NAME_SEPARATOR + namespace;
protected static String getName(String applicationName, String namespace) {
return PREFIX + PROPERTY_SOURCE_NAME_SEPARATOR + applicationName + PROPERTY_SOURCE_NAME_SEPARATOR + namespace;
}
protected static Map<String, Object> processAllEntries(Map<String, String> input, Environment environment) {
@@ -89,7 +82,7 @@ public abstract class ConfigMapPropertySource extends MapPropertySource {
protected static Map<String, Object> defaultProcessAllEntries(Map<String, String> input, Environment environment) {
return input.entrySet().stream().map(e -> extractProperties(e.getKey(), e.getValue(), environment))
.filter(m -> !m.isEmpty()).flatMap(m -> m.entrySet().stream())
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, throwingMerger(), HashMap::new));
}

View File

@@ -68,6 +68,7 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo
CompositePropertySource composite = new CompositePropertySource("composite-configmap");
if (this.properties.isEnableApi()) {
List<NormalizedSource> sources = this.properties.determineSources();
LOG.debug("Config Map normalized sources : " + sources);
sources.forEach(s -> composite.addFirstPropertySource(getMapPropertySourceForSingleConfigMap(env, s)));
}

View File

@@ -20,7 +20,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -28,9 +27,9 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.kubernetes.commons.config.ConfigMapPropertySource;
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;
import static org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigUtils.getApplicationNamespace;
import static org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigUtils.getConfigMapData;
/**
* A {@link MapPropertySource} that uses Kubernetes config maps.
@@ -49,34 +48,21 @@ public class Fabric8ConfigMapPropertySource extends ConfigMapPropertySource {
public Fabric8ConfigMapPropertySource(KubernetesClient client, String applicationName, String namespace,
Environment environment) {
super(getName(applicationName, getNamespace(client, namespace)),
getData(client, applicationName, getNamespace(client, namespace), environment));
super(getName(applicationName, getApplicationNamespace(client, namespace)),
getData(client, applicationName, getApplicationNamespace(client, namespace), environment));
}
private static Map<String, Object> getData(KubernetesClient client, String applicationName, String namespace,
Environment environment) {
try {
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));
}
Map<String, String> data = getConfigMapData(client, namespace, applicationName);
Map<String, Object> result = new HashMap<>(processAllEntries(data, environment));
if (environment != null) {
for (String activeProfile : environment.getActiveProfiles()) {
String mapNameWithProfile = applicationName + "-" + activeProfile;
ConfigMap mapWithProfile = !StringUtils.hasLength(namespace)
? client.configMaps().withName(mapNameWithProfile).get()
: client.configMaps().inNamespace(namespace).withName(mapNameWithProfile).get();
if (mapWithProfile != null) {
result.putAll(processAllEntries(mapWithProfile.getData(), environment));
}
Map<String, String> dataWithProfile = getConfigMapData(client, namespace, mapNameWithProfile);
result.putAll(processAllEntries(dataWithProfile, environment));
}
}

View File

@@ -16,6 +16,10 @@
package org.springframework.cloud.kubernetes.fabric8.config;
import java.util.Collections;
import java.util.Map;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -34,19 +38,31 @@ public final class Fabric8ConfigUtils {
private Fabric8ConfigUtils() {
}
public static String getApplicationNamespace(KubernetesClient client, String configNamespace,
public static String getApplicationNamespace(KubernetesClient client, String namespace,
String configurationTarget) {
if (!StringUtils.hasLength(configNamespace)) {
if (!StringUtils.hasLength(namespace)) {
LOG.debug(configurationTarget + " namespace has not been set, taking it from client (ns="
+ client.getNamespace() + ")");
configNamespace = client.getNamespace();
namespace = client.getNamespace();
}
return configNamespace;
return namespace;
}
static String getNamespace(KubernetesClient client, String namespace) {
public static String getApplicationNamespace(KubernetesClient client, String namespace) {
return !StringUtils.hasLength(namespace) ? client.getNamespace() : namespace;
}
public static Map<String, String> getConfigMapData(KubernetesClient client, String namespace, String name) {
ConfigMap configMap = !StringUtils.hasLength(namespace) ? client.configMaps().withName(name).get()
: client.configMaps().inNamespace(namespace).withName(name).get();
if (configMap == null) {
LOG.warn("config-map with name : '" + name + "' not present in namespace : '" + namespace + "'");
return Collections.emptyMap();
}
return configMap.getData();
}
}