move secrets source to record (#1106)

This commit is contained in:
erabii
2022-10-22 14:42:09 +03:00
committed by GitHub
parent cf2a0d6082
commit ebbf3d65e0
4 changed files with 57 additions and 143 deletions

View File

@@ -113,8 +113,10 @@ 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("bootstrap-640", "default",
Collections.emptyMap(), null, null, null);
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

@@ -17,6 +17,7 @@
package org.springframework.cloud.kubernetes.client.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.github.tomakehurst.wiremock.WireMockServer;
@@ -106,12 +107,15 @@ class KubernetesClientSecretsPropertySourceLocatorTests {
CoreV1Api api = new CoreV1Api();
stubFor(get(LIST_API).willReturn(aResponse().withStatus(200).withBody(LIST_BODY)));
SecretsConfigProperties secretsConfigProperties = new SecretsConfigProperties();
SecretsConfigProperties.Source source1 = new SecretsConfigProperties.Source();
source1.setName("db-secret");
source1.setNamespace("");
SecretsConfigProperties.Source source2 = new SecretsConfigProperties.Source();
source2.setName("rabbit-password");
source2.setNamespace("");
SecretsConfigProperties.Source source1 = new SecretsConfigProperties.Source(
"db-secret", "", Collections.emptyMap(), null, null, null
);
SecretsConfigProperties.Source source2 = new SecretsConfigProperties.Source(
"rabbit-password", "", Collections.emptyMap(), null, null, null
);
List<SecretsConfigProperties.Source> sources = new ArrayList<>();
sources.add(source1);
sources.add(source2);

View File

@@ -24,6 +24,7 @@ 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;
@@ -107,94 +108,17 @@ public class SecretsConfigProperties extends AbstractConfigProperties {
.collect(Collectors.toList());
}
public static class Source {
/**
* The name of the Secret.
*/
private String name;
/**
* The namespace where the Secret is found.
*/
private String namespace;
/**
* The labels of the Secret to find.
*/
private Map<String, String> labels = Collections.emptyMap();
/**
* An explicit prefix to be used for properties.
*/
private String explicitPrefix;
/**
* Use secret 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 void setLabels(Map<String, String> labels) {
this.labels = labels;
}
public Map<String, String> getLabels() {
return this.labels;
}
public String getExplicitPrefix() {
return explicitPrefix;
}
public void setExplicitPrefix(String explicitPrefix) {
this.explicitPrefix = explicitPrefix;
}
public Boolean getUseNameAsPrefix() {
return useNameAsPrefix;
}
public void setUseNameAsPrefix(Boolean useNameAsPrefix) {
this.useNameAsPrefix = useNameAsPrefix;
}
public Boolean getIncludeProfileSpecificSources() {
return includeProfileSpecificSources;
}
public void setIncludeProfileSpecificSources(Boolean includeProfileSpecificSources) {
this.includeProfileSpecificSources = includeProfileSpecificSources;
}
public boolean isEmpty() {
return !StringUtils.hasLength(this.name) && !StringUtils.hasLength(this.namespace);
}
/**
* @param name The name of the Secret.
* @param namespace The namespace where the Secret is found.
* @param labels The labels of the Secret to find.
* @param explicitPrefix An explicit prefix to be used for properties.
* @param useNameAsPrefix Use secret name as prefix for properties.
* @param includeProfileSpecificSources Use profile name to append to a config map
* name.
*/
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,

View File

@@ -70,18 +70,14 @@ class SecretsConfigPropertiesTests {
*/
@Test
void multipleSources() {
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
one.setNamespace("spring-k8s");
one.setName("one");
one.setLabels(Collections.singletonMap("one", "1"));
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source("one", "spring-k8s",
Collections.singletonMap("one", "1"), null, null, null);
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
two.setLabels(Collections.singletonMap("two", "2"));
two.setNamespace("spring-k8s");
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source(null, "spring-k8s",
Collections.singletonMap("two", "2"), null, null, null);
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
three.setLabels(Collections.singletonMap("three", "3"));
three.setNamespace("spring-k8s");
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source(null, "spring-k8s",
Collections.singletonMap("three", "3"), null, null, null);
properties.setSources(Arrays.asList(one, two, three));
@@ -188,8 +184,8 @@ class SecretsConfigPropertiesTests {
properties.setUseNameAsPrefix(true);
properties.setNamespace("spring-k8s");
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
one.setName("secret-one");
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source("secret-one", null,
Collections.emptyMap(), null, null, null);
properties.setSources(Collections.singletonList(one));
List<NormalizedSource> sources = properties.determineSources(new MockEnvironment());
@@ -226,16 +222,14 @@ class SecretsConfigPropertiesTests {
properties.setUseNameAsPrefix(true);
properties.setNamespace("spring-k8s");
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
one.setName("secret-one");
one.setUseNameAsPrefix(false);
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source("secret-one", null,
Collections.emptyMap(), null, false, null);
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
two.setName("secret-two");
two.setUseNameAsPrefix(true);
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source("secret-two", null,
Collections.emptyMap(), null, true, null);
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
three.setName("secret-three");
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source("secret-three", null,
Collections.emptyMap(), null, null, null);
properties.setSources(Arrays.asList(one, two, three));
@@ -276,22 +270,17 @@ class SecretsConfigPropertiesTests {
properties.setUseNameAsPrefix(false);
properties.setNamespace("spring-k8s");
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
one.setName("secret-one");
one.setUseNameAsPrefix(false);
one.setExplicitPrefix("one");
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source("secret-one", null,
Collections.emptyMap(), "one", false, null);
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
two.setName("secret-two");
two.setUseNameAsPrefix(true);
two.setExplicitPrefix("two");
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source("secret-two", null,
Collections.emptyMap(), "two", true, null);
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
three.setName("secret-three");
three.setExplicitPrefix("three");
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source("secret-three", null,
Collections.emptyMap(), "three", null, null);
SecretsConfigProperties.Source four = new SecretsConfigProperties.Source();
four.setName("secret-four");
SecretsConfigProperties.Source four = new SecretsConfigProperties.Source("secret-four", null,
Collections.emptyMap(), null, null, null);
properties.setSources(Arrays.asList(one, two, three, four));
@@ -338,28 +327,23 @@ class SecretsConfigPropertiesTests {
*/
@Test
void testLabelsMultipleCases() {
SecretsConfigProperties properties = new SecretsConfigProperties();
properties.setUseNameAsPrefix(false);
properties.setNamespace("spring-k8s");
properties.setIncludeProfileSpecificSources(false);
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
one.setLabels(Map.of("first-label", "secret-one"));
one.setUseNameAsPrefix(false);
one.setExplicitPrefix("one");
SecretsConfigProperties.Source one = new SecretsConfigProperties.Source(null, null,
Map.of("first-label", "secret-one"), "one", false, null);
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
two.setLabels(Map.of("second-label", "secret-two"));
two.setUseNameAsPrefix(true);
two.setExplicitPrefix("two");
two.setIncludeProfileSpecificSources(true);
SecretsConfigProperties.Source two = new SecretsConfigProperties.Source(null, null,
Map.of("second-label", "secret-two"), "two", true, true);
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
three.setLabels(Map.of("third-label", "secret-three"));
three.setExplicitPrefix("three");
SecretsConfigProperties.Source three = new SecretsConfigProperties.Source(null, null,
Map.of("third-label", "secret-three"), "three", null, null);
SecretsConfigProperties.Source four = new SecretsConfigProperties.Source();
four.setLabels(Map.of("fourth-label", "secret-four"));
SecretsConfigProperties.Source four = new SecretsConfigProperties.Source(null, null,
Map.of("fourth-label", "secret-four"), null, null, null);
properties.setSources(Arrays.asList(one, two, three, four));