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 1, 2024
2 parents 6b58a62 + 9c74e14 commit cfa1b0b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,6 @@ static MultipleSourcesContainer secretsDataByName(CoreV1Api coreV1Api, String na
includeDefaultProfileData);
}

static MultipleSourcesContainer secretsDataByName(CoreV1Api coreV1Api, String namespace,
LinkedHashSet<String> sourceNames, Environment environment) {
return secretsDataByName(coreV1Api, namespace, sourceNames, environment, true);
}

static MultipleSourcesContainer configMapsDataByName(CoreV1Api coreV1Api, String namespace,
LinkedHashSet<String> sourceNames, Environment environment) {
return configMapsDataByName(coreV1Api, namespace, sourceNames, environment, true);
}

/**
* <pre>
* 1. read all config maps in the provided namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
public class KubernetesClientSecretsPropertySource extends SecretsPropertySource {

@Deprecated(forRemoval = true)
public KubernetesClientSecretsPropertySource(SourceData sourceData) {
super(sourceData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ void matchIncludeSingleProfile() {
ConfigUtils.Prefix.DEFAULT, true, true);
MockEnvironment environment = new MockEnvironment();
environment.setActiveProfiles("with-profile");
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment, false);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment,
false);

KubernetesClientContextToSourceData data = new NamedConfigMapContextToSourceDataProvider().get();
SourceData sourceData = data.apply(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ void matchIncludeSingleProfile() {
true, true);
MockEnvironment environment = new MockEnvironment();
environment.addActiveProfile("with-profile");
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment, false);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment,
false);

KubernetesClientContextToSourceData data = new NamedSecretContextToSourceDataProvider().get();
SourceData sourceData = data.apply(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,28 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
// processed before profile based sources. This way, we replicate that
// "application-dev.yaml"
// overrides properties from "application.yaml"
sourceNames.forEach(source -> {
StrippedSourceContainer stripped = hashByName.get(source);
sourceNames.forEach(sourceName -> {
StrippedSourceContainer stripped = hashByName.get(sourceName);
if (stripped != null) {
LOG.debug("Found source with name : '" + source + " in namespace: '" + namespace + "'");
foundSourceNames.add(source);
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) {
rawData = decodeData(rawData);
}

// In some cases we want to include properties from the default profile along with any active profiles
// 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 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
// Check the source to see if it contains any active profiles
boolean containsActiveProfile = environment.getActiveProfiles().length == 0
|| Arrays.stream(environment.getActiveProfiles())
.anyMatch(p -> source.contains("-" + p) || "default".equals(p));
.anyMatch(activeProfile -> sourceName.endsWith("-" + activeProfile)
|| "default".equals(activeProfile));
if (includeDefaultProfileData || containsActiveProfile
|| containsDataWithProfile(rawData, environment.getActiveProfiles())) {
data.putAll(SourceDataEntriesProcessor.processAllEntries(rawData == null ? Map.of() : rawData,
Expand All @@ -225,12 +228,12 @@ public static MultipleSourcesContainer processNamedData(List<StrippedSourceConta
}

/*
* 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.
* 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.
*/
private static boolean containsDataWithProfile(Map<String, String> rawData, String[] activeProfiles) {
return rawData.keySet().stream().anyMatch(
key -> Arrays.stream(activeProfiles).anyMatch(p -> key.contains("-" + p) || "default".equals(p)));
return rawData.keySet().stream().anyMatch(key -> Arrays.stream(activeProfiles)
.anyMatch(activeProfile -> key.contains("-" + activeProfile) || "default".equals(activeProfile)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public boolean profileSpecificSources() {
}

/**
* append or not the active profiles to the name of the generated source.
* At the moment this is true only for config server generated sources.
* append or not the active profiles to the name of the generated source. At the
* moment this is true only for config server generated sources.
*/
public boolean appendProfileToName() {
return appendProfileToName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public boolean profileSpecificSources() {
}

/**
* append or not the active profiles to the name of the generated source.
* At the moment this is true only for config server generated sources.
* append or not the active profiles to the name of the generated source. At the
* moment this is true only for config server generated sources.
*/
public boolean appendProfileToName() {
return appendProfileToName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ record WeightedEntry(Map.Entry<String, String> entry, int weight) {
String applicationName = ConfigUtils.getApplicationName(environment, "", "");
String[] activeProfiles = environment.getActiveProfiles();

// In some cases we want to include the properties from the default profile along with any properties from any active profiles
// In this case includeDefaultProfileData will be true or the active profile will be default
// In the case where includeDefaultProfileData is false we only want to include the properties from the active profiles
boolean includeDataEntry = includeDefaultProfileData || Arrays.asList(environment.getActiveProfiles()).contains("default");
// In some cases we want to include the properties from the default profile along
// with any properties from any active profiles
// In this case includeDefaultProfileData will be true or the active profile will
// be default
// In the case where includeDefaultProfileData is false we only want to include
// the properties from the active profiles
boolean includeDataEntry = includeDefaultProfileData
|| Arrays.asList(environment.getActiveProfiles()).contains("default");

// the order here is important, first has to come "application.yaml" and then
// "application-dev.yaml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package org.springframework.cloud.kubernetes.configserver;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -41,11 +40,11 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {

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

private CoreV1Api coreApi;
private final CoreV1Api coreApi;

private List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers;
private final List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers;

private String namespace;
private final String namespace;

public KubernetesEnvironmentRepository(CoreV1Api coreApi,
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers, String namespace) {
Expand All @@ -64,8 +63,7 @@ public Environment findOne(String application, String profile, String label, boo
if (!StringUtils.hasText(profile)) {
profile = "default";
}
List<String> profiles = new java.util.ArrayList<>(
Arrays.stream(StringUtils.commaDelimitedListToStringArray(profile)).toList());
List<String> profiles = new ArrayList<>(List.of(StringUtils.commaDelimitedListToStringArray(profile)));

Collections.reverse(profiles);
if (!profiles.contains("default")) {
Expand Down Expand Up @@ -98,8 +96,7 @@ public Environment findOne(String application, String profile, String label, boo
}

private MutablePropertySources createPropertySources(String application) {
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put("spring.application.name", application);
Map<String, Object> applicationProperties = Map.of("spring.application.name", application);
MapPropertySource propertySource = new MapPropertySource("kubernetes-config-server", applicationProperties);
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(propertySource);
Expand All @@ -108,7 +105,7 @@ private MutablePropertySources createPropertySources(String application) {

private void addApplicationConfiguration(Environment environment, StandardEnvironment springEnv,
String applicationName) {
kubernetesPropertySourceSuppliers.stream().forEach(supplier -> {
kubernetesPropertySourceSuppliers.forEach(supplier -> {
List<MapPropertySource> propertySources = supplier.get(coreApi, applicationName, namespace, springEnv);
propertySources.forEach(propertySource -> {
if (propertySource.getPropertyNames().length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.springframework.cloud.kubernetes.configserver;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import io.kubernetes.client.openapi.apis.CoreV1Api;
Expand All @@ -33,11 +31,18 @@ public interface KubernetesPropertySourceSupplier {

List<MapPropertySource> get(CoreV1Api coreV1Api, String name, String namespace, Environment environment);

/*
* return either a List containing 'currentNamespace' (if 'namespacesString' is empty
* or null), or a List of comma delimited tokens (namespaces) from 'namespacesString'.
*
* 'currentNamespace' can be treated logically as the "default namespace" to use, if
* the other argument is not provided.
*/
static List<String> namespaceSplitter(String namespacesString, String currentNamespace) {
List<String> namespaces = Collections.singletonList(currentNamespace);
List<String> namespaces = List.of(currentNamespace);
String[] namespacesArray = StringUtils.commaDelimitedListToStringArray(namespacesString);
if (namespacesArray.length > 0) {
namespaces = Arrays.asList(namespacesArray);
namespaces = List.of(namespacesArray);
}
return namespaces;
}
Expand Down

0 comments on commit cfa1b0b

Please sign in to comment.