Skip to content

Commit d240fe1

Browse files
committed
cleanup
Signed-off-by: wind57 <[email protected]>
1 parent 0b46806 commit d240fe1

File tree

7 files changed

+81
-122
lines changed

7 files changed

+81
-122
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/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/SourceDataFlattener.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ static Map<String, Object> defaultFlattenedSourceData(LinkedHashMap<String, Map<
4444
static Map<String, Object> prefixFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData,
4545
String prefix) {
4646
Map<String, Object> flattenedData = new HashMap<>();
47-
sourceData.values().forEach(data ->
48-
data.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)));
47+
sourceData.values().forEach(data -> data.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)));
4948
return flattenedData;
5049
}
5150

@@ -55,8 +54,7 @@ static Map<String, Object> prefixFlattenedSourceData(LinkedHashMap<String, Map<S
5554
*/
5655
static Map<String, Object> nameFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
5756
Map<String, Object> flattenedData = new HashMap<>();
58-
sourceData.forEach((name, data) ->
59-
data.forEach((key, value) -> flattenedData.put(name + "." + key, value)));
57+
sourceData.forEach((name, data) -> data.forEach((key, value) -> flattenedData.put(name + "." + key, value)));
6058
return flattenedData;
6159
}
6260

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ void testProcessNamedDataOne() {
8080
MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames,
8181
namespace, decode, includeDefaultProfileData);
8282
Assertions.assertThat(result).isNotNull();
83-
Assertions.assertThat(result.names()).containsExactlyInAnyOrder("configmap-a");
83+
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("configmap-a");
8484

85-
@SuppressWarnings("unchecked")
86-
Map<String, Object> data = (Map<String, Object>) result.data().get("configmap-a");
85+
Map<String, Object> data = result.data().get("configmap-a");
8786
Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1"));
8887
}
8988

@@ -162,7 +161,7 @@ void testProcessNamedDataTwo(CapturedOutput output) {
162161
MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames,
163162
namespace, decode, includeDefaultProfileData);
164163
Assertions.assertThat(result).isNotNull();
165-
Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account");
164+
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account");
166165

167166
/**
168167
* <pre>
@@ -176,8 +175,7 @@ void testProcessNamedDataTwo(CapturedOutput output) {
176175
* since 'k8s' is not an active profile.
177176
* </pre>
178177
*/
179-
@SuppressWarnings("unchecked")
180-
Map<String, Object> data = (Map<String, Object>) result.data().get("account");
178+
Map<String, Object> data = result.data().get("account");
181179
Assertions.assertThat(data)
182180
.containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "three", "3", "five", "5"));
183181
Assertions.assertThat(output.getOut()).contains("entry : account-k8s.properties will be skipped");
@@ -258,7 +256,7 @@ void testProcessNamedDataThree(CapturedOutput output) {
258256
MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames,
259257
namespace, decode, includeDefaultProfileData);
260258
Assertions.assertThat(result).isNotNull();
261-
Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account-default");
259+
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account-default");
262260

263261
/**
264262
* <pre>
@@ -274,8 +272,7 @@ void testProcessNamedDataThree(CapturedOutput output) {
274272
* 6. we do not have 'four=4' since we do not read 'account-k8s.properties'
275273
* </pre>
276274
*/
277-
@SuppressWarnings("unchecked")
278-
Map<String, Object> data = (Map<String, Object>) result.data().get("account-default");
275+
Map<String, Object> data = result.data().get("account-default");
279276
Assertions.assertThat(data)
280277
.containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "three", "3", "five", "5"));
281278
Assertions.assertThat(output.getOut()).contains("entry : account-k8s.properties will be skipped");
@@ -373,14 +370,13 @@ void testProcessNamedDataFive(CapturedOutput output) {
373370
MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames,
374371
namespace, decode, includeDefaultProfileData);
375372
Assertions.assertThat(result).isNotNull();
376-
Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account");
373+
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account");
377374

378375
/*
379376
* <pre> - we only read from 'account-k8s.properties' </pre>
380377
*/
381378

