move configmap source to record (#1105)

This commit is contained in:
erabii
2022-10-22 00:06:07 +03:00
committed by GitHub
parent 58ef67a4f9
commit 025571e36d
3 changed files with 43 additions and 159 deletions

View File

@@ -113,9 +113,9 @@ class KubernetesClientConfigMapPropertySourceLocatorTests {
.willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(PROPERTIES_CONFIGMAP_LIST))));
ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties();
configMapConfigProperties.setName("fake-name");
ConfigMapConfigProperties.Source source = new ConfigMapConfigProperties.Source();
source.setName("bootstrap-640");
source.setNamespace("default");
ConfigMapConfigProperties.Source source = new ConfigMapConfigProperties.Source(
"bootstrap-640", "default", Collections.emptyMap(), null, null, null
);
List<ConfigMapConfigProperties.Source> sources = Collections.singletonList(source);
configMapConfigProperties.setSources(sources);
KubernetesClientProperties kubernetesClientProperties = new KubernetesClientProperties();

View File

@@ -20,11 +20,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
@@ -109,100 +109,16 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties {
/**
* Config map source.
* @param name The name of the ConfigMap.
* @param namespace The namespace where the ConfigMap is found.
* @param labels labels of the config map to look for against.
* @param explicitPrefix An explicit prefix to be used for properties.
* @param useNameAsPrefix Use config map name as prefix for properties.
* @param includeProfileSpecificSources Use profile name to append to a config map
* name.
*/
public static class Source {
/**
* The name of the ConfigMap.
*/
private String name;
/**
* The namespace where the ConfigMap is found.
*/
private String namespace;
/**
* labels of the config map to look for against.
*/
private Map<String, String> labels = Collections.emptyMap();
/**
* An explicit prefix to be used for properties.
*/
private String explicitPrefix;
/**
* Use config map name as prefix for properties. Can't be a primitive, we need to
* know if it was explicitly set or not
*/
private Boolean useNameAsPrefix;
/**
* Use profile name to append to a config map name. Can't be a primitive, we need
* to know if it was explicitly set or not
*/
protected Boolean includeProfileSpecificSources;
public Source() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getNamespace() {
return this.namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public Boolean isUseNameAsPrefix() {
return useNameAsPrefix;
}
public Boolean getUseNameAsPrefix() {
return useNameAsPrefix;
}
public void setUseNameAsPrefix(Boolean useNameAsPrefix) {
this.useNameAsPrefix = useNameAsPrefix;
}
public String getExplicitPrefix() {
return explicitPrefix;
}
public void setExplicitPrefix(String explicitPrefix) {
this.explicitPrefix = explicitPrefix;
}
public Boolean getIncludeProfileSpecificSources() {
return includeProfileSpecificSources;
}
public void setIncludeProfileSpecificSources(Boolean includeProfileSpecificSources) {
this.includeProfileSpecificSources = includeProfileSpecificSources;
}
public Map<String, String> getLabels() {
return labels;
}
public void setLabels(Map<String, String> labels) {
this.labels = labels;
}
public boolean isEmpty() {
return !StringUtils.hasLength(this.name) && !StringUtils.hasLength(this.namespace);
}
public record Source(String name, String namespace, @DefaultValue Map<String, String> labels, String explicitPrefix,
Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) {
private Stream<NormalizedSource> normalize(String defaultName, String defaultNamespace,
Map<String, String> defaultLabels, boolean defaultIncludeProfileSpecificSources, boolean failFast,
@@ -235,23 +151,6 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties {
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Source other = (Source) o;
return Objects.equals(this.name, other.name) && Objects.equals(this.namespace, other.namespace);
}
@Override
public int hashCode() {
return Objects.hash(name, namespace);
}
}
}

View File

@@ -110,8 +110,8 @@ class ConfigMapConfigPropertiesTests {
properties.setUseNameAsPrefix(true);
properties.setNamespace("spring-k8s");
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source();
one.setName("config-map-one");
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source("config-map-one", null,
Collections.emptyMap(), null, null, null);
properties.setSources(Collections.singletonList(one));
List<NormalizedSource> sources = properties.determineSources(new MockEnvironment());
@@ -148,16 +148,14 @@ class ConfigMapConfigPropertiesTests {
properties.setUseNameAsPrefix(true);
properties.setNamespace("spring-k8s");
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source();
one.setName("config-map-one");
one.setUseNameAsPrefix(false);
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source("config-map-one", null,
Collections.emptyMap(), null, false, null);
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source();
two.setName("config-map-two");
two.setUseNameAsPrefix(true);
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source("config-map-two", null,
Collections.emptyMap(), null, true, null);
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source();
three.setName("config-map-three");
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source("config-map-three", null,
Collections.emptyMap(), null, true, null);
properties.setSources(Arrays.asList(one, two, three));
@@ -198,22 +196,17 @@ class ConfigMapConfigPropertiesTests {
properties.setUseNameAsPrefix(false);
properties.setNamespace("spring-k8s");
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source();
one.setNamespace("config-map-one");
one.setUseNameAsPrefix(false);
one.setExplicitPrefix("one");
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source("config-map-one", null,
Collections.emptyMap(), "one", false, null);
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source();
two.setNamespace("config-map-two");
two.setUseNameAsPrefix(true);
two.setExplicitPrefix("two");
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source("config-map-two", null,
Collections.emptyMap(), "two", true, null);
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source();
three.setNamespace("config-map-three");
three.setExplicitPrefix("three");
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source("config-map-three", null,
Collections.emptyMap(), "three", false, null);
ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source();
four.setNamespace("config-map-four");
ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source(null, "config-map-four",
Collections.emptyMap(), null, false, null);
properties.setSources(Arrays.asList(one, two, three, four));
@@ -318,16 +311,14 @@ class ConfigMapConfigPropertiesTests {
properties.setNamespace("spring-k8s");
properties.setIncludeProfileSpecificSources(false);
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source();
one.setName("config-map-one");
one.setIncludeProfileSpecificSources(true);
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source("config-map-one", null,
Collections.emptyMap(), "one", null, true);
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source();
two.setName("config-map-two");
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source("config-map-two", null,
Collections.emptyMap(), null, false, null);
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source();
three.setName("config-map-three");
three.setIncludeProfileSpecificSources(false);
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source("config-map-three", null,
Collections.emptyMap(), null, null, false);
properties.setSources(Arrays.asList(one, two, three));
@@ -377,23 +368,17 @@ class ConfigMapConfigPropertiesTests {
properties.setNamespace("spring-k8s");
properties.setIncludeProfileSpecificSources(false);
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source();
one.setLabels(Map.of("first-label", "configmap-one"));
one.setUseNameAsPrefix(false);
one.setExplicitPrefix("one");
ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source(null, null,
Map.of("first-label", "configmap-one"), "one", false, null);
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source();
two.setLabels(Map.of("second-label", "configmap-two"));
two.setUseNameAsPrefix(true);
two.setExplicitPrefix("two");
two.setIncludeProfileSpecificSources(true);
ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source(null, null,
Map.of("second-label", "configmap-two"), "two", true, true);
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source();
three.setLabels(Map.of("third-label", "configmap-three"));
three.setExplicitPrefix("three");
ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source(null, null,
Map.of("third-label", "configmap-three"), "three", null, null);
ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source();
four.setLabels(Map.of("fourth-label", "configmap-four"));
ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source(null, null,
Map.of("fourth-label", "configmap-four"), null, null, null);
properties.setSources(Arrays.asList(one, two, three, four));