Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -157,21 +158,6 @@ public static void onException(boolean failFast, Exception e) {
LOG.warn(e.getMessage() + ". Ignoring.", e);
}

/*
* This method will return a SourceData that has a name in the form :
* "configmap.my-configmap.my-configmap-2.namespace" and the "data" from the context
* is appended with prefix. So if incoming is "a=b", the result will be : "prefix.a=b"
*/
@Deprecated(forRemoval = true)
public static SourceData withPrefix(String target, PrefixContext context) {
Map<String, Object> withPrefix = CollectionUtils.newHashMap(context.data().size());
context.data().forEach((key, value) -> withPrefix.put(context.prefix() + "." + key, value));

String propertySourceTokens = String.join(PROPERTY_SOURCE_NAME_SEPARATOR,
context.propertySourceNames().stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)));
return new SourceData(sourceName(target, propertySourceTokens, context.namespace()), withPrefix);
}

public static String sourceName(String target, String applicationName, String namespace) {
return target + PROPERTY_SOURCE_NAME_SEPARATOR + applicationName + PROPERTY_SOURCE_NAME_SEPARATOR + namespace;
}
Expand Down Expand Up @@ -201,8 +187,7 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
Map<String, StrippedSourceContainer> hashByName = strippedSources.stream()
.collect(Collectors.toMap(StrippedSourceContainer::name, Function.identity()));

LinkedHashSet<String> foundSourceNames = new LinkedHashSet<>();
Map<String, Object> data = new HashMap<>();
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();

// This is an ordered stream, and it means that non-profile-based sources will be
// processed before profile-based sources.
Expand All @@ -212,7 +197,6 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
StrippedSourceContainer stripped = hashByName.get(sourceName);
if (stripped != null) {
LOG.debug("Found source with name : '" + sourceName + "' in namespace: '" + namespace + "'");
foundSourceNames.add(sourceName);
// see if data is a single yaml/properties file and if it needs decoding
Map<String, String> rawData = stripped.data();
if (decode) {
Expand All @@ -237,7 +221,7 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
}
});

return new MultipleSourcesContainer(foundSourceNames, data);
return new MultipleSourcesContainer(data);
}

static boolean processSource(boolean includeDefaultProfileData, Environment environment, String sourceName,
Expand Down Expand Up @@ -273,7 +257,7 @@ static BooleanSupplier rawDataContainsProfileBasedSource(List<String> activeProf
.anyMatch(activeProfile -> ENDS_WITH_PROFILE_AND_EXTENSION.test(keyName, activeProfile)));
}

static String sourceDataName(String target, LinkedHashSet<String> sourceNames, String namespace) {
static String sourceDataName(String target, Set<String> sourceNames, String namespace) {
String sortedNames = sourceNames.stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
return sourceName(target, sortedNames, namespace);
}
Expand Down Expand Up @@ -318,13 +302,11 @@ public static MultipleSourcesContainer processLabeledData(List<StrippedSourceCon
all.addAll(byLabels);
all.addAll(byProfile);

LinkedHashSet<String> sourceNames = new LinkedHashSet<>();
Map<String, Object> data = new HashMap<>();
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();

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

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

return new MultipleSourcesContainer(sourceNames, data);
return new MultipleSourcesContainer(data);
}

private static Map<String, String> decodeData(Map<String, String> data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.springframework.cloud.kubernetes.commons.config;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -57,34 +57,33 @@ public final SourceData compute(Map<String, String> labels, Prefix prefix, Strin
profiles = Arrays.stream(activeProfiles).collect(Collectors.toSet());
}
data = dataSupplier(labels, profiles);
LinkedHashMap<String, Map<String, Object>> sourceData = data.data();
sourceDataName = sourceDataName(target, sourceData.keySet(), namespace);

LinkedHashSet<String> sourceNames = data.names();
Map<String, Object> sourceDataForSourceName = data.data();
sourceDataName = sourceDataName(target, sourceNames, namespace);

if (sourceNames.isEmpty()) {
if (sourceData.isEmpty()) {
return emptySourceData(labels, target, namespace);
}

if (prefix.getName().equals(Prefix.DEFAULT.getName())) {
return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceNames, sourceDataForSourceName));
return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceData));
}

if (prefix.getName().equals(Prefix.KNOWN.getName())) {
return new SourceData(sourceDataName,
prefixFlattenedSourceData(sourceNames, sourceDataForSourceName, prefix.prefixProvider().get()));
prefixFlattenedSourceData(sourceData, prefix.prefixProvider().get()));
}

