From c697df4264aeb90f1498e55ea953208ce812cbd0 Mon Sep 17 00:00:00 2001 From: wind57 Date: Tue, 1 Apr 2025 22:46:56 +0300 Subject: [PATCH 1/8] started some basic work Signed-off-by: wind57 --- .../commons/config/ConfigUtils.java | 4 ++- .../commons/config/PrefixContext.java | 2 +- .../commons/config/ConfigUtilsTests.java | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 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 95cf263616..b43b3617cc 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 @@ -323,7 +323,9 @@ public static MultipleSourcesContainer processLabeledData(List dataFromOneSource = SourceDataEntriesProcessor.processAllEntries(rawData, environment); + result.put(foundSourceName, dataFromOneSource); }); return new MultipleSourcesContainer(sourceNames, result); diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java index 93282e2c19..d94a3812b9 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java @@ -25,6 +25,6 @@ * * @author wind57 */ -public final record PrefixContext(Map data, String prefix, String namespace, +public record PrefixContext(Map data, String prefix, String namespace, Set propertySourceNames) { } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java index 0a51d74751..66e66c6e65 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java @@ -28,6 +28,8 @@ import org.springframework.mock.env.MockEnvironment; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author wind57 */ @@ -222,4 +224,29 @@ void testKeysWithPrefixNonEmptyPrefix() { Assertions.assertEquals(Map.of("prefix-a", "b", "prefix-c", "d"), result); } + @Test + void testIssue1757() { + + StrippedSourceContainer containerA = new StrippedSourceContainer(Map.of("load", "true"), "client-1", + Map.of("client-id", "clientA", "client-secret", "a")); + + StrippedSourceContainer containerB = new StrippedSourceContainer(Map.of("load", "true"), "client-2", + Map.of("client-id", "clientB", "client-secret", "b")); + + MultipleSourcesContainer container = ConfigUtils.processLabeledData(List.of(containerA, containerB), + new MockEnvironment(), Map.of("load", "true"), "default", Set.of(), false); + + System.out.println(container); + assertThat(container.names()).containsExactly("client-1", "client-2"); + + Map client1Data = (Map) container.data().get("client-1"); + assertThat(client1Data) + .containsExactlyInAnyOrderEntriesOf(Map.of("client-id", "clientA", "client-secret", "a")); + + Map client2Data = (Map) container.data().get("client-2"); + assertThat(client2Data) + .containsExactlyInAnyOrderEntriesOf(Map.of("client-id", "clientB", "client-secret", "b")); + + } + } From 8a43f03a16c45c017c60b4a95209c1ce2858aa20 Mon Sep 17 00:00:00 2001 From: wind57 Date: Tue, 8 Apr 2025 23:42:50 +0300 Subject: [PATCH 2/8] dirty Signed-off-by: wind57 --- .../kubernetes/commons/config/MultipleSourcesContainer.java | 2 ++ .../cloud/kubernetes/commons/config/ConfigUtilsTests.java | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java index 892171f758..7d2d4feaf8 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java @@ -24,6 +24,8 @@ * * 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. + * @apiNote in a future major release, instead of taking two arguments as input, we will + * take only one: {@code LinkedHashMap> } */ public record MultipleSourcesContainer(LinkedHashSet names, Map data) { diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java index f14f89415d..c826889a01 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java @@ -226,6 +226,7 @@ void testKeysWithPrefixNonEmptyPrefix() { } @Test + @SuppressWarnings("unchecked") void testIssue1757() { StrippedSourceContainer containerA = new StrippedSourceContainer(Map.of("load", "true"), "client-1", @@ -238,7 +239,7 @@ void testIssue1757() { new MockEnvironment(), Map.of("load", "true"), "default", Set.of(), false); System.out.println(container); - assertThat(container.names()).containsExactly("client-1", "client-2"); + assertThat(container.names()).containsExactlyInAnyOrder("client-1", "client-2"); Map client1Data = (Map) container.data().get("client-1"); assertThat(client1Data) From 65b701111e5ada2d0d922b087c9724b8931573d8 Mon Sep 17 00:00:00 2001 From: wind57 Date: Sun, 27 Apr 2025 21:51:42 +0300 Subject: [PATCH 3/8] first test Signed-off-by: wind57 --- ...dConfigMapContextToSourceDataProvider.java | 7 +- ...igMapContextToSourceDataProviderTests.java | 39 ++-- ...ecretContextToSourceDataProviderTests.java | 32 ++-- ...igMapContextToSourceDataProviderTests.java | 12 +- ...ecretContextToSourceDataProviderTests.java | 5 +- .../LabeledConfigMapWithProfileApp.java | 7 +- .../LabeledConfigMapWithProfileTests.java | 74 +++++--- ...LabeledConfigMapWithProfileController.java | 46 ----- .../properties/Green.java | 34 +--- .../properties/GreenK8s.java | 34 ++++ .../properties/GreenProd.java | 34 ++++ .../properties/GreenPurple.java | 34 ++++ .../properties/GreenPurpleK8s.java | 34 ++++ .../LabeledSecretWithProfileApp.java | 7 +- .../LabeledSecretWithProfileTests.java | 71 +++++--- .../LabeledSecretWithProfileController.java | 46 ----- .../properties/Green.java | 32 +--- .../properties/GreenK8s.java | 34 ++++ .../properties/GreenProd.java | 34 ++++ .../properties/GreenPurple.java | 34 ++++ .../properties/GreenPurpleK8s.java | 34 ++++ .../commons/config/ConfigUtils.java | 53 +++--- .../commons/config/LabeledSourceData.java | 72 +++++--- .../config/MultipleSourcesContainer.java | 2 - .../commons/config/NamedSourceData.java | 63 +++++-- .../commons/config/PrefixContext.java | 2 + .../commons/config/SourceDataFlattener.java | 79 ++++++++ .../config/ConfigUtilsProcessSourceTests.java | 28 ++- .../commons/config/ConfigUtilsTests.java | 45 ++--- .../config/SourceDataFlattenerTests.java | 169 ++++++++++++++++++ .../config/Fabric8ConfigUtilsTests.java | 73 ++++++-- ...igMapContextToSourceDataProviderTests.java | 40 ++--- ...ecretContextToSourceDataProviderTests.java | 30 ++-- .../LabeledConfigMapWithProfile.java | 85 ++++++--- .../LabeledConfigMapWithProfileApp.java | 7 +- ...LabeledConfigMapWithProfileController.java | 46 ----- .../properties/Green.java | 34 +--- .../properties/GreenK8s.java | 34 ++++ .../properties/GreenProd.java | 34 ++++ .../properties/GreenPurple.java | 34 ++++ .../properties/GreenPurpleK8s.java | 34 ++++ .../LabeledSecretWithProfile.java | 95 +++++++--- .../LabeledSecretWithProfileApp.java | 7 +- .../LabeledSecretWithProfileController.java | 46 ----- .../properties/Green.java | 34 +--- .../properties/GreenPurple.java | 34 ++++ .../properties/GreenPurpleK8s.java | 34 ++++ .../properties/GreenSecretK8s.java | 34 ++++ .../properties/GreenSecretProd.java | 34 ++++ 49 files changed, 1332 insertions(+), 634 deletions(-) delete mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenK8s.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenProd.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurple.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurpleK8s.java delete mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenK8s.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenProd.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurple.java create mode 100644 spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurpleK8s.java create mode 100644 spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java create mode 100644 spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java delete mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenK8s.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenProd.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurple.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurpleK8s.java delete mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurple.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurpleK8s.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretK8s.java create mode 100644 spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretProd.java diff --git a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProvider.java b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProvider.java index 6d4381ffd2..054fb767f1 100644 --- a/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProvider.java +++ b/spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProvider.java @@ -34,11 +34,10 @@ class LabeledConfigMapContextToSourceDataProvider implements Supplier in the end. * - * If there is no config maps found for the provided labels, we will return an "empty" - * SourceData. Its name is going to be the concatenated labels mapped to an empty Map. + * If there are no config maps found for the provided labels, we will return an + * "empty" SourceData. Its name is going to be the concatenated labels mapped to an + * empty Map. * - * If we find config maps(s) for the provided labels, its name is going to be the - * concatenated names mapped to the data they hold as a Map. */ @Override public KubernetesClientContextToSourceData get() { diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java index 66b1211e11..de3f803632 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java @@ -50,6 +50,8 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author wind57 */ @@ -318,10 +320,10 @@ void testTwoConfigmapsWithPrefix() { String secondKey = keys.next(); if (firstKey.contains("first")) { - Assertions.assertThat(firstKey).isEqualTo("another-blue-configmap.blue-configmap.first"); + Assertions.assertThat(firstKey).isEqualTo("blue-configmap.first"); } - Assertions.assertThat(secondKey).isEqualTo("another-blue-configmap.blue-configmap.second"); + Assertions.assertThat(secondKey).isEqualTo("another-blue-configmap.second"); Assertions.assertThat(properties.get(firstKey)).isEqualTo("blue"); Assertions.assertThat(properties.get(secondKey)).isEqualTo("blue"); } @@ -445,11 +447,10 @@ void searchWithLabelsOneConfigMapFoundAndOneFromProfileFound() { KubernetesClientContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2); - Assertions.assertThat(sourceData.sourceData().get("color-configmap.color-configmap-k8s.one")).isEqualTo("1"); - Assertions.assertThat(sourceData.sourceData().get("color-configmap.color-configmap-k8s.two")).isEqualTo("2"); - Assertions.assertThat(sourceData.sourceName()) - .isEqualTo("configmap.color-configmap.color-configmap-k8s.default"); + assertThat(sourceData.sourceData().size()).isEqualTo(2); + assertThat(sourceData.sourceData().get("color-configmap.one")).isEqualTo("1"); + assertThat(sourceData.sourceData().get("color-configmap-k8s.two")).isEqualTo("2"); + assertThat(sourceData.sourceName()).isEqualTo("configmap.color-configmap.color-configmap-k8s.default"); } @@ -523,25 +524,13 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() { KubernetesClientContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(4); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.one")) - .isEqualTo("1"); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.two")) - .isEqualTo("2"); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.four")) - .isEqualTo("4"); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.five")) - .isEqualTo("5"); + assertThat(sourceData.sourceData().size()).isEqualTo(4); + assertThat(sourceData.sourceData().get("color-configmap.one")).isEqualTo("1"); + assertThat(sourceData.sourceData().get("shape-configmap.two")).isEqualTo("2"); + assertThat(sourceData.sourceData().get("color-configmap-k8s.four")).isEqualTo("4"); + assertThat(sourceData.sourceData().get("shape-configmap-k8s.five")).isEqualTo("5"); - Assertions.assertThat(sourceData.sourceName()) + assertThat(sourceData.sourceName()) .isEqualTo("configmap.color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.default"); } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java index 946691eb22..ebe8a26f99 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java @@ -52,6 +52,8 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author wind57 */ @@ -292,10 +294,10 @@ void testTwoSecretsWithPrefix() { String secondKey = keys.next(); if (firstKey.contains("first")) { - Assertions.assertThat(firstKey).isEqualTo("another-blue-secret.blue-secret.first"); + Assertions.assertThat(firstKey).isEqualTo("blue-secret.first"); } - Assertions.assertThat(secondKey).isEqualTo("another-blue-secret.blue-secret.second"); + Assertions.assertThat(secondKey).isEqualTo("another-blue-secret.second"); Assertions.assertThat(properties.get(firstKey)).isEqualTo("blue"); Assertions.assertThat(properties.get(secondKey)).isEqualTo("blue"); } @@ -383,8 +385,8 @@ void searchWithLabelsOneSecretFoundAndOneFromProfileFound() { SourceData sourceData = data.apply(context); Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2); - Assertions.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.one")).isEqualTo("1"); - Assertions.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.two")).isEqualTo("2"); + Assertions.assertThat(sourceData.sourceData().get("color-secret.one")).isEqualTo("1"); + Assertions.assertThat(sourceData.sourceData().get("color-secret-k8s.two")).isEqualTo("2"); Assertions.assertThat(sourceData.sourceName()).isEqualTo("secret.color-secret.color-secret-k8s.default"); } @@ -459,21 +461,13 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() { KubernetesClientContextToSourceData data = new LabeledSecretContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(4); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.one")) - .isEqualTo("1"); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.two")) - .isEqualTo("2"); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.four")) - .isEqualTo("4"); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.five")) - .isEqualTo("5"); - - Assertions.assertThat(sourceData.sourceName()) + assertThat(sourceData.sourceData().size()).isEqualTo(4); + assertThat(sourceData.sourceData().get("color-secret.one")).isEqualTo("1"); + assertThat(sourceData.sourceData().get("shape-secret.two")).isEqualTo("2"); + assertThat(sourceData.sourceData().get("color-secret-k8s.four")).isEqualTo("4"); + assertThat(sourceData.sourceData().get("shape-secret-k8s.five")).isEqualTo("5"); + + assertThat(sourceData.sourceName()) .isEqualTo("secret.color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.default"); } 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 e32e01abc3..3a4f193e30 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 @@ -169,18 +169,18 @@ void matchIncludeSingleProfile() { CoreV1Api api = new CoreV1Api(); NormalizedSource source = new NamedConfigMapNormalizedSource(RED_CONFIG_MAP_NAME, NAMESPACE, true, - ConfigUtils.Prefix.DEFAULT, true, true); + ConfigUtils.Prefix.DEFAULT, true); MockEnvironment environment = new MockEnvironment(); - environment.setActiveProfiles("with-profile"); + environment.addActiveProfile("with-profile"); KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment, - false); + true); KubernetesClientContextToSourceData data = new NamedConfigMapContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); - Assertions.assertThat(sourceData.sourceName()).isEqualTo("configmap.red.red-with-profile.default.with-profile"); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(1); - Assertions.assertThat(sourceData.sourceData().get("taste")).isEqualTo("mango"); + Assertions.assertThat(sourceData.sourceName()).isEqualTo("configmap.red.red-with-profile.default"); + Assertions.assertThat(sourceData.sourceData()) + .containsExactlyInAnyOrderEntriesOf(Map.of("color", "really-red", "taste", "mango")); } 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 5e259f9d6b..af7ed9042a 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 @@ -229,14 +229,15 @@ void matchIncludeSingleProfile() { MockEnvironment environment = new MockEnvironment(); environment.addActiveProfile("with-profile"); KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment, - false); + true); KubernetesClientContextToSourceData data = new NamedSecretContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); Assertions.assertThat(sourceData.sourceName()).isEqualTo("secret.red.red-with-profile.default.with-profile"); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(1); + Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2); Assertions.assertThat(sourceData.sourceData().get("taste")).isEqualTo("mango"); + Assertions.assertThat(sourceData.sourceData().get("color")).isEqualTo("really-red"); } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java index 6135608b5d..c32136846d 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java @@ -21,12 +21,17 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Blue; import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenK8s; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenProd; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurpleK8s; /** * @author wind57 */ @SpringBootApplication -@EnableConfigurationProperties({ Blue.class, Green.class }) +@EnableConfigurationProperties({ Blue.class, Green.class, GreenK8s.class, GreenProd.class, GreenPurple.class, + GreenPurpleK8s.class }) public class LabeledConfigMapWithProfileApp { public static void main(String[] args) { diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileTests.java index cc4b6798b2..6c58afa250 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileTests.java @@ -17,14 +17,21 @@ package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile; import com.github.tomakehurst.wiremock.client.WireMock; -import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Blue; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenK8s; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenProd; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurpleK8s; import org.springframework.test.web.reactive.server.WebTestClient; +import static org.assertj.core.api.Assertions.assertThat; + /** * Stub data is in * {@link org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.LabeledConfigMapWithProfileConfigurationStub} @@ -36,6 +43,24 @@ abstract class LabeledConfigMapWithProfileTests { @Autowired private WebTestClient webClient; + @Autowired + private Blue blue; + + @Autowired + private Green green; + + @Autowired + private GreenK8s greenK8s; + + @Autowired + private GreenProd greenProd; + + @Autowired + private GreenPurple greenPurple; + + @Autowired + private GreenPurpleK8s greenPurpleK8s; + @AfterEach void afterEach() { WireMock.reset(); @@ -55,32 +80,35 @@ static void afterAll() { */ @Test void testBlue() { - this.webClient.get() - .uri("/labeled-configmap/profile/blue") - .exchange() - .expectStatus() - .isOk() - .expectBody(String.class) - .value(Matchers.equalTo("1")); + assertThat(blue.getOne()).isEqualTo("1"); } - /** - *
-	 *   this one is taken from : ""green-configmap.green-configmap-k8s.green-configmap-prod.green-purple-configmap.green-purple-configmap-k8s"".
-	 *   We find "green-configmap" by labels, also "green-configmap-k8s", "green-configmap-prod" exists,
-	 *   because "includeProfileSpecificSources=true" is set. Also "green-purple-configmap" and "green-purple-configmap-k8s"
-	 *   are found.
-	 * 
- */ + // found by labels @Test void testGreen() { - this.webClient.get() - .uri("/labeled-configmap/profile/green") - .exchange() - .expectStatus() - .isOk() - .expectBody(String.class) - .value(Matchers.equalTo("2#6#7#eight-ish")); + assertThat(green.getTwo()).isEqualTo("2"); + } + + // found because above is found, plus active profile is included + @Test + void testGreenK8s() { + assertThat(greenK8s.getSix()).isEqualTo("6"); + } + + // found because above is found, plus active profile is included + @Test + void testGreenProd() { + assertThat(greenProd.getSeven()).isEqualTo("7"); + } + + @Test + void testGreenPurple() { + assertThat(greenPurple.getEight()).isEqualTo("8"); + } + + @Test + void testGreenPurpleK8s() { + assertThat(greenPurpleK8s.getEight()).isEqualTo("eight-ish"); } } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java deleted file mode 100644 index e81f6678b4..0000000000 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.controller; - -import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Blue; -import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Green; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class LabeledConfigMapWithProfileController { - - private final Blue blue; - - private final Green green; - - public LabeledConfigMapWithProfileController(Blue blue, Green green) { - this.blue = blue; - this.green = green; - } - - @GetMapping("/labeled-configmap/profile/blue") - public String blue() { - return blue.getOne(); - } - - @GetMapping("/labeled-configmap/profile/green") - public String green() { - return green.getTwo() + "#" + green.getSix() + "#" + green.getSeven() + "#" + green.getEight(); - } - -} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/Green.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/Green.java index 7e8807f411..79da152a67 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/Green.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/Green.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 the original author or authors. + * Copyright 2013-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,17 +18,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; -@ConfigurationProperties("green-configmap.green-configmap-k8s.green-configmap-prod.green-purple-configmap.green-purple-configmap-k8s") +@ConfigurationProperties("green-configmap") public class Green { private String two; - private String six; - - private String seven; - - private String eight; - public String getTwo() { return two; } @@ -37,28 +31,4 @@ public void setTwo(String two) { this.two = two; } - public String getSix() { - return six; - } - - public void setSix(String six) { - this.six = six; - } - - public String getSeven() { - return seven; - } - - public void setSeven(String seven) { - this.seven = seven; - } - - public String getEight() { - return eight; - } - - public void setEight(String eight) { - this.eight = eight; - } - } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenK8s.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenK8s.java new file mode 100644 index 0000000000..3007e2b45a --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-configmap-k8s") +public class GreenK8s { + + private String six; + + public String getSix() { + return six; + } + + public void setSix(String six) { + this.six = six; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenProd.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenProd.java new file mode 100644 index 0000000000..10d0ac4b53 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenProd.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-configmap-prod") +public class GreenProd { + + private String seven; + + public String getSeven() { + return seven; + } + + public void setSeven(String seven) { + this.seven = seven; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurple.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurple.java new file mode 100644 index 0000000000..e3a3ffb023 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurple.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-configmap") +public class GreenPurple { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurpleK8s.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurpleK8s.java new file mode 100644 index 0000000000..743deae16f --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/properties/GreenPurpleK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-configmap-k8s") +public class GreenPurpleK8s { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileApp.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileApp.java index eae8cd695a..0e448ec145 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileApp.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileApp.java @@ -21,9 +21,14 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.Blue; import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenK8s; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenProd; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenPurpleK8s; @SpringBootApplication -@EnableConfigurationProperties({ Blue.class, Green.class }) +@EnableConfigurationProperties({ Blue.class, Green.class, GreenK8s.class, GreenProd.class, GreenPurple.class, + GreenPurpleK8s.class }) public class LabeledSecretWithProfileApp { public static void main(String[] args) { diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileTests.java index b66dab4a1c..941dfb97d6 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileTests.java @@ -17,13 +17,19 @@ package org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile; import com.github.tomakehurst.wiremock.client.WireMock; -import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.Blue; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenK8s; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenProd; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.GreenPurpleK8s; + +import static org.assertj.core.api.Assertions.assertThat; /* *
@@ -46,7 +52,22 @@
 abstract class LabeledSecretWithProfileTests {
 
 	@Autowired
-	private WebTestClient webClient;
+	private Blue blue;
+
+	@Autowired
+	private Green green;
+
+	@Autowired
+	private GreenK8s greenK8s;
+
+	@Autowired
+	private GreenProd greenProd;
+
+	@Autowired
+	private GreenPurple greenPurple;
+
+	@Autowired
+	private GreenPurpleK8s greenPurpleK8s;
 
 	@AfterEach
 	public void afterEach() {
@@ -67,32 +88,32 @@ static void afterAll() {
 	 */
 	@Test
 	void testBlue() {
-		this.webClient.get()
-			.uri("/labeled-secret/profile/blue")
-			.exchange()
-			.expectStatus()
-			.isOk()
-			.expectBody(String.class)
-			.value(Matchers.equalTo("1"));
+		assertThat(blue.getOne()).isEqualTo("1");
 	}
 
-	/**
-	 * 
-	 *   this one is taken from : "green-purple-secret.green-purple-secret-k8s.green-secret.green-secret-k8s.green-secret-prod".
-	 *   We find "green-secret" by labels, also "green-secrets-k8s" and "green-secrets-prod" exists,
-	 *   because "includeProfileSpecificSources=true" is set. Also "green-purple-secret" and "green-purple-secret-k8s"
-	 * 	 are found.
-	 * 
- */ @Test void testGreen() { - this.webClient.get() - .uri("/labeled-secret/profile/green") - .exchange() - .expectStatus() - .isOk() - .expectBody(String.class) - .value(Matchers.equalTo("2#6#7#eight-ish")); + assertThat(green.getTwo()).isEqualTo("2"); + } + + @Test + void testGreenK8s() { + assertThat(greenK8s.getSix()).isEqualTo("6"); + } + + @Test + void testGreenProd() { + assertThat(greenProd.getSeven()).isEqualTo("7"); + } + + @Test + void testGreenPurple() { + assertThat(greenPurple.getEight()).isEqualTo("8"); + } + + @Test + void testGreenPurpleK8s() { + assertThat(greenPurpleK8s.getEight()).isEqualTo("eight-ish"); } } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java deleted file mode 100644 index 97f6a06ebc..0000000000 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.controller; - -import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.Blue; -import org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties.Green; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class LabeledSecretWithProfileController { - - private final Blue blue; - - private final Green green; - - public LabeledSecretWithProfileController(Blue blue, Green green) { - this.blue = blue; - this.green = green; - } - - @GetMapping("/labeled-secret/profile/blue") - public String blue() { - return blue.getOne(); - } - - @GetMapping("/labeled-secret/profile/green") - public String green() { - return green.getTwo() + "#" + green.getSix() + "#" + green.getSeven() + "#" + green.getEight(); - } - -} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/Green.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/Green.java index 851f229ed4..f36bbdfa09 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/Green.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/Green.java @@ -18,17 +18,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; -@ConfigurationProperties("green-purple-secret.green-purple-secret-k8s.green-secret.green-secret-k8s.green-secret-prod") +@ConfigurationProperties("green-secret") public class Green { private String two; - private String six; - - private String seven; - - private String eight; - public String getTwo() { return two; } @@ -37,28 +31,4 @@ public void setTwo(String two) { this.two = two; } - public String getSix() { - return six; - } - - public void setSix(String six) { - this.six = six; - } - - public String getSeven() { - return seven; - } - - public void setSeven(String seven) { - this.seven = seven; - } - - public String getEight() { - return eight; - } - - public void setEight(String eight) { - this.eight = eight; - } - } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenK8s.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenK8s.java new file mode 100644 index 0000000000..781d02bfc4 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-secret-k8s") +public class GreenK8s { + + private String six; + + public String getSix() { + return six; + } + + public void setSix(String six) { + this.six = six; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenProd.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenProd.java new file mode 100644 index 0000000000..689ea8fc26 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenProd.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-secret-prod") +public class GreenProd { + + private String seven; + + public String getSeven() { + return seven; + } + + public void setSeven(String seven) { + this.seven = seven; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurple.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurple.java new file mode 100644 index 0000000000..c659a6de8f --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurple.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-secret") +public class GreenPurple { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurpleK8s.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurpleK8s.java new file mode 100644 index 0000000000..8e6cdc1e14 --- /dev/null +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/properties/GreenPurpleK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client.config.applications.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-secret-k8s") +public class GreenPurpleK8s { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} 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 b43b3617cc..05fd1f166c 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 @@ -158,10 +158,11 @@ public static void onException(boolean failFast, Exception e) { } /* - * this method will return a SourceData that has a name in the form : + * 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 withPrefix = CollectionUtils.newHashMap(context.data().size()); context.data().forEach((key, value) -> withPrefix.put(context.prefix() + "." + key, value)); @@ -189,7 +190,7 @@ public static MultipleSourcesContainer processNamedData(List foundSourceNames = new LinkedHashSet<>(); Map data = new HashMap<>(); - // this is an ordered stream, and it means that non-profile based sources will be - // processed before profile based sources. This way, we replicate that - // "application-dev.yaml" - // overrides properties from "application.yaml" + // This is an ordered stream, and it means that non-profile-based sources will be + // processed before profile-based sources. + // This way, we replicate that "application-dev.yaml" overrides properties from + // "application.yaml" sourceNames.forEach(sourceName -> { StrippedSourceContainer stripped = hashByName.get(sourceName); if (stripped != null) { @@ -219,14 +220,16 @@ public static MultipleSourcesContainer processNamedData(List processedData = SourceDataEntriesProcessor.processAllEntries( + rawData == null ? Map.of() : rawData, environment, includeDefaultProfileData); + data.put(sourceName, processedData); } } else { @@ -270,13 +273,17 @@ static BooleanSupplier rawDataContainsProfileBasedSource(List activeProf .anyMatch(activeProfile -> ENDS_WITH_PROFILE_AND_EXTENSION.test(keyName, activeProfile))); } + static String sourceDataName(String target, LinkedHashSet sourceNames, String namespace) { + String sortedNames = sourceNames.stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); + return sourceName(target, sortedNames, namespace); + } + /** - * transforms raw data from one or multiple sources into an entry of source names and + * Transforms raw data from one or multiple sources into an entry of source names and * flattened data that they all hold (potentially overriding entries without any * defined order). This method first searches by labels, find the sources, then uses - * these names to find any profile based sources. + * these names to find any profile-based sources. */ - public static MultipleSourcesContainer processLabeledData(List containers, Environment environment, Map labels, String namespace, Set profiles, boolean decode) { @@ -288,7 +295,7 @@ public static MultipleSourcesContainer processLabeledData(List sourceNamesByLabelsWithProfile = new ArrayList<>(); if (profiles != null && !profiles.isEmpty()) { for (StrippedSourceContainer one : byLabels) { @@ -299,7 +306,7 @@ public static MultipleSourcesContainer processLabeledData(List byProfile = containers.stream() @@ -312,7 +319,7 @@ public static MultipleSourcesContainer processLabeledData(List sourceNames = new LinkedHashSet<>(); - Map result = new HashMap<>(); + Map data = new HashMap<>(); all.forEach(source -> { String foundSourceName = source.name(); @@ -325,10 +332,10 @@ public static MultipleSourcesContainer processLabeledData(List dataFromOneSource = SourceDataEntriesProcessor.processAllEntries(rawData, environment); - result.put(foundSourceName, dataFromOneSource); + data.put(foundSourceName, dataFromOneSource); }); - return new MultipleSourcesContainer(sourceNames, result); + return new MultipleSourcesContainer(sourceNames, data); } private static Map decodeData(Map data) { @@ -392,7 +399,7 @@ public static final class Prefix { /** * prefix is known at the callsite. */ - public static Prefix KNOWN; + public static Prefix KNOWN = new Prefix(() -> "", "KNOWN"); public Supplier prefixProvider() { return prefixProvider; @@ -411,6 +418,10 @@ private static void computeKnown(Supplier supplier) { KNOWN = new Prefix(supplier, "KNOWN"); } + String getName() { + return name; + } + public String toString() { return new ToStringCreator(this).append("name", name).toString(); } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java index 428949d427..5a18a096dc 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java @@ -17,6 +17,7 @@ package org.springframework.cloud.kubernetes.commons.config; import java.util.Arrays; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -24,9 +25,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.Prefix; import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException; +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.sourceDataName; +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.sourceName; import static org.springframework.cloud.kubernetes.commons.config.Constants.ERROR_PROPERTY; import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR; +import static org.springframework.cloud.kubernetes.commons.config.SourceDataFlattener.defaultFlattenedSourceData; +import static org.springframework.cloud.kubernetes.commons.config.SourceDataFlattener.nameFlattenedSourceData; +import static org.springframework.cloud.kubernetes.commons.config.SourceDataFlattener.prefixFlattenedSourceData; /** * @author wind57 @@ -38,10 +45,11 @@ public abstract class LabeledSourceData { private static final Log LOG = LogFactory.getLog(LabeledSourceData.class); - public final SourceData compute(Map labels, ConfigUtils.Prefix prefix, String target, - boolean profileSources, boolean failFast, String namespace, String[] activeProfiles) { + public final SourceData compute(Map labels, Prefix prefix, String target, boolean profileSources, + boolean failFast, String namespace, String[] activeProfiles) { MultipleSourcesContainer data = MultipleSourcesContainer.empty(); + String sourceDataName; try { Set profiles = Set.of(); @@ -50,43 +58,49 @@ public final SourceData compute(Map labels, ConfigUtils.Prefix p } data = dataSupplier(labels, profiles); - // need this check because when there is no data, the name of the property - // source is using provided labels, - // unlike when the data is present: when we use secret names - if (data.names().isEmpty()) { - String names = labels.keySet() - .stream() - .sorted() - .collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); - return SourceData.emptyRecord(ConfigUtils.sourceName(target, names, namespace)); + LinkedHashSet sourceNames = data.names(); + Map sourceDataForSourceName = data.data(); + sourceDataName = sourceDataName(target, sourceNames, namespace); + + if (sourceNames.isEmpty()) { + return emptySourceData(labels, target, namespace); + } + + if (prefix.getName().equals(Prefix.DEFAULT.getName())) { + return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceNames, sourceDataForSourceName)); + } + + if (prefix.getName().equals(Prefix.KNOWN.getName())) { + return new SourceData(sourceDataName, + prefixFlattenedSourceData(sourceNames, sourceDataForSourceName, prefix.prefixProvider().get())); } - if (prefix != ConfigUtils.Prefix.DEFAULT) { - - String prefixToUse; - if (prefix == ConfigUtils.Prefix.KNOWN) { - prefixToUse = prefix.prefixProvider().get(); - } - else { - prefixToUse = data.names() - .stream() - .sorted() - .collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); - } - - PrefixContext prefixContext = new PrefixContext(data.data(), prefixToUse, namespace, data.names()); - return ConfigUtils.withPrefix(target, prefixContext); + if (prefix.getName().equals(Prefix.DELAYED.getName())) { + return new SourceData(sourceDataName, nameFlattenedSourceData(sourceNames, sourceDataForSourceName)); } + + throw new IllegalArgumentException("Unsupported prefix: " + prefix); } catch (Exception e) { LOG.warn("Failure in reading labeled sources"); onException(failFast, e); - data = new MultipleSourcesContainer(data.names(), Map.of(ERROR_PROPERTY, "true")); + return new SourceData(sourceDataName(target, data.names(), namespace), Map.of(ERROR_PROPERTY, "true")); } - String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); - return new SourceData(ConfigUtils.sourceName(target, names, namespace), data.data()); + } + /* + * When there is no data, the name of the property source is made from provided + * labels, unlike when the data is present: when we use secret names. + */ + private SourceData emptySourceData(Map labels, String target, String namespace) { + String sourceName = labels.keySet() + .stream() + .sorted() + .collect(Collectors.collectingAndThen(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR), + sortedLabels -> sourceName(target, sortedLabels, namespace))); + + return SourceData.emptyRecord(sourceName); } /** diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java index 7d2d4feaf8..892171f758 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java @@ -24,8 +24,6 @@ * * 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. - * @apiNote in a future major release, instead of taking two arguments as input, we will - * take only one: {@code LinkedHashMap> } */ public record MultipleSourcesContainer(LinkedHashSet names, Map data) { diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java index 34da831a86..315ef63ec2 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java @@ -23,9 +23,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.Prefix; import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException; +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.sourceDataName; +import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.sourceName; import static org.springframework.cloud.kubernetes.commons.config.Constants.ERROR_PROPERTY; import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR; +import static org.springframework.cloud.kubernetes.commons.config.SourceDataFlattener.defaultFlattenedSourceData; +import static org.springframework.cloud.kubernetes.commons.config.SourceDataFlattener.nameFlattenedSourceData; +import static org.springframework.cloud.kubernetes.commons.config.SourceDataFlattener.prefixFlattenedSourceData; /** * @author wind57 @@ -37,47 +43,68 @@ public abstract class NamedSourceData { private static final Log LOG = LogFactory.getLog(NamedSourceData.class); - public final SourceData compute(String sourceName, ConfigUtils.Prefix prefix, String target, boolean profileSources, + public final SourceData compute(String sourceName, Prefix prefix, String target, boolean profileSources, boolean failFast, String namespace, String[] activeProfiles) { - LinkedHashSet sourceNames = new LinkedHashSet<>(); - // first comes non-profile based source - sourceNames.add(sourceName); + // first comes a non-profile-based source + LinkedHashSet sourceNamesToSearchFor = new LinkedHashSet<>(); + sourceNamesToSearchFor.add(sourceName); MultipleSourcesContainer data = MultipleSourcesContainer.empty(); + String sourceDataName; try { if (profileSources) { for (String activeProfile : activeProfiles) { - // add all profile based sources _after_ non-profile based one - sourceNames.add(sourceName + "-" + activeProfile); + sourceNamesToSearchFor.add(sourceName + "-" + activeProfile); } } - data = dataSupplier(sourceNames); + data = dataSupplier(sourceNamesToSearchFor); + Map sourceDataForSourceName = data.data(); + LinkedHashSet sourceNamesFound = data.names(); + String sortedNames = data.names() + .stream() + .sorted() + .collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); + sourceDataName = generateSourceName(target, sortedNames, namespace, activeProfiles); if (data.names().isEmpty()) { - String emptySourceName = ConfigUtils.sourceName(target, sourceName, namespace); - LOG.debug("Will return empty source with name : " + emptySourceName); - return SourceData.emptyRecord(emptySourceName); + return emptySourceData(target, sourceName, namespace); } - if (prefix != ConfigUtils.Prefix.DEFAULT) { - // since we are in a named source, calling get on the supplier is safe - String prefixToUse = prefix.prefixProvider().get(); - PrefixContext prefixContext = new PrefixContext(data.data(), prefixToUse, namespace, data.names()); - return ConfigUtils.withPrefix(target, prefixContext); + if (prefix.getName().equals(Prefix.DEFAULT.getName())) { + return new SourceData(sourceDataName, + defaultFlattenedSourceData(sourceNamesFound, sourceDataForSourceName)); } + if (prefix.getName().equals(Prefix.KNOWN.getName())) { + return new SourceData(sourceDataName, prefixFlattenedSourceData(sourceNamesFound, + sourceDataForSourceName, prefix.prefixProvider().get())); + } + + if (prefix.getName().equals(Prefix.DELAYED.getName())) { + return new SourceData(sourceDataName, + nameFlattenedSourceData(sourceNamesFound, sourceDataForSourceName)); + } + + throw new IllegalArgumentException("Unsupported prefix: " + prefix); + } catch (Exception e) { LOG.warn("Failure in reading named sources"); onException(failFast, e); - data = new MultipleSourcesContainer(data.names(), Map.of(ERROR_PROPERTY, "true")); + String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); + return new SourceData(generateSourceName(target, names, namespace, activeProfiles), + Map.of(ERROR_PROPERTY, "true")); } - String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); - return new SourceData(generateSourceName(target, names, namespace, activeProfiles), data.data()); + } + + private SourceData emptySourceData(String target, String sourceName, String namespace) { + String emptySourceName = sourceName(target, sourceName, namespace); + LOG.debug("Will return empty source with name : " + emptySourceName); + return SourceData.emptyRecord(emptySourceName); } protected String generateSourceName(String target, String sourceName, String namespace, String[] activeProfiles) { diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java index d94a3812b9..720998e34a 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java @@ -24,7 +24,9 @@ * config map. * * @author wind57 + * @deprecated will be deleted in a future release. */ +@Deprecated(forRemoval = true) public record PrefixContext(Map data, String prefix, String namespace, Set propertySourceNames) { } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java new file mode 100644 index 0000000000..4a768be30f --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java @@ -0,0 +1,79 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.commons.config; + +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; + +/** + * @author wind57 + */ +final class SourceDataFlattener { + + private SourceDataFlattener() { + + } + + /** + * Flattens the data from rawData without any additional processing. + */ + static Map defaultFlattenedSourceData(LinkedHashSet names, Map rawData) { + Map flattenedData = new HashMap<>(); + + names.forEach(name -> { + @SuppressWarnings("unchecked") + Map singleDataEntry = (Map) rawData.get(name); + flattenedData.putAll(singleDataEntry); + }); + + return flattenedData; + } + + /** + * Flattens the data from rawData by adding a prefix for each key. + */ + static Map prefixFlattenedSourceData(LinkedHashSet names, Map rawData, + String prefix) { + Map flattenedData = new HashMap<>(); + + names.forEach(name -> { + @SuppressWarnings("unchecked") + Map singleDataEntry = (Map) rawData.get(name); + singleDataEntry.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 nameFlattenedSourceData(LinkedHashSet names, Map rawData) { + Map flattenedData = new HashMap<>(); + + names.forEach(name -> { + @SuppressWarnings("unchecked") + Map singleDataEntry = (Map) rawData.get(name); + singleDataEntry.forEach((key, value) -> flattenedData.put(name + "." + key, value)); + }); + + return flattenedData; + } + +} diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java index 27734b1b8f..ec18f1ca03 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java @@ -81,7 +81,10 @@ void testProcessNamedDataOne() { namespace, decode, includeDefaultProfileData); Assertions.assertThat(result).isNotNull(); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("configmap-a"); - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1")); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("configmap-a"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1")); } /** @@ -173,7 +176,9 @@ void testProcessNamedDataTwo(CapturedOutput output) { * since 'k8s' is not an active profile. *
*/ - Assertions.assertThat(result.data()) + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("account"); + Assertions.assertThat(data) .containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "three", "3", "five", "5")); Assertions.assertThat(output.getOut()).contains("entry : account-k8s.properties will be skipped"); } @@ -269,7 +274,9 @@ void testProcessNamedDataThree(CapturedOutput output) { * 6. we do not have 'four=4' since we do not read 'account-k8s.properties' * */ - Assertions.assertThat(result.data()) + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("account-default"); + Assertions.assertThat(data) .containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "three", "3", "five", "5")); Assertions.assertThat(output.getOut()).contains("entry : account-k8s.properties will be skipped"); @@ -368,12 +375,13 @@ void testProcessNamedDataFive(CapturedOutput output) { Assertions.assertThat(result).isNotNull(); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account"); - /** - *
-		 * 		- we only read from 'account-k8s.properties'
-		 * 
+ /* + *
 - we only read from 'account-k8s.properties' 
*/ - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "four", "4")); + + @SuppressWarnings("unchecked") + Map accountData = (Map) result.data().get("account"); + Assertions.assertThat(accountData).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "four", "4")); Assertions.assertThat(output.getOut()).contains("entry : account.properties will be skipped"); Assertions.assertThat(output.getOut()).contains("entry : account-default.properties will be skipped"); @@ -473,7 +481,9 @@ void testProcessNamedDataSix(CapturedOutput output) { * (because 'prod' is not an active profile) * */ - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "five", "5")); + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("account-k8s"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "five", "5")); Assertions.assertThat(output.getOut()).contains("entry : account-prod.properties will be skipped"); Assertions.assertThat(output.getOut()).contains("entry : account.properties will be skipped"); Assertions.assertThat(output.getOut()).contains("entry : account-default.properties will be skipped"); diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java index c826889a01..6a40e4f0cc 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java @@ -140,41 +140,13 @@ void testUseIncludeProfileSpecificSourcesSourcesOverridesDefault() { Assertions.assertThat(ConfigUtils.includeProfileSpecificSources(true, false)).isFalse(); } - @Test - void testWithPrefix() { - PrefixContext context = new PrefixContext(Map.of("a", "b", "c", "d"), "prefix", "namespace", - Set.of("name1", "name2")); - - SourceData result = ConfigUtils.withPrefix("configmap", context); - - Assertions.assertThat(result.sourceName()).isEqualTo("configmap.name1.name2.namespace"); - - Assertions.assertThat(result.sourceData().get("prefix.a")).isEqualTo("b"); - Assertions.assertThat(result.sourceData().get("prefix.c")).isEqualTo("d"); - } - - /* - * source names should be reproducible all the time, this test asserts this. - */ - @Test - void testWithPrefixSortedName() { - PrefixContext context = new PrefixContext(Map.of("a", "b", "c", "d"), "prefix", "namespace", - Set.of("namec", "namea", "nameb")); - - SourceData result = ConfigUtils.withPrefix("configmap", context); - Assertions.assertThat(result.sourceName()).isEqualTo("configmap.namea.nameb.namec.namespace"); - - Assertions.assertThat(result.sourceData().get("prefix.a")).isEqualTo("b"); - Assertions.assertThat(result.sourceData().get("prefix.c")).isEqualTo("d"); - } - /** *
 	 *
 	 *     - we have configmap-one with an application.yaml with two properties propA = A, prop = B
 	 *     - we have configmap-one-kubernetes with an application.yaml with two properties propA = AA, probC = C
 	 *
-	 *     As a result we should get three properties as output.
+	 *     As a result we should get two keys with 2 properties each.
 	 *
 	 * 
*/ @@ -193,10 +165,17 @@ void testMerge() { MultipleSourcesContainer result = ConfigUtils.processNamedData(List.of(configMapOne, configMapOneK8s), new MockEnvironment(), sourceNames, "default", false); - Assertions.assertThat(result.data().size()).isEqualTo(3); - Assertions.assertThat(result.data().get("propA")).isEqualTo("AA"); - Assertions.assertThat(result.data().get("propB")).isEqualTo("B"); - Assertions.assertThat(result.data().get("propC")).isEqualTo("C"); + Assertions.assertThat(result.data().size()).isEqualTo(2); + @SuppressWarnings("unchecked") + Map one = (Map) result.data().get("configmap-one"); + Assertions.assertThat(one.get("propA")).isEqualTo("A"); + Assertions.assertThat(one.get("propB")).isEqualTo("B"); + + @SuppressWarnings("unchecked") + Map oneKubernetes = (Map) result.data().get("configmap-one-kubernetes"); + Assertions.assertThat(oneKubernetes.get("propA")).isEqualTo("AA"); + Assertions.assertThat(oneKubernetes.get("propC")).isEqualTo("C"); + } @Test diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java new file mode 100644 index 0000000000..f03d274373 --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java @@ -0,0 +1,169 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.commons.config; + +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * @author wind57 + */ +class SourceDataFlattenerTests { + + /** + *
+	 *     - nameA -> [a, b]
+	 *     - nameB -> [c, d]
+	 * 
+ */ + @Test + void defaultFlattenedSourceDataNoOverlap() { + LinkedHashSet names = new LinkedHashSet<>(); + names.add("nameA"); + names.add("nameB"); + + Map rawData = new HashMap<>(); + rawData.put("nameA", Map.of("a", "b")); + rawData.put("nameB", Map.of("c", "d")); + + Map result = SourceDataFlattener.defaultFlattenedSourceData(names, rawData); + Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("a", "b", "c", "d")); + + } + + /** + *
+	 *     - nameA -> [a, b]
+	 *     - nameB -> [c, d]
+	 *     - nameC -> [a, w]
+	 * 
+ */ + @Test + void defaultFlattenedSourceDataOverlap() { + LinkedHashSet names = new LinkedHashSet<>(); + names.add("nameA"); + names.add("nameB"); + names.add("nameC"); + + Map rawData = new HashMap<>(); + rawData.put("nameA", Map.of("a", "b")); + rawData.put("nameB", Map.of("c", "d")); + rawData.put("nameC", Map.of("a", "w")); + + Map result = SourceDataFlattener.defaultFlattenedSourceData(names, rawData); + Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("a", "w", "c", "d")); + + } + + /** + *
+	 *     - nameA -> [a, b]
+	 *     - nameB -> [c, d]
+	 *     - prefix -> one
+	 * 
+ */ + @Test + void prefixFlattenedSourceDataNoOverlap() { + LinkedHashSet names = new LinkedHashSet<>(); + names.add("nameA"); + names.add("nameB"); + + Map rawData = new HashMap<>(); + rawData.put("nameA", Map.of("a", "b")); + rawData.put("nameB", Map.of("c", "d")); + + Map result = SourceDataFlattener.prefixFlattenedSourceData(names, rawData, "one"); + Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("one.a", "b", "one.c", "d")); + + } + + /** + *
+	 *     - nameA -> [a, b]
+	 *     - nameB -> [c, d]
+	 *     - nameC -> [a, w]
+	 *     - prefix -> one
+	 * 
+ */ + @Test + void prefixFlattenedSourceDataOverlap() { + LinkedHashSet names = new LinkedHashSet<>(); + names.add("nameA"); + names.add("nameB"); + names.add("nameC"); + + Map rawData = new HashMap<>(); + rawData.put("nameA", Map.of("a", "b")); + rawData.put("nameB", Map.of("c", "d")); + rawData.put("nameC", Map.of("a", "w")); + + Map result = SourceDataFlattener.prefixFlattenedSourceData(names, rawData, "one"); + Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("one.a", "w", "one.c", "d")); + + } + + /** + *
+	 *     - nameA -> [a, b]
+	 *     - nameB -> [c, d]
+	 * 
+ */ + @Test + void nameFlattenedSourceDataNoOverlap() { + LinkedHashSet names = new LinkedHashSet<>(); + names.add("nameA"); + names.add("nameB"); + + Map rawData = new HashMap<>(); + rawData.put("nameA", Map.of("a", "b")); + rawData.put("nameB", Map.of("c", "d")); + + Map result = SourceDataFlattener.nameFlattenedSourceData(names, rawData); + Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("nameA.a", "b", "nameB.c", "d")); + + } + + /** + *
+	 *     - nameA -> [a, b]
+	 *     - nameB -> [c, d]
+	 *     - nameC -> [a, w]
+	 * 
+ */ + @Test + void nameFlattenedSourceDataOverlap() { + LinkedHashSet names = new LinkedHashSet<>(); + names.add("nameA"); + names.add("nameB"); + names.add("nameC"); + + Map rawData = new HashMap<>(); + rawData.put("nameA", Map.of("a", "b")); + rawData.put("nameB", Map.of("c", "d")); + rawData.put("nameC", Map.of("a", "w")); + + Map result = SourceDataFlattener.nameFlattenedSourceData(names, rawData); + Assertions.assertThat(result) + .containsExactlyInAnyOrderEntriesOf(Map.of("nameA.a", "b", "nameB.c", "d", "nameC.a", "w")); + + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java index 4b579a592b..4be4ed2ea3 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java @@ -81,7 +81,10 @@ void testSecretDataByLabelsSecretFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("color", "pink"), new MockEnvironment(), Set.of()); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-secret"); - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-secret"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); } // secret "my-secret" is deployed with label {color:pink}; we search for it by same @@ -100,7 +103,10 @@ void testSecretDataByLabelsSecretFoundWithPropertyFile() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("color", "pink"), new MockEnvironment(), Set.of()); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-secret"); - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1")); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-secret"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1")); } // secrets "my-secret" and "my-secret-2" are deployed with label {color:pink}; @@ -129,8 +135,13 @@ void testSecretDataByLabelsTwoSecretsFound() { Assertions.assertThat(result.names()).contains("my-secret"); Assertions.assertThat(result.names()).contains("my-secret-2"); - Assertions.assertThat(result.data()) - .containsExactlyInAnyOrderEntriesOf(Map.of("property-2", "value-2", "property", "value")); + @SuppressWarnings("unchecked") + Map mySecretData = (Map) result.data().get("my-secret"); + Assertions.assertThat(mySecretData).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); + + @SuppressWarnings("unchecked") + Map mySecret2Data = (Map) result.data().get("my-secret-2"); + Assertions.assertThat(mySecret2Data).containsExactlyInAnyOrderEntriesOf(Map.of("property-2", "value-2")); } /** @@ -195,8 +206,18 @@ void testSecretDataByLabelsThreeSecretsFound() { Assertions.assertThat(result.names()).contains("blue-square-secret"); Assertions.assertThat(result.names()).contains("blue-square-secret-k8s"); - Assertions.assertThat(result.data()) - .containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "four", "4")); + @SuppressWarnings("unchecked") + Map dataBlueSecret = (Map) result.data().get("blue-circle-secret"); + Assertions.assertThat(dataBlueSecret).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1")); + + @SuppressWarnings("unchecked") + Map dataSquareSecret = (Map) result.data().get("blue-square-secret"); + Assertions.assertThat(dataSquareSecret).containsExactlyInAnyOrderEntriesOf(Map.of("two", "2")); + + @SuppressWarnings("unchecked") + Map dataSquareSecretK8s = (Map) result.data().get("blue-square-secret-k8s"); + Assertions.assertThat(dataSquareSecretK8s).containsExactlyInAnyOrderEntriesOf(Map.of("four", "4")); + } // secret "my-secret" is deployed; we search for it by name and do not find it. @@ -229,7 +250,10 @@ void testSecretDataByNameSecretFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByName(client, "spring-k8s", names, new MockEnvironment()); Assertions.assertThat(result.names().size()).isEqualTo(1); - Assertions.assertThat(result.data().get("property")).isEqualTo("value"); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-secret"); + Assertions.assertThat(data.get("property")).isEqualTo("value"); } // secrets "my-secret" and "my-secret-2" are deployed; @@ -259,8 +283,14 @@ void testSecretDataByNameTwoSecretsFound() { Assertions.assertThat(result.names()).contains("my-secret-2"); Assertions.assertThat(result.data().size()).isEqualTo(2); - Assertions.assertThat(result.data().get("property")).isEqualTo("value"); - Assertions.assertThat(result.data().get("property-2")).isEqualTo("value-2"); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-secret"); + Assertions.assertThat(data.get("property")).isEqualTo("value"); + + @SuppressWarnings("unchecked") + Map data2 = (Map) result.data().get("my-secret-2"); + Assertions.assertThat(data2.get("property-2")).isEqualTo("value-2"); } // config-map "my-config-map" is deployed without any data; we search for it by name @@ -278,7 +308,10 @@ void testConfigMapsDataByNameFoundNoData() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-config-map"); - Assertions.assertThat(result.data()).isEmpty(); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-config-map"); + Assertions.assertThat(data).isEmpty(); } // config-map "my-config-map" is deployed; we search for it and do not find it. @@ -313,7 +346,10 @@ void testConfigMapDataByNameFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-config-map"); - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-config-map"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); } // config-map "my-config-map" is deployed; we search for it and find it. @@ -333,7 +369,10 @@ void testConfigMapDataByNameFoundWithPropertyFile() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-config-map"); - Assertions.assertThat(result.data()).containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1")); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-config-map"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1")); } // config-map "my-config-map" and "my-config-map-2" are deployed; @@ -364,8 +403,14 @@ void testConfigMapDataByNameTwoFound() { Assertions.assertThat(result.names()).contains("my-config-map-2"); Assertions.assertThat(result.data().size()).isEqualTo(2); - Assertions.assertThat(result.data().get("property")).isEqualTo("value"); - Assertions.assertThat(result.data().get("property-2")).isEqualTo("value-2"); + + @SuppressWarnings("unchecked") + Map data = (Map) result.data().get("my-config-map"); + Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); + + @SuppressWarnings("unchecked") + Map data2 = (Map) result.data().get("my-config-map-2"); + Assertions.assertThat(data2).containsExactlyInAnyOrderEntriesOf(Map.of("property-2", "value-2")); } @Test diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledConfigMapContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledConfigMapContextToSourceDataProviderTests.java index 46906685b9..88c8e93dc8 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledConfigMapContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledConfigMapContextToSourceDataProviderTests.java @@ -40,6 +40,8 @@ import org.springframework.cloud.kubernetes.commons.config.SourceData; import org.springframework.mock.env.MockEnvironment; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author wind57 */ @@ -292,10 +294,10 @@ void testTwoConfigmapsWithPrefix() { String secondKey = keys.next(); if (firstKey.contains("first")) { - Assertions.assertThat(firstKey).isEqualTo("another-blue-configmap.blue-configmap.first"); + Assertions.assertThat(firstKey).isEqualTo("blue-configmap.first"); } - Assertions.assertThat(secondKey).isEqualTo("another-blue-configmap.blue-configmap.second"); + Assertions.assertThat(secondKey).isEqualTo("another-blue-configmap.second"); Assertions.assertThat(properties.get(firstKey)).isEqualTo("blue"); Assertions.assertThat(properties.get(secondKey)).isEqualTo("blue"); } @@ -410,11 +412,10 @@ void searchWithLabelsOneConfigMapFoundAndOneFromProfileFound() { Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2); - Assertions.assertThat(sourceData.sourceData().get("color-configmap.color-configmap-k8s.one")).isEqualTo("1"); - Assertions.assertThat(sourceData.sourceData().get("color-configmap.color-configmap-k8s.two")).isEqualTo("2"); - Assertions.assertThat(sourceData.sourceName()) - .isEqualTo("configmap.color-configmap.color-configmap-k8s.default"); + assertThat(sourceData.sourceData().size()).isEqualTo(2); + assertThat(sourceData.sourceData().get("color-configmap.one")).isEqualTo("1"); + assertThat(sourceData.sourceData().get("color-configmap-k8s.two")).isEqualTo("2"); + assertThat(sourceData.sourceName()).isEqualTo("configmap.color-configmap.color-configmap-k8s.default"); } @@ -480,25 +481,14 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() { Fabric8ContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get(); SourceData sourceData = data.apply(context); - Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(4); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.one")) - .isEqualTo("1"); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.two")) - .isEqualTo("2"); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.four")) - .isEqualTo("4"); - Assertions - .assertThat(sourceData.sourceData() - .get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.five")) - .isEqualTo("5"); + assertThat(sourceData.sourceData().size()).isEqualTo(4); - Assertions.assertThat(sourceData.sourceName()) + assertThat(sourceData.sourceData().get("color-configmap.one")).isEqualTo("1"); + assertThat(sourceData.sourceData().get("shape-configmap.two")).isEqualTo("2"); + assertThat(sourceData.sourceData().get("color-configmap-k8s.four")).isEqualTo("4"); + assertThat(sourceData.sourceData().get("shape-configmap-k8s.five")).isEqualTo("5"); + + assertThat(sourceData.sourceName()) .isEqualTo("configmap.color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.default"); } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledSecretContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledSecretContextToSourceDataProviderTests.java index 22964de0c8..8b394fad9f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledSecretContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/LabeledSecretContextToSourceDataProviderTests.java @@ -41,6 +41,8 @@ import org.springframework.cloud.kubernetes.commons.config.SourceData; import org.springframework.mock.env.MockEnvironment; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests only for the happy-path scenarios. All others are tested elsewhere. * @@ -294,10 +296,10 @@ void testTwoSecretsWithPrefix() { String secondKey = keys.next(); if (firstKey.contains("first")) { - Assertions.assertThat(firstKey).isEqualTo("another-blue-secret.blue-secret.first"); + Assertions.assertThat(firstKey).isEqualTo("blue-secret.first"); } - Assertions.assertThat(secondKey).isEqualTo("another-blue-secret.blue-secret.second"); + Assertions.assertThat(secondKey).isEqualTo("another-blue-secret.second"); Assertions.assertThat(properties.get(firstKey)).isEqualTo("blue"); Assertions.assertThat(properties.get(secondKey)).isEqualTo("blue"); } @@ -413,8 +415,8 @@ void searchWithLabelsOneSecretFoundAndOneFromProfileFound() { SourceData sourceData = data.apply(context); Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2); - Assertions.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.one")).isEqualTo("1"); - Assertions.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.two")).isEqualTo("2"); + Assertions.assertThat(sourceData.sourceData().get("color-secret.one")).isEqualTo("1"); + Assertions.assertThat(sourceData.sourceData().get("color-secret-k8s.two")).isEqualTo("2"); Assertions.assertThat(sourceData.sourceName()).isEqualTo("secret.color-secret.color-secret-k8s.default"); } @@ -482,20 +484,12 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() { SourceData sourceData = data.apply(context); Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(4); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.one")) - .isEqualTo("1"); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.two")) - .isEqualTo("2"); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.four")) - .isEqualTo("4"); - Assertions - .assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.five")) - .isEqualTo("5"); - - Assertions.assertThat(sourceData.sourceName()) + assertThat(sourceData.sourceData().get("color-secret.one")).isEqualTo("1"); + assertThat(sourceData.sourceData().get("shape-secret.two")).isEqualTo("2"); + assertThat(sourceData.sourceData().get("color-secret-k8s.four")).isEqualTo("4"); + assertThat(sourceData.sourceData().get("shape-secret-k8s.five")).isEqualTo("5"); + + assertThat(sourceData.sourceName()) .isEqualTo("secret.color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.default"); } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfile.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfile.java index 298f337e8a..e716ccad95 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfile.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfile.java @@ -22,13 +22,19 @@ import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.Blue; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenK8s; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenProd; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenPurpleK8s; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 @@ -42,7 +48,22 @@ abstract class LabeledConfigMapWithProfile { private static KubernetesClient mockClient; @Autowired - private WebTestClient webClient; + private Blue blue; + + @Autowired + private Green green; + + @Autowired + private GreenK8s greenK8s; + + @Autowired + private GreenProd greenProd; + + @Autowired + private GreenPurple greenPurple; + + @Autowired + private GreenPurpleK8s greenPurpleK8s; /** *
@@ -129,32 +150,50 @@ private static void createConfigMap(String name, Map data, Map
-	 *   this one is taken from : "green-configmap.green-configmap-k8s.green-configmap-prod.green-purple-configmap.green-purple-configmap-k8s".
-	 *   We find "green-configmap" by labels, also "green-configmap-k8s" and "green-configmap-prod" exists,
-	 *   because "includeProfileSpecificSources=true" is set. Also "green-purple-configmap" and "green-purple-configmap-k8s"
-	 * 	 are found.
-	 * 
+ * found by labels. */ @Test void testGreen() { - this.webClient.get() - .uri("/labeled-configmap/profile/green") - .exchange() - .expectStatus() - .isOk() - .expectBody(String.class) - .value(Matchers.equalTo("2#6#7#eight-ish")); + assertThat(green.getTwo()).isEqualTo("2"); + } + + /** + * we find "green-configmap" by labels, and since 'k8s' is an active profile, this one + * is taken also (includeProfileSpecificSources=true) + */ + @Test + void testGreenK8s() { + assertThat(greenK8s.getSix()).isEqualTo("6"); + } + + /** + * we find "green-configmap" by labels, and since 'prod' is an active profile, this + * one is taken also (includeProfileSpecificSources=true) + */ + @Test + void testGreenProd() { + assertThat(greenProd.getSeven()).isEqualTo("7"); + } + + /** + * found by labels. + */ + @Test + void testGreenPurple() { + assertThat(greenPurple.getEight()).isEqualTo("8"); + } + + /** + * we find "green-configmap" by labels, and since 'prod' is an active profile, this + * one is taken also (includeProfileSpecificSources=true) + */ + @Test + void testGreenPurpleK8s() { + assertThat(greenPurpleK8s.getEight()).isEqualTo("eight-ish"); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java index ca4b2d1c0a..78e4a9de4f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java @@ -21,9 +21,14 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.Blue; import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenK8s; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenProd; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.GreenPurpleK8s; @SpringBootApplication -@EnableConfigurationProperties({ Blue.class, Green.class }) +@EnableConfigurationProperties({ Blue.class, Green.class, GreenK8s.class, GreenProd.class, GreenPurple.class, + GreenPurpleK8s.class }) public class LabeledConfigMapWithProfileApp { public static void main(String[] args) { diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java deleted file mode 100644 index ecaf20c6a2..0000000000 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.controller; - -import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.Blue; -import org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties.Green; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class LabeledConfigMapWithProfileController { - - private final Blue blue; - - private final Green green; - - public LabeledConfigMapWithProfileController(Blue blue, Green green) { - this.blue = blue; - this.green = green; - } - - @GetMapping("/labeled-configmap/profile/blue") - public String blue() { - return blue.getOne(); - } - - @GetMapping("/labeled-configmap/profile/green") - public String green() { - return green.getTwo() + "#" + green.getSix() + "#" + green.getSeven() + "#" + green.getEight(); - } - -} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/Green.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/Green.java index dd5b11c774..bee03a95f9 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/Green.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/Green.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 the original author or authors. + * Copyright 2013-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,17 +18,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; -@ConfigurationProperties("green-configmap.green-configmap-k8s.green-configmap-prod.green-purple-configmap.green-purple-configmap-k8s") +@ConfigurationProperties("green-configmap") public class Green { private String two; - private String six; - - private String seven; - - private String eight; - public String getTwo() { return two; } @@ -37,28 +31,4 @@ public void setTwo(String two) { this.two = two; } - public String getSix() { - return six; - } - - public void setSix(String six) { - this.six = six; - } - - public String getSeven() { - return seven; - } - - public void setSeven(String seven) { - this.seven = seven; - } - - public String getEight() { - return eight; - } - - public void setEight(String eight) { - this.eight = eight; - } - } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenK8s.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenK8s.java new file mode 100644 index 0000000000..f01d5f22e7 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-configmap-k8s") +public class GreenK8s { + + private String six; + + public String getSix() { + return six; + } + + public void setSix(String six) { + this.six = six; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenProd.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenProd.java new file mode 100644 index 0000000000..2e59eef0be --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenProd.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-configmap-prod") +public class GreenProd { + + private String seven; + + public String getSeven() { + return seven; + } + + public void setSeven(String seven) { + this.seven = seven; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurple.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurple.java new file mode 100644 index 0000000000..6e319da7b5 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurple.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-configmap") +public class GreenPurple { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurpleK8s.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurpleK8s.java new file mode 100644 index 0000000000..3f3cad0f52 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_config_map_with_profile/properties/GreenPurpleK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_config_map_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-configmap-k8s") +public class GreenPurpleK8s { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfile.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfile.java index 184a8242bf..5f6f9dd4a3 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfile.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfile.java @@ -24,13 +24,19 @@ import io.fabric8.kubernetes.api.model.SecretBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.Blue; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenPurpleK8s; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenSecretK8s; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenSecretProd; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 @@ -43,7 +49,22 @@ abstract class LabeledSecretWithProfile { private static KubernetesClient mockClient; @Autowired - private WebTestClient webClient; + private Blue blue; + + @Autowired + private Green green; + + @Autowired + private GreenSecretK8s greenSecretK8s; + + @Autowired + private GreenSecretProd greenSecretProd; + + @Autowired + private GreenPurple greenPurple; + + @Autowired + private GreenPurpleK8s greenPurpleK8s; /** *
@@ -55,8 +76,8 @@ abstract class LabeledSecretWithProfile {
 	 *     - secret with name "green-secret-k8s", with labels : "{color: green-k8s}"
 	 *     - secret with name "green-secret-prod", with labels : "{color: green-prod}"
 	 *
-	 *     # a test that proves order: first read non-profile based secrets, thus profile based
-	 *     # secrets override non-profile ones.
+	 *     	a test that proves order: first read non-profile based secrets, thus profile based
+	 *     	secrets override non-profile ones.
 	 *     - secret with name "green-purple-secret", labels "{color: green, shape: round}", data: "{eight: 8}"
 	 *     - secret with name "green-purple-secret-k8s", labels "{color: black}", data: "{eight: eight-ish}"
 	 * 
@@ -139,32 +160,60 @@ private static void createSecret(String name, Map data, Map - * this one is taken from : "green-purple-secret.green-purple-secret-k8s.green-secret.green-secret-k8s.green-secret-prod". - * We find "green-secret" by labels, also "green-secrets-k8s" and "green-secrets-prod" exists, - * because "includeProfileSpecificSources=true" is set. Also "green-purple-secret" and "green-purple-secret-k8s" - * are found. + * We find "green-secret" by labels. * */ @Test void testGreen() { - this.webClient.get() - .uri("/labeled-secret/profile/green") - .exchange() - .expectStatus() - .isOk() - .expectBody(String.class) - .value(Matchers.equalTo("2#6#7#eight-ish")); + assertThat(green.getTwo()).isEqualTo("2"); + } + + /** + *
+	 *   We find "green-secret" by labels, but also "green-secrets-k8s" and because
+	 *   "includeProfileSpecificSources=true", we take it also.
+	 * 
+ */ + @Test + void testGreenK8s() { + assertThat(greenSecretK8s.getSix()).isEqualTo("6"); + } + + /** + *
+	 *   We find "green-secret" by labels, but also "green-secrets-prod" and because
+	 *   "includeProfileSpecificSources=true", we take it also.
+	 * 
+ */ + @Test + void testGreenProd() { + assertThat(greenSecretProd.getSeven()).isEqualTo("7"); + } + + /** + *
+	 *   found by labels.
+	 * 
+ */ + @Test + void testGreenPurple() { + assertThat(greenPurple.getEight()).isEqualTo("8"); + } + + /** + *
+	 *   We find "green-purple" by labels, and since 'k8s' is an active profile,
+	 *   we will also find this one.
+	 * 
+ */ + @Test + void testGreenPurpleK8s() { + assertThat(greenPurpleK8s.getEight()).isEqualTo("eight-ish"); } } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfileApp.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfileApp.java index 29b5e311d6..fb0bb1d8bf 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfileApp.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/LabeledSecretWithProfileApp.java @@ -21,9 +21,14 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.Blue; import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.Green; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenPurple; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenPurpleK8s; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenSecretK8s; +import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.GreenSecretProd; @SpringBootApplication -@EnableConfigurationProperties({ Blue.class, Green.class }) +@EnableConfigurationProperties({ Blue.class, Green.class, GreenSecretK8s.class, GreenSecretProd.class, + GreenPurple.class, GreenPurpleK8s.class }) public class LabeledSecretWithProfileApp { public static void main(String[] args) { diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java deleted file mode 100644 index f4d548c7b6..0000000000 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/controller/LabeledSecretWithProfileController.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2013-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.controller; - -import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.Blue; -import org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties.Green; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class LabeledSecretWithProfileController { - - private final Blue blue; - - private final Green green; - - public LabeledSecretWithProfileController(Blue blue, Green green) { - this.blue = blue; - this.green = green; - } - - @GetMapping("/labeled-secret/profile/blue") - public String blue() { - return blue.getOne(); - } - - @GetMapping("/labeled-secret/profile/green") - public String green() { - return green.getTwo() + "#" + green.getSix() + "#" + green.getSeven() + "#" + green.getEight(); - } - -} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/Green.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/Green.java index 4c0118fb77..e6194d5f8f 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/Green.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/Green.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2021 the original author or authors. + * Copyright 2013-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,17 +18,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; -@ConfigurationProperties("green-purple-secret.green-purple-secret-k8s.green-secret.green-secret-k8s.green-secret-prod") +@ConfigurationProperties("green-secret") public class Green { private String two; - private String six; - - private String seven; - - private String eight; - public String getTwo() { return two; } @@ -37,28 +31,4 @@ public void setTwo(String two) { this.two = two; } - public String getSix() { - return six; - } - - public void setSix(String six) { - this.six = six; - } - - public String getSeven() { - return seven; - } - - public void setSeven(String seven) { - this.seven = seven; - } - - public String getEight() { - return eight; - } - - public void setEight(String eight) { - this.eight = eight; - } - } diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurple.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurple.java new file mode 100644 index 0000000000..41269694cb --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurple.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-secret") +public class GreenPurple { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurpleK8s.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurpleK8s.java new file mode 100644 index 0000000000..27df792b87 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenPurpleK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-purple-secret-k8s") +public class GreenPurpleK8s { + + private String eight; + + public String getEight() { + return eight; + } + + public void setEight(String eight) { + this.eight = eight; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretK8s.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretK8s.java new file mode 100644 index 0000000000..2a5e1489c3 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretK8s.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-secret-k8s") +public class GreenSecretK8s { + + private String six; + + public String getSix() { + return six; + } + + public void setSix(String six) { + this.six = six; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretProd.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretProd.java new file mode 100644 index 0000000000..ba25efe918 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/labeled_secret_with_profile/properties/GreenSecretProd.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.fabric8.config.labeled_secret_with_profile.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("green-secret-prod") +public class GreenSecretProd { + + private String seven; + + public String getSeven() { + return seven; + } + + public void setSeven(String seven) { + this.seven = seven; + } + +} From 36710f96ddcc47ece1f26893a86e4766f3833dd1 Mon Sep 17 00:00:00 2001 From: wind57 Date: Sun, 27 Apr 2025 21:56:16 +0300 Subject: [PATCH 4/8] assertions Signed-off-by: wind57 --- .../integration/tests/commons/native_client/Util.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java index dccf593cc0..e25a7abb99 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java @@ -53,13 +53,13 @@ import jakarta.annotation.Nullable; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.assertj.core.api.Assertions; import org.testcontainers.k3s.K3sContainer; import org.springframework.cloud.kubernetes.integration.tests.commons.Images; import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; import static org.awaitility.Awaitility.await; -import static org.junit.jupiter.api.Assertions.fail; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pullImage; @@ -548,7 +548,7 @@ private boolean isDeploymentReady(String deploymentName, String namespace) throw V1DeploymentList deployments = appsV1Api.listNamespacedDeployment(namespace, null, null, null, "metadata.name=" + deploymentName, null, null, null, null, null, null, null); if (deployments.getItems().isEmpty()) { - fail("No deployments with the name " + deploymentName); + Assertions.fail("No deployments with the name " + deploymentName); } V1Deployment deployment = deployments.getItems().get(0); if (deployment.getStatus() != null) { @@ -580,7 +580,7 @@ private static boolean isDeploymentReadyAfterPatch(String deploymentName, String V1DeploymentList deployments = new AppsV1Api().listNamespacedDeployment(namespace, null, null, null, "metadata.name=" + deploymentName, null, null, null, null, null, null, null); if (deployments.getItems().isEmpty()) { - fail("No deployment with name " + deploymentName); + Assertions.fail("No deployment with name " + deploymentName); } V1Deployment deployment = deployments.getItems().get(0); From 51c5144fe5bf3e20d7aaaeb84d60deef6ed6d9c5 Mon Sep 17 00:00:00 2001 From: wind57 Date: Sun, 27 Apr 2025 22:01:08 +0300 Subject: [PATCH 5/8] checkstyle Signed-off-by: wind57 --- .../config/LabeledConfigMapContextToSourceDataProviderTests.java | 1 - .../config/LabeledSecretContextToSourceDataProviderTests.java | 1 - 2 files changed, 2 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java index de3f803632..deb1c28847 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java @@ -49,7 +49,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; - import static org.assertj.core.api.Assertions.assertThat; /** diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java index ebe8a26f99..6cbf1766d6 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java @@ -51,7 +51,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; - import static org.assertj.core.api.Assertions.assertThat; /** From abd7d463fac3311f8c097102b7bb68e111c536de Mon Sep 17 00:00:00 2001 From: wind57 Date: Sun, 27 Apr 2025 22:10:12 +0300 Subject: [PATCH 6/8] checkstyle Signed-off-by: wind57 --- .../kubernetes/integration/tests/commons/native_client/Util.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java index 63a3553c9c..e25a7abb99 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java @@ -59,7 +59,6 @@ import org.springframework.cloud.kubernetes.integration.tests.commons.Images; import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; -import static org.assertj.core.api.Fail.fail; import static org.awaitility.Awaitility.await; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion; From cf8b2d54b112658895f4cae3d7fec7eb5b480380 Mon Sep 17 00:00:00 2001 From: wind57 Date: Mon, 5 May 2025 16:19:35 +0300 Subject: [PATCH 7/8] fix tests Signed-off-by: wind57 --- .../kubernetes/commons/config/SourceDataFlattener.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java index 4a768be30f..2f22310750 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java @@ -37,7 +37,7 @@ static Map defaultFlattenedSourceData(LinkedHashSet name names.forEach(name -> { @SuppressWarnings("unchecked") - Map singleDataEntry = (Map) rawData.get(name); + Map singleDataEntry = (Map) rawData.getOrDefault(name, Map.of()); flattenedData.putAll(singleDataEntry); }); @@ -53,7 +53,7 @@ static Map prefixFlattenedSourceData(LinkedHashSet names names.forEach(name -> { @SuppressWarnings("unchecked") - Map singleDataEntry = (Map) rawData.get(name); + Map singleDataEntry = (Map) rawData.getOrDefault(name, Map.of()); singleDataEntry.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)); }); @@ -69,7 +69,7 @@ static Map nameFlattenedSourceData(LinkedHashSet names, names.forEach(name -> { @SuppressWarnings("unchecked") - Map singleDataEntry = (Map) rawData.get(name); + Map singleDataEntry = (Map) rawData.getOrDefault(name, Map.of()); singleDataEntry.forEach((key, value) -> flattenedData.put(name + "." + key, value)); }); From f32f29b18617157ce03dd946bdee7dba7fdad6d8 Mon Sep 17 00:00:00 2001 From: wind57 Date: Mon, 5 May 2025 17:46:28 +0300 Subject: [PATCH 8/8] checkstyle Signed-off-by: wind57 --- .../cloud/kubernetes/commons/config/NamedSourceData.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java index 315ef63ec2..f87d078336 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java @@ -25,7 +25,6 @@ import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.Prefix; import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.onException; -import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.sourceDataName; import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.sourceName; import static org.springframework.cloud.kubernetes.commons.config.Constants.ERROR_PROPERTY; import static org.springframework.cloud.kubernetes.commons.config.Constants.PROPERTY_SOURCE_NAME_SEPARATOR;