Skip to content

Commit 173ee09

Browse files
authored
Merge pull request #2048 from wind57/common_code_for_locate
common code for locate methods
2 parents b393952 + ad63517 commit 173ee09

File tree

12 files changed

+160
-142
lines changed

12 files changed

+160
-142
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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
import java.util.Collection;
20+
import java.util.LinkedHashSet;
21+
import java.util.List;
22+
import java.util.Locale;
23+
import java.util.Set;
24+
25+
import org.apache.commons.logging.LogFactory;
26+
27+
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
28+
import org.springframework.core.env.CompositePropertySource;
29+
import org.springframework.core.env.ConfigurableEnvironment;
30+
import org.springframework.core.env.Environment;
31+
import org.springframework.core.env.MapPropertySource;
32+
import org.springframework.core.env.PropertySource;
33+
import org.springframework.core.log.LogAccessor;
34+
35+
/**
36+
* @author wind57
37+
*/
38+
abstract class CommonPropertySourceLocator implements PropertySourceLocator {
39+
40+
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(CommonPropertySourceLocator.class));
41+
42+
protected final SourceConfigProperties properties;
43+
44+
private final SourceType sourceType;
45+
46+
CommonPropertySourceLocator(SourceConfigProperties properties, SourceType sourceType) {
47+
this.properties = properties;
48+
this.sourceType = sourceType;
49+
}
50+
51+
protected abstract MapPropertySource getPropertySource(ConfigurableEnvironment environment,
52+
NormalizedSource normalizedSource, ReadType readType);
53+
54+
@Override
55+
public PropertySource<?> locate(Environment environment) {
56+
57+
if (environment instanceof ConfigurableEnvironment env) {
58+
59+
List<NormalizedSource> sources = properties.determineSources(sourceType, environment);
60+
Set<NormalizedSource> uniqueSources = new LinkedHashSet<>(sources);
61+
LOG.debug(sourceType.name() + " normalized sources : " + uniqueSources);
62+
CompositePropertySource composite = new CompositePropertySource(
63+
"composite-" + sourceType.name().toLowerCase(Locale.ROOT));
64+
65+
uniqueSources.forEach(secretSource -> {
66+
MapPropertySource propertySource = getPropertySource(env, secretSource, properties.readType());
67+
68+
if ("true".equals(propertySource.getProperty(Constants.ERROR_PROPERTY))) {
69+
LOG.warn(() -> "Failed to load source: " + secretSource);
70+
}
71+
else {
72+
LOG.debug("Adding " + sourceType.name().toLowerCase(Locale.ROOT) + " property source "
73+
+ propertySource.getName());
74+
composite.addFirstPropertySource(propertySource);
75+
}
76+
});
77+
78+
return composite;
79+
}
80+
return null;
81+
}
82+
83+
@Override
84+
public Collection<PropertySource<?>> locateCollection(Environment environment) {
85+
return PropertySourceLocator.super.locateCollection(environment);
86+
}
87+
88+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public ConfigDataRetryableConfigMapPropertySourceLocator(
4747
}
4848

4949
@Override
50-
protected MapPropertySource getMapPropertySource(NormalizedSource normalizedSource,
51-
ConfigurableEnvironment environment, ReadType readType) {
52-
return configMapPropertySourceLocator.getMapPropertySource(normalizedSource, environment, readType);
50+
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment,
51+
NormalizedSource normalizedSource, ReadType readType) {
52+
return configMapPropertySourceLocator.getPropertySource(environment, normalizedSource, readType);
5353
}
5454

5555
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.core.env.ConfigurableEnvironment;
2222
import org.springframework.core.env.Environment;
23+
import org.springframework.core.env.MapPropertySource;
2324
import org.springframework.core.env.PropertySource;
2425
import org.springframework.retry.support.RetryTemplate;
2526

@@ -56,7 +57,7 @@ public Collection<PropertySource<?>> locateCollection(Environment environment) {
5657
}
5758

