Skip to content

Commit 816f081

Browse files
committed
clean
Signed-off-by: wind57 <[email protected]>
1 parent 59ae9cf commit 816f081

File tree

10 files changed

+169
-287
lines changed

10 files changed

+169
-287
lines changed

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

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.Base64;
2222
import java.util.HashMap;
23+
import java.util.LinkedHashMap;
2324
import java.util.LinkedHashSet;
2425
import java.util.List;
2526
import java.util.Map;
@@ -57,9 +58,9 @@ public final class ConfigUtils {
5758

5859
// sourceName (configmap or secret name) ends with : "-dev.yaml" or the like.
5960
private static final BiPredicate<String, String> ENDS_WITH_PROFILE_AND_EXTENSION = (sourceName,
60-
activeProfile) -> sourceName.endsWith("-" + activeProfile + ".yml")
61-
|| sourceName.endsWith("-" + activeProfile + ".yaml")
62-
|| sourceName.endsWith("-" + activeProfile + ".properties");
61+
activeProfile) -> sourceName.endsWith("-" + activeProfile + ".yml")
62+
|| sourceName.endsWith("-" + activeProfile + ".yaml")
63+
|| sourceName.endsWith("-" + activeProfile + ".properties");
6364

6465
private static final ApplicationListener<?> NO_OP = (e) -> {
6566
};
@@ -70,7 +71,7 @@ private ConfigUtils() {
7071
public static String getApplicationName(Environment env, String configName, String configurationTarget) {
7172
if (!StringUtils.hasLength(configName)) {
7273
LOG.debug(configurationTarget + " name has not been set, taking it from property/env "
73-
+ SPRING_APPLICATION_NAME + " (default=" + FALLBACK_APPLICATION_NAME + ")");
74+
+ SPRING_APPLICATION_NAME + " (default=" + FALLBACK_APPLICATION_NAME + ")");
7475
configName = env.getProperty(SPRING_APPLICATION_NAME, FALLBACK_APPLICATION_NAME);
7576
}
7677

@@ -90,7 +91,7 @@ public static String getApplicationName(Environment env, String configName, Stri
9091
* @return prefix to use in normalized sources
9192
*/
9293
public static Prefix findPrefix(String explicitPrefix, Boolean useNameAsPrefix, boolean defaultUseNameAsPrefix,
93-
String normalizedName) {
94+
String normalizedName) {
9495
// if explicitPrefix is set, it takes priority over useNameAsPrefix
9596
// (either the one from 'spring.cloud.kubernetes.config|secrets' or
9697
// 'spring.cloud.kubernetes.config|secrets.sources')
@@ -140,7 +141,7 @@ public static Prefix findPrefix(String explicitPrefix, Boolean useNameAsPrefix,
140141
* @return useProfileNameAsPrefix to be used in normalized sources
141142
*/
142143
public static boolean includeProfileSpecificSources(boolean defaultIncludeProfileSpecificSources,
143-
Boolean includeProfileSpecificSources) {
144+
Boolean includeProfileSpecificSources) {
144145
if (includeProfileSpecificSources != null) {
145146
return includeProfileSpecificSources;
146147
}
@@ -157,21 +158,6 @@ public static void onException(boolean failFast, Exception e) {
157158
LOG.warn(e.getMessage() + ". Ignoring.", e);
158159
}
159160

160-
/*
161-
* This method will return a SourceData that has a name in the form :
162-
* "configmap.my-configmap.my-configmap-2.namespace" and the "data" from the context
163-
* is appended with prefix. So if incoming is "a=b", the result will be : "prefix.a=b"
164-
*/
165-
@Deprecated(forRemoval = true)
166-
public static SourceData withPrefix(String target, PrefixContext context) {
167-
Map<String, Object> withPrefix = CollectionUtils.newHashMap(context.data().size());
168-
context.data().forEach((key, value) -> withPrefix.put(context.prefix() + "." + key, value));
169-
170-
String propertySourceTokens = String.join(PROPERTY_SOURCE_NAME_SEPARATOR,
171-
context.propertySourceNames().stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)));
172-
return new SourceData(sourceName(target, propertySourceTokens, context.namespace()), withPrefix);
173-
}
174-
175161
public static String sourceName(String target, String applicationName, String namespace) {
176162
return target + PROPERTY_SOURCE_NAME_SEPARATOR + applicationName + PROPERTY_SOURCE_NAME_SEPARATOR + namespace;
177163
}
@@ -185,7 +171,7 @@ public static String sourceName(String target, String applicationName, String na
185171
}
186172

187173
public static MultipleSourcesContainer processNamedData(List<StrippedSourceContainer> strippedSources,
188-
Environment environment, LinkedHashSet<String> sourceNames, String namespace, boolean decode) {
174+
Environment environment, LinkedHashSet<String> sourceNames, String namespace, boolean decode) {
189175
return processNamedData(strippedSources, environment, sourceNames, namespace, decode, true);
190176
}
191177

@@ -195,14 +181,13 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
195181
* defined order).
196182
*/
197183
public static MultipleSourcesContainer processNamedData(List<StrippedSourceContainer> strippedSources,
198-
Environment environment, LinkedHashSet<String> sourceNames, String namespace, boolean decode,
199-
boolean includeDefaultProfileData) {
184+
Environment environment, LinkedHashSet<String> sourceNames, String namespace, boolean decode,
185+
boolean includeDefaultProfileData) {
200186

201187
Map<String, StrippedSourceContainer> hashByName = strippedSources.stream()
202188
.collect(Collectors.toMap(StrippedSourceContainer::name, Function.identity()));
203189

204-
LinkedHashSet<String> foundSourceNames = new LinkedHashSet<>();
205-
Map<String, Object> data = new HashMap<>();
190+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
206191

207192
// This is an ordered stream, and it means that non-profile-based sources will be
208193
// processed before profile-based sources.
@@ -212,7 +197,6 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
212197
StrippedSourceContainer stripped = hashByName.get(sourceName);
213198
if (stripped != null) {
214199
LOG.debug("Found source with name : '" + sourceName + "' in namespace: '" + namespace + "'");
215-
foundSourceNames.add(sourceName);
216200
// see if data is a single yaml/properties file and if it needs decoding
217201
Map<String, String> rawData = stripped.data();
218202
if (decode) {
@@ -228,7 +212,7 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
228212
*/
229213
if (processSource(includeDefaultProfileData, environment, sourceName, rawData)) {
230214
Map<String, Object> processedData = SourceDataEntriesProcessor.processAllEntries(
231-
rawData == null ? Map.of() : rawData, environment, includeDefaultProfileData);
215+
rawData == null ? Map.of() : rawData, environment, includeDefaultProfileData);
232216
data.put(sourceName, processedData);
233217
}
234218
}
@@ -237,11 +221,11 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
237221
}
238222
});
239223

