Skip to content

Commit e34c892

Browse files
authored
Merge pull request #1914 from wind57/fix-1757
Fix #1757
2 parents 6e1e387 + f32f29b commit e34c892

File tree

49 files changed

+1363
-636
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1363
-636
lines changed

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProvider.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ class LabeledConfigMapContextToSourceDataProvider implements Supplier<Kubernetes
3434
* There could be many sources that are read based on incoming labels, for which we
3535
* will be computing a single Map<String, Object> in the end.
3636
*
37-
* If there is no config maps found for the provided labels, we will return an "empty"
38-
* SourceData. Its name is going to be the concatenated labels mapped to an empty Map.
37+
* If there are no config maps found for the provided labels, we will return an
38+
* "empty" SourceData. Its name is going to be the concatenated labels mapped to an
39+
* empty Map.
3940
*
40-
* If we find config maps(s) for the provided labels, its name is going to be the
41-
* concatenated names mapped to the data they hold as a Map.
4241
*/
4342
@Override
4443
public KubernetesClientContextToSourceData get() {

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledConfigMapContextToSourceDataProviderTests.java

+13-25
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5050
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
5151
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
52+
import static org.assertj.core.api.Assertions.assertThat;
5253

5354
/**
5455
* @author wind57
@@ -318,10 +319,10 @@ void testTwoConfigmapsWithPrefix() {
318319
String secondKey = keys.next();
319320

320321
if (firstKey.contains("first")) {
321-
Assertions.assertThat(firstKey).isEqualTo("another-blue-configmap.blue-configmap.first");
322+
Assertions.assertThat(firstKey).isEqualTo("blue-configmap.first");
322323
}
323324

324-
Assertions.assertThat(secondKey).isEqualTo("another-blue-configmap.blue-configmap.second");
325+
Assertions.assertThat(secondKey).isEqualTo("another-blue-configmap.second");
325326
Assertions.assertThat(properties.get(firstKey)).isEqualTo("blue");
326327
Assertions.assertThat(properties.get(secondKey)).isEqualTo("blue");
327328
}
@@ -445,11 +446,10 @@ void searchWithLabelsOneConfigMapFoundAndOneFromProfileFound() {
445446
KubernetesClientContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
446447
SourceData sourceData = data.apply(context);
447448

448-
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2);
449-
Assertions.assertThat(sourceData.sourceData().get("color-configmap.color-configmap-k8s.one")).isEqualTo("1");
450-
Assertions.assertThat(sourceData.sourceData().get("color-configmap.color-configmap-k8s.two")).isEqualTo("2");
451-
Assertions.assertThat(sourceData.sourceName())
452-
.isEqualTo("configmap.color-configmap.color-configmap-k8s.default");
449+
assertThat(sourceData.sourceData().size()).isEqualTo(2);
450+
assertThat(sourceData.sourceData().get("color-configmap.one")).isEqualTo("1");
451+
assertThat(sourceData.sourceData().get("color-configmap-k8s.two")).isEqualTo("2");
452+
assertThat(sourceData.sourceName()).isEqualTo("configmap.color-configmap.color-configmap-k8s.default");
453453

454454
}
455455

@@ -523,25 +523,13 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() {
523523
KubernetesClientContextToSourceData data = new LabeledConfigMapContextToSourceDataProvider().get();
524524
SourceData sourceData = data.apply(context);
525525

526-
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(4);
527-
Assertions
528-
.assertThat(sourceData.sourceData()
529-
.get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.one"))
530-
.isEqualTo("1");
531-
Assertions
532-
.assertThat(sourceData.sourceData()
533-
.get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.two"))
534-
.isEqualTo("2");
535-
Assertions
536-
.assertThat(sourceData.sourceData()
537-
.get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.four"))
538-
.isEqualTo("4");
539-
Assertions
540-
.assertThat(sourceData.sourceData()
541-
.get("color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.five"))
542-
.isEqualTo("5");
526+
assertThat(sourceData.sourceData().size()).isEqualTo(4);
527+
assertThat(sourceData.sourceData().get("color-configmap.one")).isEqualTo("1");
528+
assertThat(sourceData.sourceData().get("shape-configmap.two")).isEqualTo("2");
529+
assertThat(sourceData.sourceData().get("color-configmap-k8s.four")).isEqualTo("4");
530+
assertThat(sourceData.sourceData().get("shape-configmap-k8s.five")).isEqualTo("5");
543531

544-
Assertions.assertThat(sourceData.sourceName())
532+
assertThat(sourceData.sourceName())
545533
.isEqualTo("configmap.color-configmap.color-configmap-k8s.shape-configmap.shape-configmap-k8s.default");
546534

547535
}

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/LabeledSecretContextToSourceDataProviderTests.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5252
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
5353
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
54+
import static org.assertj.core.api.Assertions.assertThat;
5455

5556
/**
5657
* @author wind57
@@ -292,10 +293,10 @@ void testTwoSecretsWithPrefix() {
292293
String secondKey = keys.next();
293294

294295
if (firstKey.contains("first")) {
295-
Assertions.assertThat(firstKey).isEqualTo("another-blue-secret.blue-secret.first");
296+
Assertions.assertThat(firstKey).isEqualTo("blue-secret.first");
296297
}
297298

298-
Assertions.assertThat(secondKey).isEqualTo("another-blue-secret.blue-secret.second");
299+
Assertions.assertThat(secondKey).isEqualTo("another-blue-secret.second");
299300
Assertions.assertThat(properties.get(firstKey)).isEqualTo("blue");
300301
Assertions.assertThat(properties.get(secondKey)).isEqualTo("blue");
301302
}
@@ -383,8 +384,8 @@ void searchWithLabelsOneSecretFoundAndOneFromProfileFound() {
383384
SourceData sourceData = data.apply(context);
384385

385386
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2);
386-
Assertions.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.one")).isEqualTo("1");
387-
Assertions.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.two")).isEqualTo("2");
387+
Assertions.assertThat(sourceData.sourceData().get("color-secret.one")).isEqualTo("1");
388+
Assertions.assertThat(sourceData.sourceData().get("color-secret-k8s.two")).isEqualTo("2");
388389
Assertions.assertThat(sourceData.sourceName()).isEqualTo("secret.color-secret.color-secret-k8s.default");
389390

390391
}
@@ -459,21 +460,13 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() {
459460
KubernetesClientContextToSourceData data = new LabeledSecretContextToSourceDataProvider().get();
460461
SourceData sourceData = data.apply(context);
461462

462-
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(4);
463-
Assertions
464-
.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.one"))
465-
.isEqualTo("1");
466-
Assertions
467-
.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.two"))
468-
.isEqualTo("2");
469-
Assertions
470-
.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.four"))
471-
.isEqualTo("4");
472-
Assertions
473-
.assertThat(sourceData.sourceData().get("color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.five"))
474-
.isEqualTo("5");
475-
476-
Assertions.assertThat(sourceData.sourceName())
463+
assertThat(sourceData.sourceData().size()).isEqualTo(4);
464+
assertThat(sourceData.sourceData().get("color-secret.one")).isEqualTo("1");
465+
assertThat(sourceData.sourceData().get("shape-secret.two")).isEqualTo("2");
466+
assertThat(sourceData.sourceData().get("color-secret-k8s.four")).isEqualTo("4");
467+
assertThat(sourceData.sourceData().get("shape-secret-k8s.five")).isEqualTo("5");
468+
469+
assertThat(sourceData.sourceName())
477470
.isEqualTo("secret.color-secret.color-secret-k8s.shape-secret.shape-secret-k8s.default");
478471

479472
}

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedConfigMapContextToSourceDataProviderTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,18 @@ void matchIncludeSingleProfile() {
169169
CoreV1Api api = new CoreV1Api();
170170

171171
NormalizedSource source = new NamedConfigMapNormalizedSource(RED_CONFIG_MAP_NAME, NAMESPACE, true,
172-
ConfigUtils.Prefix.DEFAULT, true, true);
172+
ConfigUtils.Prefix.DEFAULT, true);
173173
MockEnvironment environment = new MockEnvironment();
174-
environment.setActiveProfiles("with-profile");
174+
environment.addActiveProfile("with-profile");
175175
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment,
176-
false);
176+
true);
177177

178178
KubernetesClientContextToSourceData data = new NamedConfigMapContextToSourceDataProvider().get();
179179
SourceData sourceData = data.apply(context);
180180

181-
Assertions.assertThat(sourceData.sourceName()).isEqualTo("configmap.red.red-with-profile.default.with-profile");
182-
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(1);
183-
Assertions.assertThat(sourceData.sourceData().get("taste")).isEqualTo("mango");
181+
Assertions.assertThat(sourceData.sourceName()).isEqualTo("configmap.red.red-with-profile.default");
182+
Assertions.assertThat(sourceData.sourceData())
183+
.containsExactlyInAnyOrderEntriesOf(Map.of("color", "really-red", "taste", "mango"));
184184

185185
}
186186

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/NamedSecretContextToSourceDataProviderTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,15 @@ void matchIncludeSingleProfile() {
229229
MockEnvironment environment = new MockEnvironment();
230230
environment.addActiveProfile("with-profile");
231231
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment,
232-
false);
232+
true);
233233

234234
KubernetesClientContextToSourceData data = new NamedSecretContextToSourceDataProvider().get();
235235
SourceData sourceData = data.apply(context);
236236

237237
Assertions.assertThat(sourceData.sourceName()).isEqualTo("secret.red.red-with-profile.default.with-profile");
238-
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(1);
238+
Assertions.assertThat(sourceData.sourceData().size()).isEqualTo(2);
239239
Assertions.assertThat(sourceData.sourceData().get("taste")).isEqualTo("mango");
240+
Assertions.assertThat(sourceData.sourceData().get("color")).isEqualTo("really-red");
240241

241242
}
242243

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileApp.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2222
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Blue;
2323
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Green;
24+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenK8s;
25+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenProd;
26+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurple;
27+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurpleK8s;
2428

2529
/**
2630
* @author wind57
2731
*/
2832
@SpringBootApplication
29-
@EnableConfigurationProperties({ Blue.class, Green.class })
33+
@EnableConfigurationProperties({ Blue.class, Green.class, GreenK8s.class, GreenProd.class, GreenPurple.class,
34+
GreenPurpleK8s.class })
3035
public class LabeledConfigMapWithProfileApp {
3136

3237
public static void main(String[] args) {

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/LabeledConfigMapWithProfileTests.java

+51-23
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717
package org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile;
1818

1919
import com.github.tomakehurst.wiremock.client.WireMock;
20-
import org.hamcrest.Matchers;
2120
import org.junit.jupiter.api.AfterAll;
2221
import org.junit.jupiter.api.AfterEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Blue;
26+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.Green;
27+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenK8s;
28+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenProd;
29+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurple;
30+
import org.springframework.cloud.kubernetes.client.config.applications.labeled_config_map_with_profile.properties.GreenPurpleK8s;
2631
import org.springframework.test.web.reactive.server.WebTestClient;
2732

33+
import static org.assertj.core.api.Assertions.assertThat;
34+
2835
/**
2936
* Stub data is in
3037
* {@link org.springframework.cloud.kubernetes.client.config.bootstrap.stubs.LabeledConfigMapWithProfileConfigurationStub}
@@ -36,6 +43,24 @@ abstract class LabeledConfigMapWithProfileTests {
3643
@Autowired
3744
private WebTestClient webClient;
3845

46+
@Autowired
47+
private Blue blue;
48+
49+
@Autowired
50+
private Green green;
51+
52+
@Autowired
53+
private GreenK8s greenK8s;
54+
55+
@Autowired
56+
private GreenProd greenProd;
57+
58+
@Autowired
59+
private GreenPurple greenPurple;
60+
61+
@Autowired
62+
private GreenPurpleK8s greenPurpleK8s;
63+
3964
@AfterEach
4065
void afterEach() {
4166
WireMock.reset();
@@ -55,32 +80,35 @@ static void afterAll() {
5580
*/
5681
@Test
5782
void testBlue() {
58-
this.webClient.get()
59-
.uri("/labeled-configmap/profile/blue")
60-
.exchange()
61-
.expectStatus()
62-
.isOk()
63-
.expectBody(String.class)
64-
.value(Matchers.equalTo("1"));
83+
assertThat(blue.getOne()).isEqualTo("1");
6584
}
6685

67-
/**
68-
* <pre>
69-
* this one is taken from : ""green-configmap.green-configmap-k8s.green-configmap-prod.green-purple-configmap.green-purple-configmap-k8s"".
70-
* We find "green-configmap" by labels, also "green-configmap-k8s", "green-configmap-prod" exists,
71-
* because "includeProfileSpecificSources=true" is set. Also "green-purple-configmap" and "green-purple-configmap-k8s"
72-
* are found.
73-
* </pre>
74-
*/
86+
// found by labels
7587
@Test
7688
void testGreen() {
77-
this.webClient.get()
78-
.uri("/labeled-configmap/profile/green")
79-
.exchange()
80-
.expectStatus()
81-
.isOk()
82-
.expectBody(String.class)
83-
.value(Matchers.equalTo("2#6#7#eight-ish"));
89+
assertThat(green.getTwo()).isEqualTo("2");
90+
}
91+
92+
// found because above is found, plus active profile is included
93+
@Test
94+
void testGreenK8s() {
95+
assertThat(greenK8s.getSix()).isEqualTo("6");
96+
}
97+
98+
// found because above is found, plus active profile is included
99+
@Test
100+
void testGreenProd() {
101+
assertThat(greenProd.getSeven()).isEqualTo("7");
102+
}
103+
104+
@Test
105+
void testGreenPurple() {
106+
assertThat(greenPurple.getEight()).isEqualTo("8");
107+
}
108+
109+
@Test
110+
void testGreenPurpleK8s() {
111+
assertThat(greenPurpleK8s.getEight()).isEqualTo("eight-ish");
84112
}
85113

86114
}

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_config_map_with_profile/controller/LabeledConfigMapWithProfileController.java

-46
This file was deleted.

0 commit comments

Comments
 (0)