Skip to content

Commit e1c534c

Browse files
committed
base master
2 parents 9662a1e + 7f6ddf6 commit e1c534c

File tree

124 files changed

+1828
-1853
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+1828
-1853
lines changed

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ You can specify multiple (exact) file paths in `spring.cloud.kubernetes.config.p
437437

438438
NOTE: You have to provide the full exact path to each property file, because directories are not being recursively parsed.
439439

440+
NOTE: If you use `spring.cloud.kubernetes.config.paths` or `spring.cloud.kubernetes.secrets.path` the automatic reload
441+
functionality will not work. You will need to make a `POST` request to the `/actuator/refresh` endpoint or
442+
restart/redeploy the application.
443+
440444
.Properties:
441445
[options="header,footer"]
442446
|===

docs/src/main/asciidoc/property-source-config.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ You can specify multiple (exact) file paths in `spring.cloud.kubernetes.config.p
279279

280280
NOTE: You have to provide the full exact path to each property file, because directories are not being recursively parsed.
281281

282+
NOTE: If you use `spring.cloud.kubernetes.config.paths` or `spring.cloud.kubernetes.secrets.path` the automatic reload
283+
functionality will not work. You will need to make a `POST` request to the `/actuator/refresh` endpoint or
284+
restart/redeploy the application.
285+
282286
.Properties:
283287
[options="header,footer"]
284288
|===

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,21 @@ public class BootstrapConfiguration {
4141

4242
@Configuration(proxyBeanMethods = false)
4343
@Import(KubernetesAutoConfiguration.class)
44-
@EnableConfigurationProperties({ ConfigMapConfigProperties.class,
45-
SecretsConfigProperties.class })
44+
@EnableConfigurationProperties({ ConfigMapConfigProperties.class, SecretsConfigProperties.class })
4645
protected static class KubernetesPropertySourceConfiguration {
4746

4847
@Autowired
4948
private KubernetesClient client;
5049

5150
@Bean
52-
@ConditionalOnProperty(name = "spring.cloud.kubernetes.config.enabled",
53-
matchIfMissing = true)
54-
public ConfigMapPropertySourceLocator configMapPropertySourceLocator(
55-
ConfigMapConfigProperties properties) {
51+
@ConditionalOnProperty(name = "spring.cloud.kubernetes.config.enabled", matchIfMissing = true)
52+
public ConfigMapPropertySourceLocator configMapPropertySourceLocator(ConfigMapConfigProperties properties) {
5653
return new ConfigMapPropertySourceLocator(this.client, properties);
5754
}
5855

5956
@Bean
60-
@ConditionalOnProperty(name = "spring.cloud.kubernetes.secrets.enabled",
61-
matchIfMissing = true)
62-
public SecretsPropertySourceLocator secretsPropertySourceLocator(
63-
SecretsConfigProperties properties) {
57+
@ConditionalOnProperty(name = "spring.cloud.kubernetes.secrets.enabled", matchIfMissing = true)
58+
public SecretsPropertySourceLocator secretsPropertySourceLocator(SecretsConfigProperties properties) {
6459
return new SecretsPropertySourceLocator(this.client, properties);
6560
}
6661

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ public List<NormalizedSource> determineSources() {
8282
};
8383
}
8484

85-
return this.sources.stream().map(s -> s.normalize(this.name, this.namespace))
86-
.collect(Collectors.toList());
85+
return this.sources.stream().map(s -> s.normalize(this.name, this.namespace)).collect(Collectors.toList());
8786
}
8887

8988
@Override
@@ -135,10 +134,8 @@ public boolean isEmpty() {
135134
}
136135

137136
public NormalizedSource normalize(String defaultName, String defaultNamespace) {
138-
final String normalizedName = StringUtils.isEmpty(this.name) ? defaultName
139-
: this.name;
140-
final String normalizedNamespace = StringUtils.isEmpty(this.namespace)
141-
? defaultNamespace : this.namespace;
137+
final String normalizedName = StringUtils.isEmpty(this.name) ? defaultName : this.name;
138+
final String normalizedNamespace = StringUtils.isEmpty(this.namespace) ? defaultNamespace : this.namespace;
142139

143140
return new NormalizedSource(normalizedName, normalizedNamespace);
144141
}

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

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,31 @@ public ConfigMapPropertySource(KubernetesClient client, String name) {
6060
this(client, name, null, (Environment) null);
6161
}
6262

63-
public ConfigMapPropertySource(KubernetesClient client, String name, String namespace,
64-
String[] profiles) {
63+
public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, String[] profiles) {
6564
this(client, name, namespace, createEnvironmentWithActiveProfiles(profiles));
6665
}
6766

68-
private static Environment createEnvironmentWithActiveProfiles(
69-
String[] activeProfiles) {
67+
private static Environment createEnvironmentWithActiveProfiles(String[] activeProfiles) {
7068
StandardEnvironment environment = new StandardEnvironment();
7169
environment.setActiveProfiles(activeProfiles);
7270
return environment;
7371
}
7472

75-
public ConfigMapPropertySource(KubernetesClient client, String name, String namespace,
76-
Environment environment) {
77-
super(getName(client, name, namespace),
78-
asObjectMap(getData(client, name, namespace, environment)));
73+
public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, Environment environment) {
74+
super(getName(client, name, namespace), asObjectMap(getData(client, name, namespace, environment)));
7975
}
8076

81-
private static String getName(KubernetesClient client, String name,
82-
String namespace) {
83-
return new StringBuilder().append(PREFIX)
84-
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(name)
77+
private static String getName(KubernetesClient client, String name, String namespace) {
78+
return new StringBuilder().append(PREFIX).append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(name)
8579
.append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR)
86-
.append(namespace == null || namespace.isEmpty() ? client.getNamespace()
87-
: namespace)
88-
.toString();
80+
.append(namespace == null || namespace.isEmpty() ? client.getNamespace() : namespace).toString();
8981
}
9082

91-
private static Map<String, Object> getData(KubernetesClient client, String name,
92-
String namespace, Environment environment) {
83+
private static Map<String, Object> getData(KubernetesClient client, String name, String namespace,
84+
Environment environment) {
9385
try {
9486
Map<String, Object> result = new LinkedHashMap<>();
95-
ConfigMap map = StringUtils.isEmpty(namespace)
96-
? client.configMaps().withName(name).get()
87+
ConfigMap map = StringUtils.isEmpty(namespace) ? client.configMaps().withName(name).get()
9788
: client.configMaps().inNamespace(namespace).withName(name).get();
9889

9990
if (map != null) {
@@ -107,12 +98,10 @@ private static Map<String, Object> getData(KubernetesClient client, String name,
10798

10899
ConfigMap mapWithProfile = StringUtils.isEmpty(namespace)
109100
? client.configMaps().withName(mapNameWithProfile).get()
110-
: client.configMaps().inNamespace(namespace)
111-
.withName(mapNameWithProfile).get();
101+
: client.configMaps().inNamespace(namespace).withName(mapNameWithProfile).get();
112102

113103
if (mapWithProfile != null) {
114-
result.putAll(
115-
processAllEntries(mapWithProfile.getData(), environment));
104+
result.putAll(processAllEntries(mapWithProfile.getData(), environment));
116105
}
117106

118107
}
@@ -122,15 +111,13 @@ private static Map<String, Object> getData(KubernetesClient client, String name,
122111

123112
}
124113
catch (Exception e) {
125-
LOG.warn("Can't read configMap with name: [" + name + "] in namespace:["
126-
+ namespace + "]. Ignoring.", e);
114+
LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring.", e);
127115
}
128116

129117
return new LinkedHashMap<>();
130118
}
131119

132-
private static Map<String, Object> processAllEntries(Map<String, String> input,
133-
Environment environment) {
120+
private static Map<String, Object> processAllEntries(Map<String, String> input, Environment environment) {
134121

135122
Set<Entry<String, String>> entrySet = input.entrySet();
136123
if (entrySet.size() == 1) {
@@ -141,21 +128,18 @@ private static Map<String, Object> processAllEntries(Map<String, String> input,
141128
String propertyValue = singleEntry.getValue();
142129
if (propertyName.endsWith(".yml") || propertyName.endsWith(".yaml")) {
143130
if (LOG.isDebugEnabled()) {
144-
LOG.debug("The single property with name: [" + propertyName
145-
+ "] will be treated as a yaml file");
131+
LOG.debug("The single property with name: [" + propertyName + "] will be treated as a yaml file");
146132
}
147133

148-
return yamlParserGenerator(environment).andThen(PROPERTIES_TO_MAP)
149-
.apply(propertyValue);
134+
return yamlParserGenerator(environment).andThen(PROPERTIES_TO_MAP).apply(propertyValue);
150135
}
151136
else if (propertyName.endsWith(".properties")) {
152137
if (LOG.isDebugEnabled()) {
153138
LOG.debug("The single property with name: [" + propertyName
154139
+ "] will be treated as a properties file");
155140
}
156141

157-
return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP)
158-
.apply(propertyValue);
142+
return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(propertyValue);
159143
}
160144
else {
161145
return defaultProcessAllEntries(input, environment);
@@ -165,23 +149,17 @@ else if (propertyName.endsWith(".properties")) {
165149
return defaultProcessAllEntries(input, environment);
166150
}
167151

168-
private static Map<String, Object> defaultProcessAllEntries(Map<String, String> input,
169-
Environment environment) {
152+
private static Map<String, Object> defaultProcessAllEntries(Map<String, String> input, Environment environment) {
170153

171-
return input.entrySet().stream()
172-
.map(e -> extractProperties(e.getKey(), e.getValue(), environment))
154+
return input.entrySet().stream().map(e -> extractProperties(e.getKey(), e.getValue(), environment))
173155
.filter(m -> !m.isEmpty()).flatMap(m -> m.entrySet().stream())
174-
.collect(Collectors.toMap(Entry::getKey, Entry::getValue,
175-
throwingMerger(), LinkedHashMap::new));
156+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, throwingMerger(), LinkedHashMap::new));
176157
}
177158

178-
private static Map<String, Object> extractProperties(String resourceName,
179-
String content, Environment environment) {
159+
private static Map<String, Object> extractProperties(String resourceName, String content, Environment environment) {
180160

181-
if (resourceName.equals(APPLICATION_YAML)
182-
|| resourceName.equals(APPLICATION_YML)) {
183-
return yamlParserGenerator(environment).andThen(PROPERTIES_TO_MAP)
184-
.apply(content);
161+
if (resourceName.equals(APPLICATION_YAML) || resourceName.equals(APPLICATION_YML)) {
162+
return yamlParserGenerator(environment).andThen(PROPERTIES_TO_MAP).apply(content);
185163
}
186164
else if (resourceName.equals(APPLICATION_PROPERTIES)) {
187165
return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(content);
@@ -195,8 +173,8 @@ else if (resourceName.equals(APPLICATION_PROPERTIES)) {
195173
}
196174

197175
private static Map<String, Object> asObjectMap(Map<String, Object> source) {
198-
return source.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
199-
Map.Entry::getValue, throwingMerger(), LinkedHashMap::new));
176+
return source.entrySet().stream().collect(
177+
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, throwingMerger(), LinkedHashMap::new));
200178
}
201179

202180
}

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

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@
5252
@Order(0)
5353
public class ConfigMapPropertySourceLocator implements PropertySourceLocator {
5454

55-
private static final Log LOG = LogFactory
56-
.getLog(ConfigMapPropertySourceLocator.class);
55+
private static final Log LOG = LogFactory.getLog(ConfigMapPropertySourceLocator.class);
5756

5857
private final KubernetesClient client;
5958

6059
private final ConfigMapConfigProperties properties;
6160

62-
public ConfigMapPropertySourceLocator(KubernetesClient client,
63-
ConfigMapConfigProperties properties) {
61+
public ConfigMapPropertySourceLocator(KubernetesClient client, ConfigMapConfigProperties properties) {
6462
this.client = client;
6563
this.properties = properties;
6664
}
@@ -70,13 +68,10 @@ public PropertySource locate(Environment environment) {
7068
if (environment instanceof ConfigurableEnvironment) {
7169
ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
7270

73-
List<ConfigMapConfigProperties.NormalizedSource> sources = this.properties
74-
.determineSources();
75-
CompositePropertySource composite = new CompositePropertySource(
76-
"composite-configmap");
71+
List<ConfigMapConfigProperties.NormalizedSource> sources = this.properties.determineSources();
72+
CompositePropertySource composite = new CompositePropertySource("composite-configmap");
7773
if (this.properties.isEnableApi()) {
78-
sources.forEach(s -> composite.addFirstPropertySource(
79-
getMapPropertySourceForSingleConfigMap(env, s)));
74+
sources.forEach(s -> composite.addFirstPropertySource(getMapPropertySourceForSingleConfigMap(env, s)));
8075
}
8176

8277
addPropertySourcesFromPaths(environment, composite);
@@ -86,44 +81,36 @@ public PropertySource locate(Environment environment) {
8681
return null;
8782
}
8883

89-
private MapPropertySource getMapPropertySourceForSingleConfigMap(
90-
ConfigurableEnvironment environment, NormalizedSource normalizedSource) {
84+
private MapPropertySource getMapPropertySourceForSingleConfigMap(ConfigurableEnvironment environment,
85+
NormalizedSource normalizedSource) {
9186

9287
String configurationTarget = this.properties.getConfigurationTarget();
9388
return new ConfigMapPropertySource(this.client,
94-
getApplicationName(environment, normalizedSource.getName(),
95-
configurationTarget),
96-
getApplicationNamespace(this.client, normalizedSource.getNamespace(),
97-
configurationTarget),
89+
getApplicationName(environment, normalizedSource.getName(), configurationTarget),
90+
getApplicationNamespace(this.client, normalizedSource.getNamespace(), configurationTarget),
9891
environment);
9992
}
10093

101-
private void addPropertySourcesFromPaths(Environment environment,
102-
CompositePropertySource composite) {
94+
private void addPropertySourcesFromPaths(Environment environment, CompositePropertySource composite) {
10395
this.properties.getPaths().stream().map(Paths::get).peek(p -> {
10496
if (!Files.exists(p)) {
10597
LOG.warn("Configured input path: " + p
10698
+ " will be ignored because it does not exist on the file system");
10799
}
108100
}).filter(Files::exists).peek(p -> {
109101
if (!Files.isRegularFile(p)) {
110-
LOG.warn("Configured input path: " + p
111-
+ " will be ignored because it is not a regular file");
102+
LOG.warn("Configured input path: " + p + " will be ignored because it is not a regular file");
112103
}
113104
}).filter(Files::isRegularFile).forEach(p -> {
114105
try {
115106
String content = new String(Files.readAllBytes(p)).trim();
116107
String filename = p.getFileName().toString().toLowerCase();
117108
if (filename.endsWith(".properties")) {
118-
addPropertySourceIfNeeded(
119-
c -> PROPERTIES_TO_MAP
120-
.apply(KEY_VALUE_TO_PROPERTIES.apply(c)),
121-
content, filename, composite);
109+
addPropertySourceIfNeeded(c -> PROPERTIES_TO_MAP.apply(KEY_VALUE_TO_PROPERTIES.apply(c)), content,
110+
filename, composite);
122111
}
123112
else if (filename.endsWith(".yml") || filename.endsWith(".yaml")) {
124-
addPropertySourceIfNeeded(
125-
c -> PROPERTIES_TO_MAP
126-
.apply(yamlParserGenerator(environment).apply(c)),
113+
addPropertySourceIfNeeded(c -> PROPERTIES_TO_MAP.apply(yamlParserGenerator(environment).apply(c)),
127114
content, filename, composite);
128115
}
129116
}
@@ -133,15 +120,13 @@ else if (filename.endsWith(".yml") || filename.endsWith(".yaml")) {
133120
});
134121
}
135122

136-
private void addPropertySourceIfNeeded(
137-
Function<String, Map<String, Object>> contentToMapFunction, String content,
123+
private void addPropertySourceIfNeeded(Function<String, Map<String, Object>> contentToMapFunction, String content,
138124
String name, CompositePropertySource composite) {
139125

140126
Map<String, Object> map = new HashMap<>();
141127
map.putAll(contentToMapFunction.apply(content));
142128
if (map.isEmpty()) {
143-
LOG.warn("Property source: " + name
144-
+ "will be ignored because no properties could be found");
129+
LOG.warn("Property source: " + name + "will be ignored because no properties could be found");
145130
}
146131
else {
147132
composite.addFirstPropertySource(new MapPropertySource(name, map));

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ private ConfigUtils() {
3939
throw new IllegalStateException("Can't instantiate a utility class");
4040
}
4141

42-
public static <C extends AbstractConfigProperties> String getApplicationName(
43-
Environment env, String configName, String configurationTarget) {
42+
public static <C extends AbstractConfigProperties> String getApplicationName(Environment env, String configName,
43+
String configurationTarget) {
4444
String name = configName;
4545
if (StringUtils.isEmpty(name)) {
4646
// TODO: use relaxed binding
4747
if (LOG.isDebugEnabled()) {
48-
LOG.debug(configurationTarget
49-
+ " name has not been set, taking it from property/env "
50-
+ SPRING_APPLICATION_NAME + " (default="
51-
+ FALLBACK_APPLICATION_NAME + ")");
48+
LOG.debug(configurationTarget + " name has not been set, taking it from property/env "
49+
+ SPRING_APPLICATION_NAME + " (default=" + FALLBACK_APPLICATION_NAME + ")");
5250
}
5351

5452
name = env.getProperty(SPRING_APPLICATION_NAME, FALLBACK_APPLICATION_NAME);
@@ -57,13 +55,12 @@ public static <C extends AbstractConfigProperties> String getApplicationName(
5755
return name;
5856
}
5957

60-
public static <C extends AbstractConfigProperties> String getApplicationNamespace(
61-
KubernetesClient client, String configNamespace, String configurationTarget) {
58+
public static <C extends AbstractConfigProperties> String getApplicationNamespace(KubernetesClient client,
59+
String configNamespace, String configurationTarget) {
6260
String namespace = configNamespace;
6361
if (StringUtils.isEmpty(namespace)) {
6462
if (LOG.isDebugEnabled()) {
65-
LOG.debug(configurationTarget
66-
+ " namespace has not been set, taking it from client (ns="
63+
LOG.debug(configurationTarget + " namespace has not been set, taking it from client (ns="
6764
+ client.getNamespace() + ")");
6865
}
6966

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public final class PropertySourceUtils {
5252
throw new IllegalArgumentException();
5353
}
5454
};
55-
static final Function<Properties, Map<String, Object>> PROPERTIES_TO_MAP = p -> p
56-
.entrySet().stream().collect(Collectors.toMap(e -> String.valueOf(e.getKey()),
57-
Map.Entry::getValue, throwingMerger(), java.util.LinkedHashMap::new));
55+
static final Function<Properties, Map<String, Object>> PROPERTIES_TO_MAP = p -> p.entrySet().stream()
56+
.collect(Collectors.toMap(e -> String.valueOf(e.getKey()), Map.Entry::getValue, throwingMerger(),
57+
java.util.LinkedHashMap::new));
5858

5959
private PropertySourceUtils() {
6060
throw new IllegalStateException("Can't instantiate a utility class");
@@ -66,8 +66,7 @@ static Function<String, Properties> yamlParserGenerator(Environment environment)
6666
yamlFactory.setDocumentMatchers(properties -> {
6767
String profiles = properties.getProperty("spring.profiles");
6868
if (environment != null && StringUtils.hasText(profiles)) {
69-
return environment.acceptsProfiles(Profiles.of(profiles)) ? FOUND
70-
: NOT_FOUND;
69+
return environment.acceptsProfiles(Profiles.of(profiles)) ? FOUND : NOT_FOUND;
7170
}
7271
else {
7372
return ABSTAIN;

0 commit comments

Comments
 (0)