Skip to content

Commit bb0c35d

Browse files
committed
first draft
Signed-off-by: wind57 <[email protected]>
1 parent 9bd74ed commit bb0c35d

File tree

10 files changed

+73
-41
lines changed

10 files changed

+73
-41
lines changed

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigMapPropertySourceLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public PropertySource<?> locate(Environment environment) {
5757
}
5858

5959
@Override
60-
protected MapPropertySource getMapPropertySource(NormalizedSource source, ConfigurableEnvironment environment,
60+
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment, NormalizedSource source,
6161
ReadType readType) {
6262

6363
String normalizedNamespace = source.namespace().orElse(null);

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/CommonPropertySourceLocator.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collection;
2020
import java.util.HashSet;
2121
import java.util.List;
22+
import java.util.Locale;
2223
import java.util.Set;
2324

2425
import org.apache.commons.logging.LogFactory;
@@ -40,11 +41,11 @@ abstract class CommonPropertySourceLocator implements PropertySourceLocator {
4041

4142
protected final SourceConfigProperties properties;
4243

43-
private final String type;
44+
private final SourceType sourceType;
4445

45-
CommonPropertySourceLocator(SourceConfigProperties properties, String type) {
46+
CommonPropertySourceLocator(SourceConfigProperties properties, SourceType sourceType) {
4647
this.properties = properties;
47-
this.type = type;
48+
this.sourceType = sourceType;
4849
}
4950

5051
protected abstract MapPropertySource getPropertySource(ConfigurableEnvironment environment,
@@ -55,10 +56,11 @@ public PropertySource<?> locate(Environment environment) {
5556

5657
if (environment instanceof ConfigurableEnvironment env) {
5758

58-
List<NormalizedSource> sources = properties.determineSources(false, environment);
59+
List<NormalizedSource> sources = properties.determineSources(sourceType, environment);
5960
Set<NormalizedSource> uniqueSources = new HashSet<>(sources);
60-
LOG.debug(type + " normalized sources : " + uniqueSources);
61-
CompositePropertySource composite = new CompositePropertySource("composite-" + type);
61+
LOG.debug(sourceType.name() + " normalized sources : " + uniqueSources);
62+
CompositePropertySource composite = new CompositePropertySource(
63+
"composite-" + sourceType.name().toLowerCase(Locale.ROOT));
6264

6365
uniqueSources.forEach(secretSource -> {
6466
MapPropertySource propertySource = getPropertySource(env, secretSource, properties.readType());
@@ -67,7 +69,8 @@ public PropertySource<?> locate(Environment environment) {
6769
LOG.warn(() -> "Failed to load source: " + secretSource);
6870
}
6971
else {
70-
LOG.debug("Adding " + type + " property source " + propertySource.getName());
72+
LOG.debug("Adding " + sourceType.name().toLowerCase(Locale.ROOT) + " property source "
73+
+ propertySource.getName());
7174
composite.addFirstPropertySource(propertySource);
7275
}
7376
});

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public abstract class ConfigMapPropertySourceLocator extends CommonPropertySourceLocator {
2727

2828
public ConfigMapPropertySourceLocator(ConfigMapConfigProperties configMapConfigProperties) {
29-
super(configMapConfigProperties, "configmaps");
29+
super(configMapConfigProperties, SourceType.CONFIGMAP);
3030
}
3131

3232
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SecretsPropertySourceLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public abstract class SecretsPropertySourceLocator extends CommonPropertySourceLocator {
3030

3131
public SecretsPropertySourceLocator(SecretsConfigProperties properties) {
32-
super(properties, "secrets");
32+
super(properties, SourceType.SECRET);
3333
}
3434

3535
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceConfigProperties.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,12 @@ public ReadType readType() {
110110
return readType;
111111
}
112112

113-
protected final List<NormalizedSource> determineSources(boolean configMap, Environment environment) {
113+
protected final List<NormalizedSource> determineSources(SourceType sourceType, Environment environment) {
114114
if (sources().isEmpty()) {
115115
List<NormalizedSource> result = new ArrayList<>(2);
116-
String configurationTarget = configMap ? "ConfigMap" : "Secret";
117-
String name = getApplicationName(environment, name(), configurationTarget);
116+
String name = getApplicationName(environment, name(), sourceType.name());
118117
NormalizedSource normalizedSource;
119-
if (configMap) {
118+
if (sourceType == SourceType.CONFIGMAP) {
120119
normalizedSource = new NamedConfigMapNormalizedSource(name, namespace(), failFast(),
121120
includeProfileSpecificSources());
122121
}
@@ -128,7 +127,7 @@ protected final List<NormalizedSource> determineSources(boolean configMap, Envir
128127

129128
if (!labels().isEmpty()) {
130129
NormalizedSource labeledSource;
131-
if (configMap) {
130+
if (sourceType == SourceType.CONFIGMAP) {
132131
labeledSource = new LabeledConfigMapNormalizedSource(namespace(), labels(), failFast(),
133132
ConfigUtils.Prefix.DEFAULT, false);
134133
}
@@ -142,7 +141,7 @@ protected final List<NormalizedSource> determineSources(boolean configMap, Envir
142141
}
143142

144143
return sources().stream()
145-
.flatMap(s -> s.normalize(configMap, name(), namespace(), labels(), includeProfileSpecificSources(),
144+
.flatMap(s -> s.normalize(sourceType, name(), namespace(), labels(), includeProfileSpecificSources(),
146145
failFast(), useNameAsPrefix(), environment))
147146
.toList();
148147
}
@@ -161,7 +160,7 @@ protected final List<NormalizedSource> determineSources(boolean configMap, Envir
161160
public record Source(String name, String namespace, @DefaultValue Map<String, String> labels, String explicitPrefix,
162161
Boolean useNameAsPrefix, Boolean includeProfileSpecificSources) {
163162

164-
Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String defaultNamespace,
163+
Stream<NormalizedSource> normalize(SourceType sourceType, String defaultName, String defaultNamespace,
165164
Map<String, String> defaultLabels, boolean defaultIncludeProfileSpecificSources, boolean failFast,
166165
boolean defaultUseNameAsPrefix, Environment environment) {
167166

@@ -171,16 +170,15 @@ Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String
171170
String normalizedNamespace = hasLength(namespace) ? namespace : defaultNamespace;
172171
Map<String, String> normalizedLabels = labels.isEmpty() ? defaultLabels : labels;
173172

174-
String configurationTarget = configMap ? "ConfigMap" : "Secret";
175-
String sourceName = getApplicationName(environment, normalizedName, configurationTarget);
173+
String sourceName = getApplicationName(environment, normalizedName, sourceType.name());
176174

177175
Prefix prefix = findPrefix(explicitPrefix, useNameAsPrefix, defaultUseNameAsPrefix, normalizedName);
178176

179177
boolean includeProfileSpecificSources = ConfigUtils.includeProfileSpecificSources(
180178
defaultIncludeProfileSpecificSources, this.includeProfileSpecificSources);
181179

182180
NormalizedSource namedSource;
183-
if (configMap) {
181+
if (sourceType == SourceType.CONFIGMAP) {
184182
namedSource = new NamedConfigMapNormalizedSource(sourceName, normalizedNamespace, failFast, prefix,
185183
includeProfileSpecificSources);
186184
}
@@ -192,7 +190,7 @@ Stream<NormalizedSource> normalize(boolean configMap, String defaultName, String
192190

193191
if (!normalizedLabels.isEmpty()) {
194192
NormalizedSource labeledSource;
195-
if (configMap) {
193+
if (sourceType == SourceType.CONFIGMAP) {
196194
labeledSource = new LabeledConfigMapNormalizedSource(normalizedNamespace, labels, failFast, prefix,
197195
includeProfileSpecificSources);
198196
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.commons.config;
18+
19+
public enum SourceType {
20+
21+
/**
22+
* ConfigMap as the source type.
23+
*/
24+
CONFIGMAP,
25+
26+
/**
27+
* Secret as the source type.
28+
*/
29+
SECRET
30+
31+
}

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void testUseNameAsPrefixUnsetEmptySources() {
5151
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), "config-map-a",
5252
"spring-k8s", false, false, false, RetryProperties.DEFAULT, BATCH);
5353

54-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
54+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
5555
Assertions.assertThat(sources.size()).isEqualTo(1);
5656

5757
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).prefix())
@@ -77,7 +77,7 @@ void testUseNameAsPrefixSetEmptySources() {
7777
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), "config-map-a",
7878
"spring-k8s", true, false, false, RetryProperties.DEFAULT, BATCH);
7979

80-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
80+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
8181
Assertions.assertThat(sources.size()).isEqualTo(1);
8282

8383
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).prefix())
@@ -108,7 +108,7 @@ void testUseNameAsPrefixUnsetNonEmptySources() {
108108
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one), Map.of(),
109109
"config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT, BATCH);
110110

111-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
111+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
112112
Assertions.assertThat(sources.size()).isEqualTo(1);
113113

114114
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).prefix().prefixProvider().get())
@@ -151,7 +151,7 @@ void testUseNameAsPrefixSetNonEmptySources() {
151151
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three), Map.of(),
152152
"config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT, BATCH);
153153

154-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
154+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
155155
Assertions.assertThat(sources.size()).isEqualTo(3);
156156

157157
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).prefix())
@@ -201,7 +201,7 @@ void testMultipleCases() {
201201
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three, four),
202202
Map.of(), "config-map-a", "spring-k8s", true, false, false, RetryProperties.DEFAULT, BATCH);
203203

204-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
204+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
205205
Assertions.assertThat(sources.size()).isEqualTo(4);
206206

207207
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).prefix().prefixProvider().get())
@@ -234,7 +234,7 @@ void testUseIncludeProfileSpecificSourcesNoChanges() {
234234
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), "config-map-a",
235235
"spring-k8s", false, true, false, RetryProperties.DEFAULT, BATCH);
236236

237-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
237+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
238238
Assertions.assertThat(sources.size()).isEqualTo(1);
239239

240240
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).profileSpecificSources()).isTrue();
@@ -263,7 +263,7 @@ void testUseIncludeProfileSpecificSourcesDefaultChanged() {
263263
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(), Map.of(), "config-map-a",
264264
"spring-k8s", false, false, false, RetryProperties.DEFAULT, BATCH);
265265

266-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
266+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
267267
Assertions.assertThat(sources.size()).isEqualTo(1);
268268

269269
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).profileSpecificSources()).isFalse();
@@ -307,7 +307,7 @@ void testUseIncludeProfileSpecificSourcesDefaultChangedSourceOverride() {
307307
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three), Map.of(),
308308
"config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT, BATCH);
309309

310-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
310+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
311311
Assertions.assertThat(sources.size()).isEqualTo(3);
312312

313313
Assertions.assertThat(((NamedConfigMapNormalizedSource) sources.get(0)).profileSpecificSources()).isTrue();
@@ -364,7 +364,7 @@ void testLabelsMultipleCases() {
364364
ConfigMapConfigProperties properties = new ConfigMapConfigProperties(true, List.of(one, two, three, four),
365365
Map.of(), "config-map-a", "spring-k8s", false, false, false, RetryProperties.DEFAULT, BATCH);
366366

367-
List<NormalizedSource> sources = properties.determineSources(true, new MockEnvironment());
367+
List<NormalizedSource> sources = properties.determineSources(SourceType.CONFIGMAP, new MockEnvironment());
368368
// we get 8 property sources, since "named" ones with "application" are
369369
// duplicated.
370370
// that's OK, since later in the code we get a LinkedHashSet out of them all,

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void emptySourcesSecretName() {
4141
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(), Map.of(), null, "namespace",
4242
false, true, false, RetryProperties.DEFAULT, ReadType.BATCH);
4343

44-
List<NormalizedSource> source = properties.determineSources(false, new MockEnvironment());
44+
List<NormalizedSource> source = properties.determineSources(SourceType.SECRET, new MockEnvironment());
4545
Assertions.assertThat(source.size()).isEqualTo(1);
4646
Assertions.assertThat(source.get(0) instanceof NamedSecretNormalizedSource).isTrue();
4747
Assertions.assertThat(source.get(0).name().isPresent()).isTrue();
@@ -82,7 +82,7 @@ void multipleSources() {
8282
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(one, two, three), Map.of(), null,
8383
"namespace", false, true, false, RetryProperties.DEFAULT, ReadType.BATCH);
8484

85-
List<NormalizedSource> result = properties.determineSources(false, new MockEnvironment());
85+
List<NormalizedSource> result = properties.determineSources(SourceType.SECRET, new MockEnvironment());
8686
Assertions.assertThat(result.size()).isEqualTo(6);
8787

8888
Set<NormalizedSource> resultAsSet = new LinkedHashSet<>(result);
@@ -124,7 +124,7 @@ void testUseNameAsPrefixUnsetEmptySources() {
124124
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(), Map.of(), "secret-a",
125125
"namespace", false, true, false, RetryProperties.DEFAULT, ReadType.BATCH);
126126

127-
List<NormalizedSource> sources = properties.determineSources(false, new MockEnvironment());
127+
List<NormalizedSource> sources = properties.determineSources(SourceType.SECRET, new MockEnvironment());
128128
Assertions.assertThat(sources.size()).isEqualTo(1);
129129

130130
Assertions.assertThat(((NamedSecretNormalizedSource) sources.get(0)).prefix())
@@ -151,7 +151,7 @@ void testUseNameAsPrefixSetEmptySources() {
151151
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(), Map.of(), "secret-a",
152152
"namespace", true, true, false, RetryProperties.DEFAULT, ReadType.BATCH);
153153

154-
List<NormalizedSource> sources = properties.determineSources(false, new MockEnvironment());
154+
List<NormalizedSource> sources = properties.determineSources(SourceType.SECRET, new MockEnvironment());
155155
Assertions.assertThat(sources.size()).isEqualTo(1);
156156

157157
Assertions.assertThat(((NamedSecretNormalizedSource) sources.get(0)).prefix())
@@ -182,7 +182,7 @@ void testUseNameAsPrefixUnsetNonEmptySources() {
182182
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(one), Map.of(), "secret-one",
183183
null, false, true, false, RetryProperties.DEFAULT, ReadType.BATCH);
184184

185-
List<NormalizedSource> sources = properties.determineSources(false, new MockEnvironment());
185+
List<NormalizedSource> sources = properties.determineSources(SourceType.SECRET, new MockEnvironment());
186186
Assertions.assertThat(sources.size()).isEqualTo(1);
187187

188188
Assertions.assertThat(((NamedSecretNormalizedSource) sources.get(0)).prefix().prefixProvider().get())
@@ -225,7 +225,7 @@ void testUseNameAsPrefixSetNonEmptySources() {
225225
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(one, two, three), Map.of(),
226226
"secret-one", null, false, true, false, RetryProperties.DEFAULT, ReadType.BATCH);
227227

228-
List<NormalizedSource> sources = properties.determineSources(false, new MockEnvironment());
228+
List<NormalizedSource> sources = properties.determineSources(SourceType.SECRET, new MockEnvironment());
229229
Assertions.assertThat(sources.size()).isEqualTo(3);
230230

231231
Assertions.assertThat(((NamedSecretNormalizedSource) sources.get(0)).prefix())
@@ -275,7 +275,7 @@ void testMultipleCases() {
275275
SecretsConfigProperties properties = new SecretsConfigProperties(true, List.of(one, two, three, four), Map.of(),
276276
"secret-one", "spring-k8s", false, false, false, RetryProperties.DEFAULT, ReadType.BATCH);
277277

278-
List<NormalizedSource> sources = properties.determineSources(false, new MockEnvironment());
278+
List<NormalizedSource> sources = properties.determineSources(SourceType.SECRET, new MockEnvironment());
279279
Assertions.assertThat(sources.size()).isEqualTo(4);
280280

281281
Assertions.assertThat(((NamedSecretNormalizedSource) sources.get(0)).prefix().prefixProvider().get())
@@ -336,7 +336,7 @@ void testLabelsMultipleCases() {
336336
SecretsConfigProperties properties = new SecretsConfigProperties(false, List.of(one, two, three, four),
337337
Map.of(), null, "spring-k8s", false, false, false, RetryProperties.DEFAULT, ReadType.BATCH);
338338

339-
List<NormalizedSource> sources = properties.determineSources(false, new MockEnvironment());
339+
List<NormalizedSource> sources = properties.determineSources(SourceType.SECRET, new MockEnvironment());
340340
// we get 8 property sources, since "named" ones with "application" are
341341
// duplicated.
342342
// that's OK, since later in the code we get a LinkedHashSet out of them all,

spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public PropertySource<?> locate(Environment environment) {
6262
}
6363

6464
@Override
65-
protected MapPropertySource getMapPropertySource(NormalizedSource normalizedSource,
66-
ConfigurableEnvironment environment, ReadType readType) {
65+
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment,
66+
NormalizedSource normalizedSource, ReadType readType) {
6767
// NormalizedSource has a namespace, but users can skip it.
6868
// In such cases we try to get it elsewhere
6969
String namespace = getApplicationNamespace(this.client, normalizedSource.namespace().orElse(null),

spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigMapPropertySourceLocatorMockTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void constructorWithoutClientNamespaceMustFail() {
5454
Fabric8ConfigMapPropertySourceLocator source = new Fabric8ConfigMapPropertySourceLocator(client,
5555
configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
5656
NormalizedSource normalizedSource = new NamedConfigMapNormalizedSource("name", null, false, PREFIX, false);
57-
assertThatThrownBy(() -> source.getMapPropertySource(normalizedSource, new MockEnvironment(), ReadType.BATCH))
57+
assertThatThrownBy(() -> source.getPropertySource(new MockEnvironment(), normalizedSource, ReadType.BATCH))
5858
.isInstanceOf(NamespaceResolutionFailedException.class);
5959
}
6060

0 commit comments

Comments
 (0)