Skip to content

Commit

Permalink
Merge branch 'main' into cleanup-fabric8-loadbalancer-mapper-part-5
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Apr 3, 2024
2 parents cfa1b0b + dcb8fb4 commit 3d3a94b
Show file tree
Hide file tree
Showing 5 changed files with 632 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ private V1Pod internalGetPod() {
}
catch (Throwable t) {
if (failFast) {
if (t instanceof ApiException apiException) {
LOG.error("error reading pod : " + apiException.getResponseBody());
}
throw new RuntimeException(t);
}
if (t instanceof ApiException apiException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -51,6 +54,12 @@ public final class ConfigUtils {

private static final Log LOG = LogFactory.getLog(ConfigUtils.class);

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

private ConfigUtils() {
}

Expand Down Expand Up @@ -205,19 +214,13 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
rawData = decodeData(rawData);
}

// In some cases we want to include properties from the default profile
// along with any active profiles
// In these cases includeDefaultProfileData will be true
// If includeDefaultProfileData is false then we want to make sure that we
// only return properties from any active profiles

// Check the source to see if it contains any active profiles
boolean containsActiveProfile = environment.getActiveProfiles().length == 0
|| Arrays.stream(environment.getActiveProfiles())
.anyMatch(activeProfile -> sourceName.endsWith("-" + activeProfile)
|| "default".equals(activeProfile));
if (includeDefaultProfileData || containsActiveProfile
|| containsDataWithProfile(rawData, environment.getActiveProfiles())) {
/*
* In some cases we want to include properties from the default profile
* along with any active profiles In these cases includeDefaultProfileData
* will be true If includeDefaultProfileData is false then we want to make
* sure that we only return properties from any active profiles
*/
if (processSource(includeDefaultProfileData, environment, sourceName, rawData)) {
data.putAll(SourceDataEntriesProcessor.processAllEntries(rawData == null ? Map.of() : rawData,
environment, includeDefaultProfileData));
}
Expand All @@ -227,13 +230,34 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
return new MultipleSourcesContainer(foundSourceNames, data);
}

static boolean processSource(boolean includeDefaultProfileData, Environment environment, String sourceName,
Map<String, String> sourceRawData) {
List<String> activeProfiles = Arrays.stream(environment.getActiveProfiles()).toList();

boolean emptyActiveProfiles = activeProfiles.isEmpty();

boolean profileBasedSourceName = activeProfiles.stream()
.anyMatch(activeProfile -> sourceName.endsWith("-" + activeProfile));

boolean defaultProfilePresent = activeProfiles.contains("default");

return includeDefaultProfileData || emptyActiveProfiles || profileBasedSourceName || defaultProfilePresent
|| rawDataContainsProfileBasedSource(activeProfiles, sourceRawData).getAsBoolean();
}

/*
* In the case there the data contains yaml or properties files we need to check their
* names to see if they contain any active profiles.
* this one is not inlined into 'processSource' because other filters, that come
* before it, might have already resolved to 'true', so no need to compute it at all.
*
* This method is supposed to answer the question if raw data that a certain source
* (configmap or secret) has entries that are themselves profile based
* yaml/yml/properties. For example: 'account-k8s.yaml' or the like.
*/
private static boolean containsDataWithProfile(Map<String, String> rawData, String[] activeProfiles) {
return rawData.keySet().stream().anyMatch(key -> Arrays.stream(activeProfiles)
.anyMatch(activeProfile -> key.contains("-" + activeProfile) || "default".equals(activeProfile)));
static BooleanSupplier rawDataContainsProfileBasedSource(List<String> activeProfiles,
Map<String, String> sourceRawData) {
return () -> Optional.ofNullable(sourceRawData).orElse(Map.of()).keySet().stream()
.anyMatch(keyName -> activeProfiles.stream()
.anyMatch(activeProfile -> ENDS_WITH_PROFILE_AND_EXTENSION.test(keyName, activeProfile)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -47,6 +48,9 @@ public class SourceDataEntriesProcessor extends MapPropertySource {

private static final Log LOG = LogFactory.getLog(SourceDataEntriesProcessor.class);

private static Predicate<String> ENDS_IN_EXTENSION = x -> x.endsWith(".yml") || x.endsWith(".yaml")
|| x.endsWith(".properties");

public SourceDataEntriesProcessor(SourceData sourceData) {
super(sourceData.sourceName(), sourceData.sourceData());
}
Expand Down Expand Up @@ -92,7 +96,7 @@ static List<Map.Entry<String, String>> sorted(Map<String, String> input, Environ
* 3. then plain properties
* </pre>
*/
static List<Map.Entry<String, String>> sorted(Map<String, String> input, Environment environment,
static List<Map.Entry<String, String>> sorted(Map<String, String> rawData, Environment environment,
boolean includeDefaultProfileData) {

record WeightedEntry(Map.Entry<String, String> entry, int weight) {
Expand Down Expand Up @@ -124,16 +128,17 @@ record WeightedEntry(Map.Entry<String, String> entry, int weight) {
int current = orderedFileNames.size() - 1;
List<WeightedEntry> weightedEntries = new ArrayList<>();

if (input.entrySet().stream().noneMatch(entry -> entry.getKey().endsWith(".yml")
|| entry.getKey().endsWith(".yaml") || entry.getKey().endsWith(".properties"))) {
for (Map.Entry<String, String> entry : input.entrySet()) {
boolean plainEntriesOnly = rawData.keySet().stream().noneMatch(ENDS_IN_EXTENSION);

if (plainEntriesOnly) {
for (Map.Entry<String, String> entry : rawData.entrySet()) {
weightedEntries.add(new WeightedEntry(entry, ++current));
}
}
else {
for (Map.Entry<String, String> entry : input.entrySet()) {
for (Map.Entry<String, String> entry : rawData.entrySet()) {
String key = entry.getKey();
if (key.endsWith(".yml") || key.endsWith(".yaml") || key.endsWith(".properties")) {
if (ENDS_IN_EXTENSION.test(key)) {
String withoutExtension = key.split("\\.", 2)[0];
int index = orderedFileNames.indexOf(withoutExtension);
if (index >= 0) {
Expand Down
Loading

0 comments on commit 3d3a94b

Please sign in to comment.