if (prefix.getName().equals(Prefix.DELAYED.getName())) {
return new SourceData(sourceDataName, nameFlattenedSourceData(sourceNames, sourceDataForSourceName));
return new SourceData(sourceDataName, nameFlattenedSourceData(sourceData));
}

throw new IllegalArgumentException("Unsupported prefix: " + prefix);
}
catch (Exception e) {
LOG.warn("Failure in reading labeled sources");
onException(failFast, e);
return new SourceData(sourceDataName(target, data.names(), namespace), Map.of(ERROR_PROPERTY, "true"));
return new SourceData(sourceDataName(target, data.data().keySet(), namespace),
Map.of(ERROR_PROPERTY, "true"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.Map;

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

private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashSet<>(0),
Map.of());
private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashMap<>(0));

public static MultipleSourcesContainer empty() {
return EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

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

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -60,31 +62,29 @@ public final SourceData compute(String sourceName, Prefix prefix, String target,
}

data = dataSupplier(sourceNamesToSearchFor);
Map<String, Object> sourceDataForSourceName = data.data();
LinkedHashSet<String> sourceNamesFound = data.names();
String sortedNames = data.names()
.stream()
LinkedHashMap<String, Map<String, Object>> sourceData = data.data();

Set<String> sourceNamesFound = sourceData.keySet();
String sortedNames = sourceNamesFound.stream()
.sorted()
.collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
sourceDataName = generateSourceName(target, sortedNames, namespace, activeProfiles);

if (data.names().isEmpty()) {
if (sourceData.isEmpty()) {
return emptySourceData(target, sourceName, namespace);
}

if (prefix.getName().equals(Prefix.DEFAULT.getName())) {
return new SourceData(sourceDataName,
defaultFlattenedSourceData(sourceNamesFound, sourceDataForSourceName));
return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceData));
}

if (prefix.getName().equals(Prefix.KNOWN.getName())) {
return new SourceData(sourceDataName, prefixFlattenedSourceData(sourceNamesFound,
sourceDataForSourceName, prefix.prefixProvider().get()));
return new SourceData(sourceDataName,
prefixFlattenedSourceData(sourceData, prefix.prefixProvider().get()));
}

if (prefix.getName().equals(Prefix.DELAYED.getName())) {
return new SourceData(sourceDataName,
nameFlattenedSourceData(sourceNamesFound, sourceDataForSourceName));
return new SourceData(sourceDataName, nameFlattenedSourceData(sourceData));
}

throw new IllegalArgumentException("Unsupported prefix: " + prefix);
Expand All @@ -93,7 +93,11 @@ public final SourceData compute(String sourceName, Prefix prefix, String target,
catch (Exception e) {
LOG.warn("Failure in reading named sources");
onException(failFast, e);
String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
String names = data.data()
.keySet()
.stream()
.sorted()
.collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR));
return new SourceData(generateSourceName(target, names, namespace, activeProfiles),
Map.of(ERROR_PROPERTY, "true"));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.springframework.cloud.kubernetes.commons.config;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand All @@ -32,47 +32,29 @@ private SourceDataFlattener() {
/**
* Flattens the data from rawData without any additional processing.
*/
static Map<String, Object> defaultFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData) {
static Map<String, Object> defaultFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
Map<String, Object> flattenedData = new HashMap<>();

names.forEach(name -> {
@SuppressWarnings("unchecked")
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
flattenedData.putAll(singleDataEntry);
});

sourceData.values().forEach(flattenedData::putAll);
return flattenedData;
}

/**
* Flattens the data from rawData by adding a prefix for each key.
*/
static Map<String, Object> prefixFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData,
static Map<String, Object> prefixFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData,
String prefix) {
Map<String, Object> flattenedData = new HashMap<>();

names.forEach(name -> {
@SuppressWarnings("unchecked")
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
singleDataEntry.forEach((key, value) -> flattenedData.put(prefix + "." + key, value));
});

sourceData.values().forEach(data -> data.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)));
return flattenedData;
}

/**
* Flattens the data from rawData by adding a prefix for each key, which is equal to
* the source name.
*/
static Map<String, Object> nameFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData) {
static Map<String, Object> nameFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
Map<String, Object> flattenedData = new HashMap<>();

names.forEach(name -> {
@SuppressWarnings("unchecked")
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
singleDataEntry.forEach((key, value) -> flattenedData.put(name + "." + key, value));
});

sourceData.forEach((name, data) -> data.forEach((key, value) -> flattenedData.put(name + "." + key, value)));
return flattenedData;
}

Expand Down
Loading
Loading