Skip to content

Commit d946296

Browse files
authored
Merge pull request #2019 from wind57/fix-1757-cleanup-for-main
clean-up in main after 1757 ( main )
2 parents cbcae88 + 76abe25 commit d946296

File tree

10 files changed

+117
-235
lines changed

10 files changed

+117
-235
lines changed

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

Lines changed: 6 additions & 24 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;
@@ -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
}
@@ -201,8 +187,7 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
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) {
@@ -237,7 +221,7 @@ 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,
@@ -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
}
@@ -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) {

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

Lines changed: 9 additions & 10 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;
@@ -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
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 16 additions & 12 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;
@@ -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,7 +93,11 @@ 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),
98102
Map.of(ERROR_PROPERTY, "true"));
99103
}

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 7 additions & 25 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.HashMap;
20-
import java.util.LinkedHashSet;
20+
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222

2323
/**
@@ -32,47 +32,29 @@ private SourceDataFlattener() {
3232
/**
3333
* Flattens the data from rawData without any additional processing.
3434
*/
35-
static Map<String, Object> defaultFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData) {
35+
static Map<String, Object> defaultFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
3636
Map<String, Object> flattenedData = new HashMap<>();
37-
38-
names.forEach(name -> {
39-
@SuppressWarnings("unchecked")
40-
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
41-
flattenedData.putAll(singleDataEntry);
42-
});
43-
37+
sourceData.values().forEach(flattenedData::putAll);
4438
return flattenedData;
4539
}
4640

4741
/**
4842
* Flattens the data from rawData by adding a prefix for each key.
4943
*/
50-
static Map<String, Object> prefixFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData,
44+
static Map<String, Object> prefixFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData,
5145
String prefix) {
5246
Map<String, Object> flattenedData = new HashMap<>();
53-
54-
names.forEach(name -> {
55-
@SuppressWarnings("unchecked")
56-
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
57-
singleDataEntry.forEach((key, value) -> flattenedData.put(prefix + "." + key, value));
58-
});
59-
47+
sourceData.values().forEach(data -> data.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)));
6048
return flattenedData;
6149
}
6250

6351
/**
6452
* Flattens the data from rawData by adding a prefix for each key, which is equal to
6553
* the source name.
6654
*/
67-
static Map<String, Object> nameFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData) {
55+
static Map<String, Object> nameFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
6856
Map<String, Object> flattenedData = new HashMap<>();
69-
70-
names.forEach(name -> {
71-
@SuppressWarnings("unchecked")
72-
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
73-
singleDataEntry.forEach((key, value) -> flattenedData.put(name + "." + key, value));
74-
});
75-
57+
sourceData.forEach((name, data) -> data.forEach((key, value) -> flattenedData.put(name + "." + key, value)));
7658
return flattenedData;
7759
}
7860

0 commit comments

Comments
 (0)