382-
@SuppressWarnings("unchecked")
383-
Map<String, Object> accountData = (Map<String, Object>) result.data().get("account");
379+
Map<String, Object> accountData = result.data().get("account");
384380
Assertions.assertThat(accountData).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "four", "4"));
385381
Assertions.assertThat(output.getOut()).contains("entry : account.properties will be skipped");
386382
Assertions.assertThat(output.getOut()).contains("entry : account-default.properties will be skipped");
@@ -462,7 +458,7 @@ void testProcessNamedDataSix(CapturedOutput output) {
462458
MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames,
463459
namespace, decode, includeDefaultProfileData);
464460
Assertions.assertThat(result).isNotNull();
465-
Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account-k8s");
461+
Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account-k8s");
466462

467463
/**
468464
* <pre>
@@ -481,8 +477,7 @@ void testProcessNamedDataSix(CapturedOutput output) {
481477
* (because 'prod' is not an active profile)
482478
* </pre>
483479
*/
484-
@SuppressWarnings("unchecked")
485-
Map<String, Object> data = (Map<String, Object>) result.data().get("account-k8s");
480+
Map<String, Object> data = result.data().get("account-k8s");
486481
Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "five", "5"));
487482
Assertions.assertThat(output.getOut()).contains("entry : account-prod.properties will be skipped");
488483
Assertions.assertThat(output.getOut()).contains("entry : account.properties will be skipped");

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ void testMerge() {
166166
new MockEnvironment(), sourceNames, "default", false);
167167

168168
Assertions.assertThat(result.data().size()).isEqualTo(2);
169-
@SuppressWarnings("unchecked")
170-
Map<String, Object> one = (Map<String, Object>) result.data().get("configmap-one");
169+
Map<String, Object> one = result.data().get("configmap-one");
171170
Assertions.assertThat(one.get("propA")).isEqualTo("A");
172171
Assertions.assertThat(one.get("propB")).isEqualTo("B");
173172

174-
@SuppressWarnings("unchecked")
175-
Map<String, Object> oneKubernetes = (Map<String, Object>) result.data().get("configmap-one-kubernetes");
173+
Map<String, Object> oneKubernetes = result.data().get("configmap-one-kubernetes");
176174
Assertions.assertThat(oneKubernetes.get("propA")).isEqualTo("AA");
177175
Assertions.assertThat(oneKubernetes.get("propC")).isEqualTo("C");
178176

@@ -205,7 +203,6 @@ void testKeysWithPrefixNonEmptyPrefix() {
205203
}
206204

207205
@Test
208-
@SuppressWarnings("unchecked")
209206
void testIssue1757() {
210207

211208
StrippedSourceContainer containerA = new StrippedSourceContainer(Map.of("load", "true"), "client-1",
@@ -217,14 +214,13 @@ void testIssue1757() {
217214
MultipleSourcesContainer container = ConfigUtils.processLabeledData(List.of(containerA, containerB),
218215
new MockEnvironment(), Map.of("load", "true"), "default", Set.of(), false);
219216

220-
System.out.println(container);
221-
assertThat(container.names()).containsExactlyInAnyOrder("client-1", "client-2");
217+
assertThat(container.data().keySet()).containsExactlyInAnyOrder("client-1", "client-2");
222218

223-
Map<String, Object> client1Data = (Map<String, Object>) container.data().get("client-1");
219+
Map<String, Object> client1Data = container.data().get("client-1");
224220
assertThat(client1Data)
225221
.containsExactlyInAnyOrderEntriesOf(Map.of("client-id", "clientA", "client-secret", "a"));
226222

227-
Map<String, Object> client2Data = (Map<String, Object>) container.data().get("client-2");
223+
Map<String, Object> client2Data = container.data().get("client-2");
228224
assertThat(client2Data)
229225
.containsExactlyInAnyOrderEntriesOf(Map.of("client-id", "clientB", "client-secret", "b"));
230226

0 commit comments

Comments
 (0)