240-
return new MultipleSourcesContainer(foundSourceNames, data);
224+
return new MultipleSourcesContainer(data);
241225
}
242226

243227
static boolean processSource(boolean includeDefaultProfileData, Environment environment, String sourceName,
244-
Map<String, String> sourceRawData) {
228+
Map<String, String> sourceRawData) {
245229
List<String> activeProfiles = Arrays.stream(environment.getActiveProfiles()).toList();
246230

247231
boolean emptyActiveProfiles = activeProfiles.isEmpty();
@@ -252,7 +236,7 @@ static boolean processSource(boolean includeDefaultProfileData, Environment envi
252236
boolean defaultProfilePresent = activeProfiles.contains("default");
253237

254238
return includeDefaultProfileData || emptyActiveProfiles || profileBasedSourceName || defaultProfilePresent
255-
|| rawDataContainsProfileBasedSource(activeProfiles, sourceRawData).getAsBoolean();
239+
|| rawDataContainsProfileBasedSource(activeProfiles, sourceRawData).getAsBoolean();
256240
}
257241

258242
/*
@@ -264,7 +248,7 @@ static boolean processSource(boolean includeDefaultProfileData, Environment envi
264248
* yaml/yml/properties. For example: 'account-k8s.yaml' or the like.
265249
*/
266250
static BooleanSupplier rawDataContainsProfileBasedSource(List<String> activeProfiles,
267-
Map<String, String> sourceRawData) {
251+
Map<String, String> sourceRawData) {
268252
return () -> Optional.ofNullable(sourceRawData)
269253
.orElse(Map.of())
270254
.keySet()
@@ -273,7 +257,7 @@ static BooleanSupplier rawDataContainsProfileBasedSource(List<String> activeProf
273257
.anyMatch(activeProfile -> ENDS_WITH_PROFILE_AND_EXTENSION.test(keyName, activeProfile)));
274258
}
275259

276-
static String sourceDataName(String target, LinkedHashSet<String> sourceNames, String namespace) {
260+
static String sourceDataName(String target, Set<String> sourceNames, String namespace) {
277261
String sortedNames = sourceNames.stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
278262
return sourceName(target, sortedNames, namespace);
279263
}
@@ -285,8 +269,8 @@ static String sourceDataName(String target, LinkedHashSet<String> sourceNames, S
285269
* these names to find any profile-based sources.
286270
*/
287271
public static MultipleSourcesContainer processLabeledData(List<StrippedSourceContainer> containers,
288-
Environment environment, Map<String, String> labels, String namespace, Set<String> profiles,
289-
boolean decode) {
272+
Environment environment, Map<String, String> labels, String namespace, Set<String> profiles,
273+
boolean decode) {
290274

291275
// find sources by provided labels
292276
List<StrippedSourceContainer> byLabels = containers.stream().filter(one -> {
@@ -318,13 +302,11 @@ public static MultipleSourcesContainer processLabeledData(List<StrippedSourceCon
318302
all.addAll(byLabels);
319303
all.addAll(byProfile);
320304

321-
LinkedHashSet<String> sourceNames = new LinkedHashSet<>();
322-
Map<String, Object> data = new HashMap<>();
305+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
323306

324307
all.forEach(source -> {
325308
String foundSourceName = source.name();
326309
LOG.debug("Loaded source with name : '" + foundSourceName + " in namespace: '" + namespace + "'");
327-
sourceNames.add(foundSourceName);
328310

329311
Map<String, String> rawData = source.data();
330312
if (decode) {
@@ -335,7 +317,7 @@ public static MultipleSourcesContainer processLabeledData(List<StrippedSourceCon
335317
data.put(foundSourceName, dataFromOneSource);
336318
});
337319

338-
return new MultipleSourcesContainer(sourceNames, data);
320+
return new MultipleSourcesContainer(data);
339321
}
340322

341323
private static Map<String, String> decodeData(Map<String, String> data) {
@@ -345,7 +327,7 @@ private static Map<String, String> decodeData(Map<String, String> data) {
345327
}
346328

347329
public static <T> void registerSingle(ConfigurableBootstrapContext bootstrapContext, Class<T> cls, T instance,
348-
String name, ApplicationListener<?> listener) {
330+
String name, ApplicationListener<?> listener) {
349331
bootstrapContext.registerIfAbsent(cls, BootstrapRegistry.InstanceSupplier.of(instance));
350332
bootstrapContext.addCloseListener(event -> {
351333

@@ -359,7 +341,7 @@ public static <T> void registerSingle(ConfigurableBootstrapContext bootstrapCont
359341
}
360342

361343
public static <T> void registerSingle(ConfigurableBootstrapContext bootstrapContext, Class<T> cls, T instance,
362-
String name) {
344+
String name) {
363345
registerSingle(bootstrapContext, cls, instance, name, NO_OP);
364346
}
365347

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.cloud.kubernetes.commons.config;
1818

1919
import java.util.Arrays;
20-
import java.util.LinkedHashSet;
20+
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222
import java.util.Set;
2323
import java.util.stream.Collectors;
@@ -46,7 +46,7 @@ public abstract class LabeledSourceData {
4646
private static final Log LOG = LogFactory.getLog(LabeledSourceData.class);
4747

4848
public final SourceData compute(Map<String, String> labels, Prefix prefix, String target, boolean profileSources,
49-
boolean failFast, String namespace, String[] activeProfiles) {
49+
boolean failFast, String namespace, String[] activeProfiles) {
5050

5151
MultipleSourcesContainer data = MultipleSourcesContainer.empty();
5252
String sourceDataName;
@@ -57,34 +57,33 @@ public final SourceData compute(Map<String, String> labels, Prefix prefix, Strin
5757
profiles = Arrays.stream(activeProfiles).collect(Collectors.toSet());
5858
}
5959
data = dataSupplier(labels, profiles);
60+
LinkedHashMap<String, Map<String, Object>> sourceData = data.data();
61+
sourceDataName = sourceDataName(target, sourceData.keySet(), namespace);
6062

61-
LinkedHashSet<String> sourceNames = data.names();
62-
Map<String, Object> sourceDataForSourceName = data.data();
63-
sourceDataName = sourceDataName(target, sourceNames, namespace);
64-
65-
if (sourceNames.isEmpty()) {
63+
if (sourceData.isEmpty()) {
6664
return emptySourceData(labels, target, namespace);
6765
}
6866

6967
if (prefix.getName().equals(Prefix.DEFAULT.getName())) {
70-
return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceNames, sourceDataForSourceName));
68+
return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceData));
7169
}
7270

7371
if (prefix.getName().equals(Prefix.KNOWN.getName())) {
7472
return new SourceData(sourceDataName,
75-
prefixFlattenedSourceData(sourceNames, sourceDataForSourceName, prefix.prefixProvider().get()));
73+
prefixFlattenedSourceData(sourceData, prefix.prefixProvider().get()));
7674
}
7775

7876
if (prefix.getName().equals(Prefix.DELAYED.getName())) {
79-
return new SourceData(sourceDataName, nameFlattenedSourceData(sourceNames, sourceDataForSourceName));
77+
return new SourceData(sourceDataName, nameFlattenedSourceData(sourceData));
8078
}
8179

8280
throw new IllegalArgumentException("Unsupported prefix: " + prefix);
8381
}
8482
catch (Exception e) {
8583
LOG.warn("Failure in reading labeled sources");
8684
onException(failFast, e);
87-
return new SourceData(sourceDataName(target, data.names(), namespace), Map.of(ERROR_PROPERTY, "true"));
85+
return new SourceData(sourceDataName(target, data.data().keySet(), namespace),
86+
Map.of(ERROR_PROPERTY, "true"));
8887
}
8988

9089
}
@@ -98,7 +97,7 @@ private SourceData emptySourceData(Map<String, String> labels, String target, St
9897
.stream()
9998
.sorted()
10099
.collect(Collectors.collectingAndThen(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR),
101-
sortedLabels -> sourceName(target, sortedLabels, namespace)));
100+
sortedLabels -> sourceName(target, sortedLabels, namespace)));
102101

103102
return SourceData.emptyRecord(sourceName);
104103
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-present the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

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

19-
import java.util.LinkedHashSet;
19+
import java.util.LinkedHashMap;
2020
import java.util.Map;
2121

2222
/**
@@ -25,10 +25,9 @@
2525
* Container that stores multiple sources, to be exact their names and their flattenned
2626
* data. We force a LinkedHashSet on purpose, to preserve the order of sources.
2727
*/
28-
public record MultipleSourcesContainer(LinkedHashSet<String> names, Map<String, Object> data) {
28+
public record MultipleSourcesContainer(LinkedHashMap<String, Map<String, Object>> data) {
2929

30-
private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashSet<>(0),
31-
Map.of());
30+
private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashMap<>(0));
3231

3332
public static MultipleSourcesContainer empty() {
3433
return EMPTY;

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

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

19+
import java.util.LinkedHashMap;
1920
import java.util.LinkedHashSet;
2021
import java.util.Map;
22+
import java.util.Set;
2123
import java.util.stream.Collectors;
2224

2325
import org.apache.commons.logging.Log;
@@ -43,7 +45,7 @@ public abstract class NamedSourceData {
4345
private static final Log LOG = LogFactory.getLog(NamedSourceData.class);
4446

4547
public final SourceData compute(String sourceName, Prefix prefix, String target, boolean profileSources,
46-
boolean failFast, String namespace, String[] activeProfiles) {
48+
boolean failFast, String namespace, String[] activeProfiles) {
4749

4850
// first comes a non-profile-based source
4951
LinkedHashSet<String> sourceNamesToSearchFor = new LinkedHashSet<>();
@@ -60,31 +62,29 @@ public final SourceData compute(String sourceName, Prefix prefix, String target,
6062
}
6163

6264
data = dataSupplier(sourceNamesToSearchFor);
63-
Map<String, Object> sourceDataForSourceName = data.data();
64-
LinkedHashSet<String> sourceNamesFound = data.names();
65-
String sortedNames = data.names()
66-
.stream()
65+
LinkedHashMap<String, Map<String, Object>> sourceData = data.data();
66+
67+
Set<String> sourceNamesFound = sourceData.keySet();
68+
String sortedNames = sourceNamesFound.stream()
6769
.sorted()
6870
.collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
6971
sourceDataName = generateSourceName(target, sortedNames, namespace, activeProfiles);
7072

71-
if (data.names().isEmpty()) {
73+
if (sourceData.isEmpty()) {
7274
return emptySourceData(target, sourceName, namespace);
7375
}
7476

7577
if (prefix.getName().equals(Prefix.DEFAULT.getName())) {
76-
return new SourceData(sourceDataName,
77-
defaultFlattenedSourceData(sourceNamesFound, sourceDataForSourceName));
78+
return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceData));
7879
}
7980

8081
if (prefix.getName().equals(Prefix.KNOWN.getName())) {
81-
return new SourceData(sourceDataName, prefixFlattenedSourceData(sourceNamesFound,
82-
sourceDataForSourceName, prefix.prefixProvider().get()));
82+
return new SourceData(sourceDataName,
83+
prefixFlattenedSourceData(sourceData, prefix.prefixProvider().get()));
8384
}
8485

8586
if (prefix.getName().equals(Prefix.DELAYED.getName())) {
86-
return new SourceData(sourceDataName,
87-
nameFlattenedSourceData(sourceNamesFound, sourceDataForSourceName));
87+
return new SourceData(sourceDataName, nameFlattenedSourceData(sourceData));
8888
}
8989

9090
throw new IllegalArgumentException("Unsupported prefix: " + prefix);
@@ -93,9 +93,13 @@ public final SourceData compute(String sourceName, Prefix prefix, String target,
9393
catch (Exception e) {
9494
LOG.warn("Failure in reading named sources");
9595
onException(failFast, e);
96-
String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
96+
String names = data.data()
97+
.keySet()
98+
.stream()
99+
.sorted()
100+
.collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
97101
return new SourceData(generateSourceName(target, names, namespace, activeProfiles),
98-
Map.of(ERROR_PROPERTY, "true"));
102+
Map.of(ERROR_PROPERTY, "true"));
99103
}
100104

101105
}

0 commit comments

Comments
 (0)