5859
@Override
59-
protected SecretsPropertySource getPropertySource(ConfigurableEnvironment environment,
60+
protected MapPropertySource getPropertySource(ConfigurableEnvironment environment,
6061
NormalizedSource normalizedSource, ReadType readType) {
6162
return this.secretsPropertySourceLocator.getPropertySource(environment, normalizedSource, readType);
6263
}

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

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,17 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19-
import java.util.Collection;
20-
import java.util.LinkedHashSet;
21-
import java.util.Set;
22-
23-
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
25-
26-
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
27-
import org.springframework.core.env.CompositePropertySource;
28-
import org.springframework.core.env.ConfigurableEnvironment;
29-
import org.springframework.core.env.Environment;
30-
import org.springframework.core.env.MapPropertySource;
31-
import org.springframework.core.env.PropertySource;
32-
3319
/**
34-
* A {@link PropertySourceLocator} that uses config maps.
20+
* A PropertySourceLocator that uses config maps.
3521
*
3622
* @author Ioannis Canellos
3723
* @author Michael Moudatsos
3824
* @author Isik Erhan
3925
*/
40-
public abstract class ConfigMapPropertySourceLocator implements PropertySourceLocator {
41-
42-
private static final Log LOG = LogFactory.getLog(ConfigMapPropertySourceLocator.class);
43-
44-
protected final ConfigMapConfigProperties properties;
45-
46-
public ConfigMapPropertySourceLocator(ConfigMapConfigProperties properties) {
47-
this.properties = properties;
48-
}
49-
50-
protected abstract MapPropertySource getMapPropertySource(NormalizedSource normalizedSource,
51-
ConfigurableEnvironment environment, ReadType readType);
52-
53-
@Override
54-
public PropertySource<?> locate(Environment environment) {
55-
if (environment instanceof ConfigurableEnvironment env) {
56-
57-
CompositePropertySource composite = new CompositePropertySource("composite-configmap");
58-
Set<NormalizedSource> sources = new LinkedHashSet<>(this.properties.determineSources(true, environment));
59-
LOG.debug("Config Map normalized sources : " + sources);
60-
sources.forEach(configMapSource -> {
61-
MapPropertySource propertySource = getMapPropertySource(configMapSource, env, properties.readType());
62-
if ("true".equals(propertySource.getProperty(Constants.ERROR_PROPERTY))) {
63-
LOG.warn("Failed to load source: " + configMapSource);
64-
}
65-
else {
66-
LOG.debug("Adding config map property source " + propertySource.getName());
67-
composite.addFirstPropertySource(propertySource);
68-
}
69-
});
70-
71-
return composite;
72-
}
73-
return null;
74-
}
26+
public abstract class ConfigMapPropertySourceLocator extends CommonPropertySourceLocator {
7527

76-
@Override
77-
public Collection<PropertySource<?>> locateCollection(Environment environment) {
78-
return PropertySourceLocator.super.locateCollection(environment);
28+
public ConfigMapPropertySourceLocator(ConfigMapConfigProperties configMapConfigProperties) {
29+
super(configMapConfigProperties, SourceType.CONFIGMAP);
7930
}
8031

8132
}

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

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,7 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19-
import java.util.Collection;
20-
import java.util.HashSet;
21-
import java.util.List;
22-
import java.util.Set;
23-
24-
import org.apache.commons.logging.Log;
25-
import org.apache.commons.logging.LogFactory;
26-
2719
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
28-
import org.springframework.core.env.CompositePropertySource;
29-
import org.springframework.core.env.ConfigurableEnvironment;
30-
import org.springframework.core.env.Environment;
31-
import org.springframework.core.env.MapPropertySource;
32-
import org.springframework.core.env.PropertySource;
3320

3421
/**
3522
* Kubernetes {@link PropertySourceLocator} for secrets.
@@ -39,48 +26,10 @@
3926
* @author wind57
4027
* @author Isik Erhan
4128
*/
42-
public abstract class SecretsPropertySourceLocator implements PropertySourceLocator {
43-
44-
private static final Log LOG = LogFactory.getLog(SecretsPropertySourceLocator.class);
45-
46-
protected final SecretsConfigProperties properties;
29+
public abstract class SecretsPropertySourceLocator extends CommonPropertySourceLocator {
4730

4831
public SecretsPropertySourceLocator(SecretsConfigProperties properties) {
49-
this.properties = properties;
50-
}
51-
52-
protected abstract SecretsPropertySource getPropertySource(ConfigurableEnvironment environment,
53-
NormalizedSource normalizedSource, ReadType readType);
54-
55-
@Override
56-
public PropertySource<?> locate(Environment environment) {
57-
if (environment instanceof ConfigurableEnvironment env) {
58-
59-
List<NormalizedSource> sources = this.properties.determineSources(false, environment);
60-
Set<NormalizedSource> uniqueSources = new HashSet<>(sources);
61-
LOG.debug("Secrets normalized sources : " + sources);
62-
CompositePropertySource composite = new CompositePropertySource("composite-secrets");
63-
64-
uniqueSources.forEach(secretSource -> {
65-
MapPropertySource propertySource = getPropertySource(env, secretSource, properties.readType());
66-
67-
if ("true".equals(propertySource.getProperty(Constants.ERROR_PROPERTY))) {
68-
LOG.warn("Failed to load source: " + secretSource);
69-
}
70-
else {
71-
LOG.debug("Adding secret property source " + propertySource.getName());
72-
composite.addFirstPropertySource(propertySource);
73-
}
74-
});
75-
76-
return composite;
77-
}
78-
return null;
79-
}
80-
81-
@Override
82-
public Collection<PropertySource<?>> locateCollection(Environment environment) {
83-
return PropertySourceLocator.super.locateCollection(environment);
32+
super(properties, SourceType.SECRET);
8433
}
8534

8635
}

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+
}

0 commit comments

Comments
 (0)