From 470ab676d2019ae2a96a24a32784df3ab1760a21 Mon Sep 17 00:00:00 2001 From: erabii Date: Thu, 28 Mar 2024 19:55:43 +0200 Subject: [PATCH 1/4] simplification in config server (#1614) --- .../KubernetesClientSecretsPropertySource.java | 1 + .../KubernetesEnvironmentRepository.java | 17 +++++++---------- .../KubernetesPropertySourceSupplier.java | 13 +++++++++---- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySource.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySource.java index 642386f604..bac7a39fe5 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySource.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientSecretsPropertySource.java @@ -29,6 +29,7 @@ */ public class KubernetesClientSecretsPropertySource extends SecretsPropertySource { + @Deprecated(forRemoval = true) public KubernetesClientSecretsPropertySource(SourceData sourceData) { super(sourceData); } diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java index 3c25e7f91b..aa377751d4 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java @@ -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; @@ -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 kubernetesPropertySourceSuppliers; + private final List kubernetesPropertySourceSuppliers; - private String namespace; + private final String namespace; public KubernetesEnvironmentRepository(CoreV1Api coreApi, List kubernetesPropertySourceSuppliers, String namespace) { @@ -64,8 +63,7 @@ public Environment findOne(String application, String profile, String label, boo if (!StringUtils.hasText(profile)) { profile = "default"; } - List profiles = new java.util.ArrayList<>( - Arrays.stream(StringUtils.commaDelimitedListToStringArray(profile)).toList()); + List profiles = new ArrayList<>(List.of(StringUtils.commaDelimitedListToStringArray(profile))); Collections.reverse(profiles); if (!profiles.contains("default")) { @@ -98,8 +96,7 @@ public Environment findOne(String application, String profile, String label, boo } private MutablePropertySources createPropertySources(String application) { - Map applicationProperties = new HashMap<>(); - applicationProperties.put("spring.application.name", application); + Map applicationProperties = Map.of("spring.application.name", application); MapPropertySource propertySource = new MapPropertySource("kubernetes-config-server", applicationProperties); MutablePropertySources mutablePropertySources = new MutablePropertySources(); mutablePropertySources.addFirst(propertySource); @@ -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 propertySources = supplier.get(coreApi, applicationName, namespace, springEnv); propertySources.forEach(propertySource -> { if (propertySource.getPropertyNames().length > 0) { diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesPropertySourceSupplier.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesPropertySourceSupplier.java index a73af430dd..e2973ac2e6 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesPropertySourceSupplier.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesPropertySourceSupplier.java @@ -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; @@ -33,11 +31,18 @@ public interface KubernetesPropertySourceSupplier { List 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 namespaceSplitter(String namespacesString, String currentNamespace) { - List namespaces = Collections.singletonList(currentNamespace); + List namespaces = List.of(currentNamespace); String[] namespacesArray = StringUtils.commaDelimitedListToStringArray(namespacesString); if (namespacesArray.length > 0) { - namespaces = Arrays.asList(namespacesArray); + namespaces = List.of(namespacesArray); } return namespaces; } From fdb51efa5894f0b11536174d93981cfd39aa8ea9 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Fri, 29 Mar 2024 01:54:59 +0000 Subject: [PATCH 2/4] Bumping versions --- ...medConfigMapContextToSourceDataProviderTests.java | 3 ++- .../NamedSecretContextToSourceDataProviderTests.java | 3 ++- .../cloud/kubernetes/commons/config/ConfigUtils.java | 12 +++++++----- .../config/NamedConfigMapNormalizedSource.java | 4 ++-- .../commons/config/NamedSecretNormalizedSource.java | 4 ++-- .../commons/config/SourceDataEntriesProcessor.java | 12 ++++++++---- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedConfigMapContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedConfigMapContextToSourceDataProviderTests.java index 63bcfa994e..e59684de1d 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedConfigMapContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedConfigMapContextToSourceDataProviderTests.java @@ -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); diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedSecretContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedSecretContextToSourceDataProviderTests.java index f5f92f7b1c..75e0322e98 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedSecretContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedSecretContextToSourceDataProviderTests.java @@ -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); diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java index 00714b9a97..e517b293d4 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java @@ -205,11 +205,13 @@ public static MultipleSourcesContainer processNamedData(List source.contains("-" + p) || "default".equals(p)); @@ -225,8 +227,8 @@ public static MultipleSourcesContainer processNamedData(List rawData, String[] activeProfiles) { return rawData.keySet().stream().anyMatch( diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSource.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSource.java index 6790cca8c7..1e0d65abc6 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSource.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSource.java @@ -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; diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSource.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSource.java index 7a51a4d4fa..3d3897b4ab 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSource.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSource.java @@ -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; diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessor.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessor.java index f1dcd14d68..a73c8508f2 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessor.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessor.java @@ -104,10 +104,14 @@ record WeightedEntry(Map.Entry 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" From 5b201f0b0199001fc23c64c3c04dace1f057a5da Mon Sep 17 00:00:00 2001 From: erabii Date: Fri, 29 Mar 2024 21:37:09 +0200 Subject: [PATCH 3/4] cleanup for config server (#1615) --- .../client/config/KubernetesClientConfigUtils.java | 10 ---------- .../kubernetes/commons/config/ConfigUtils.java | 14 ++++++++------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigUtils.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigUtils.java index ef4e3eda51..21bc92b9ee 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigUtils.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigUtils.java @@ -117,16 +117,6 @@ static MultipleSourcesContainer secretsDataByName(CoreV1Api coreV1Api, String na includeDefaultProfileData); } - static MultipleSourcesContainer secretsDataByName(CoreV1Api coreV1Api, String namespace, - LinkedHashSet sourceNames, Environment environment) { - return secretsDataByName(coreV1Api, namespace, sourceNames, environment, true); - } - - static MultipleSourcesContainer configMapsDataByName(CoreV1Api coreV1Api, String namespace, - LinkedHashSet sourceNames, Environment environment) { - return configMapsDataByName(coreV1Api, namespace, sourceNames, environment, true); - } - /** *
 	 *     1. read all config maps in the provided namespace
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java
index e517b293d4..bf0b4fcbf0 100644
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java
+++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java
@@ -194,11 +194,11 @@ public static MultipleSourcesContainer processNamedData(List {
-			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 rawData = stripped.data();
 				if (decode) {
@@ -214,7 +214,8 @@ public static MultipleSourcesContainer processNamedData(List 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,
@@ -232,7 +233,8 @@ public static MultipleSourcesContainer processNamedData(List rawData, String[] activeProfiles) {
 		return rawData.keySet().stream().anyMatch(
-				key -> Arrays.stream(activeProfiles).anyMatch(p -> key.contains("-" + p) || "default".equals(p)));
+				key -> Arrays.stream(activeProfiles).anyMatch(activeProfile -> key.contains("-" + activeProfile)
+					|| "default".equals(activeProfile)));
 	}
 
 	/**

From c79ce2dec910168cf9018bb78b4bdff7c843fa80 Mon Sep 17 00:00:00 2001
From: buildmaster 
Date: Sat, 30 Mar 2024 01:55:58 +0000
Subject: [PATCH 4/4] Bumping versions

---
 .../cloud/kubernetes/commons/config/ConfigUtils.java       | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java
index bf0b4fcbf0..70f6b1ce96 100644
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java
+++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java
@@ -215,7 +215,7 @@ public static MultipleSourcesContainer processNamedData(List sourceName.endsWith("-" + activeProfile)
-									|| "default".equals(activeProfile));
+										|| "default".equals(activeProfile));
 				if (includeDefaultProfileData || containsActiveProfile
 						|| containsDataWithProfile(rawData, environment.getActiveProfiles())) {
 					data.putAll(SourceDataEntriesProcessor.processAllEntries(rawData == null ? Map.of() : rawData,
@@ -232,9 +232,8 @@ public static MultipleSourcesContainer processNamedData(List rawData, String[] activeProfiles) {
-		return rawData.keySet().stream().anyMatch(
-				key -> Arrays.stream(activeProfiles).anyMatch(activeProfile -> key.contains("-" + activeProfile)
-					|| "default".equals(activeProfile)));
+		return rawData.keySet().stream().anyMatch(key -> Arrays.stream(activeProfiles)
+				.anyMatch(activeProfile -> key.contains("-" + activeProfile) || "default".equals(activeProfile)));
 	}
 
 	/**