From 7cb084fa65c250edab7cea01998bb3f085927ebc Mon Sep 17 00:00:00 2001 From: erabii Date: Fri, 16 Jul 2021 12:09:01 -0400 Subject: [PATCH] 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 --- ...bernetesClientConfigMapPropertySource.java | 5 +- .../config/ConfigMapPropertySource.java | 53 ++++++------------- .../ConfigMapPropertySourceLocator.java | 4 +- .../kubernetes/commons/config/Constants.java | 20 +++++++ .../Fabric8ConfigMapPropertySource.java | 34 ++++++------ ...Fabric8ConfigMapPropertySourceLocator.java | 8 +-- .../fabric8/config/Fabric8ConfigUtils.java | 4 ++ 7 files changed, 65 insertions(+), 63 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySource.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySource.java index 1020f717..ed92fbdd 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySource.java @@ -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 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(); } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySource.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySource.java index 591c8b7c..878aa2a6 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySource.java @@ -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 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 processAllEntries(Map input, Environment environment) { - Set> entrySet = input.entrySet(); + Set> 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 singleEntry = entrySet.iterator().next(); + // in this case we don't care what the name of the file is + Map.Entry 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 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() { - { - put(resourceName, content); - } - }; - } - - protected static Map asObjectMap(Map source) { - return source.entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, Entry::getValue, throwingMerger(), LinkedHashMap::new)); + return Collections.singletonMap(resourceName, content); } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java index bf2f4729..ec8f4dd9 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java @@ -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 sources = this.properties.determineSources(); CompositePropertySource composite = new CompositePropertySource("composite-configmap"); if (this.properties.isEnableApi()) { + List sources = this.properties.determineSources(); sources.forEach(s -> composite.addFirstPropertySource(getMapPropertySourceForSingleConfigMap(env, s))); } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/Constants.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/Constants.java index 81d57329..84c4b513 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/Constants.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/Constants.java @@ -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() { } diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySource.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySource.java index 61a8f6cf..28842249 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySource.java @@ -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 getData(KubernetesClient client, String name, String namespace, + private static Map getData(KubernetesClient client, String applicationName, String namespace, Environment environment) { try { - Map result = new LinkedHashMap<>(); - ConfigMap map = StringUtils.isEmpty(namespace) ? client.configMaps().withName(name).get() - : client.configMaps().inNamespace(namespace).withName(name).get(); + Map 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(); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocator.java index a6c6947b..bde03dcc 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocator.java @@ -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); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtils.java b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtils.java index 22097e32..384da72c 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtils.java +++ b/spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtils.java @@ -45,4 +45,8 @@ public final class Fabric8ConfigUtils { return configNamespace; } + static String getNamespace(KubernetesClient client, String namespace) { + return !StringUtils.hasLength(namespace) ? client.getNamespace() : namespace; + } + }