From 025571e36d8ba30fe8ef1a20420748c886cfe4ff Mon Sep 17 00:00:00 2001 From: erabii Date: Sat, 22 Oct 2022 00:06:07 +0300 Subject: [PATCH] move configmap source to record (#1105) --- ...ntConfigMapPropertySourceLocatorTests.java | 6 +- .../config/ConfigMapConfigProperties.java | 121 ++---------------- .../ConfigMapConfigPropertiesTests.java | 75 +++++------ 3 files changed, 43 insertions(+), 159 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java index 9a045e30..8d0779fe 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocatorTests.java @@ -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 sources = Collections.singletonList(source); configMapConfigProperties.setSources(sources); KubernetesClientProperties kubernetesClientProperties = new KubernetesClientProperties(); diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java index bcf0406a..d9f921b9 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigProperties.java @@ -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 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 getLabels() { - return labels; - } - - public void setLabels(Map 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 labels, String explicitPrefix, + Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) { private Stream normalize(String defaultName, String defaultNamespace, Map 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); - } - } } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java index b0d100fd..64baff08 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java @@ -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 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));