Skip to content

Commit 9ed11aa

Browse files
authored
Merge pull request #2027 from wind57/fix-1715-drop-profiles-support
Fix 1715 drop profiles support for labeled sources
2 parents 0e8b588 + 0ff54f2 commit 9ed11aa

File tree

28 files changed

+158
-244
lines changed

28 files changed

+158
-244
lines changed

docs/modules/ROOT/pages/property-source-config/configmap-propertysource.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,10 @@ spring:
520520
----
521521

522522
Besides reading the `config-map-one`, Spring will also try to read `config-map-one-dev`; in this particular order. Each active profile
523-
generates such a profile aware config map.
523+
generates such a profile aware configmap.
524524

525-
Though your application should not be impacted by such a config map, it can be disabled if needed:
525+
You can disable this "profile based sources" and then `config-map-one-dev` will
526+
not be attempted:
526527

527528
[source,yaml]
528529
----
@@ -539,6 +540,9 @@ spring:
539540
includeProfileSpecificSources: false
540541
----
541542

543+
NOTE: Since version `5.0.0-M1`, `includeProfileSpecificSources` is only supported for named sources (`spring.cloud.kubernetes.sources.name=XXX`); support for labeled sources has been removed.
544+
545+
542546
Notice that just like before, there are two levels where you can specify this property: for all config maps or
543547
for individual ones; the latter having a higher priority.
544548

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,34 @@ public static Set<String> namespaces(KubernetesNamespaceProvider provider, Confi
6565
* <pre>
6666
* 1. read all secrets in the provided namespace
6767
* 2. from the above, filter the ones that we care about (filter by labels)
68-
* 3. with secret names from (2), find out if there are any profile based secrets (if profiles is not empty)
69-
* 4. concat (2) and (3) and these are the secrets we are interested in
70-
* 5. see if any of the secrets from (4) has a single yaml/properties file
71-
* 6. gather all the names of the secrets (from 4) + data they hold
68+
* 3. see if any of the secrets from (2) has a single yaml/properties file
69+
* 4. gather all the names of the secrets + data they hold
7270
* </pre>
7371
*/
7472
static MultipleSourcesContainer secretsDataByLabels(CoreV1Api coreV1Api, String namespace,
75-
Map<String, String> labels, Environment environment, Set<String> profiles) {
73+
Map<String, String> labels, Environment environment) {
7674
List<StrippedSourceContainer> strippedSecrets = strippedSecrets(coreV1Api, namespace);
7775
if (strippedSecrets.isEmpty()) {
7876
return MultipleSourcesContainer.empty();
7977
}
80-
return ConfigUtils.processLabeledData(strippedSecrets, environment, labels, namespace, profiles, DECODE);
78+
return ConfigUtils.processLabeledData(strippedSecrets, environment, labels, namespace, DECODE);
8179
}
8280

8381
/**
8482
* <pre>
8583
* 1. read all config maps in the provided namespace
8684
* 2. from the above, filter the ones that we care about (filter by labels)
87-
* 3. with config maps names from (2), find out if there are any profile based ones (if profiles is not empty)
88-
* 4. concat (2) and (3) and these are the config maps we are interested in
89-
* 5. see if any from (4) has a single yaml/properties file
90-
* 6. gather all the names of the config maps (from 4) + data they hold
85+
* 3. see if any from (2) has a single yaml/properties file
86+
* 4. gather all the names of the config maps + data they hold
9187
* </pre>
9288
*/
9389
static MultipleSourcesContainer configMapsDataByLabels(CoreV1Api coreV1Api, String namespace,
94-
Map<String, String> labels, Environment environment, Set<String> profiles) {
90+
Map<String, String> labels, Environment environment) {
9591
List<StrippedSourceContainer> strippedConfigMaps = strippedConfigMaps(coreV1Api, namespace);
9692
if (strippedConfigMaps.isEmpty()) {
9793
return MultipleSourcesContainer.empty();
9894
}
99-
return ConfigUtils.processLabeledData(strippedConfigMaps, environment, labels, namespace, profiles, DECODE);
95+
return ConfigUtils.processLabeledData(strippedConfigMaps, environment, labels, namespace, DECODE);
10096
}
10197

10298
/**

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.kubernetes.client.config;
1818

1919
import java.util.Map;
20-
import java.util.Set;
2120
import java.util.function.Supplier;
2221

2322
import org.springframework.cloud.kubernetes.commons.config.LabeledConfigMapNormalizedSource;
@@ -48,13 +47,12 @@ public KubernetesClientContextToSourceData get() {
4847

4948
return new LabeledSourceData() {
5049
@Override
51-
public MultipleSourcesContainer dataSupplier(Map<String, String> labels, Set<String> profiles) {
50+
public MultipleSourcesContainer dataSupplier(Map<String, String> labels) {
5251
return KubernetesClientConfigUtils.configMapsDataByLabels(context.client(), context.namespace(),
53-
labels, context.environment(), profiles);
52+
labels, context.environment());
5453
}
5554

56-
}.compute(source.labels(), source.prefix(), source.target(), source.profileSpecificSources(),
57-
source.failFast(), context.namespace(), context.environment().getActiveProfiles());
55+
}.compute(source.labels(), source.prefix(), source.target(), source.failFast(), context.namespace());
5856
};
5957

6058
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.kubernetes.client.config;
1818

1919
import java.util.Map;
20-
import java.util.Set;
2120
import java.util.function.Supplier;
2221

2322
import org.springframework.cloud.kubernetes.commons.config.LabeledSecretNormalizedSource;
@@ -55,13 +54,12 @@ public KubernetesClientContextToSourceData get() {
5554

5655
return new LabeledSourceData() {
5756
@Override
58-
public MultipleSourcesContainer dataSupplier(Map<String, String> labels, Set<String> profiles) {
57+
public MultipleSourcesContainer dataSupplier(Map<String, String> labels) {
5958
return KubernetesClientConfigUtils.secretsDataByLabels(context.client(), context.namespace(),
60-
labels, context.environment(), profiles);
59+
labels, context.environment());
6160
}
6261

63-
}.compute(source.labels(), source.prefix(), source.target(), source.profileSpecificSources(),
64-
source.failFast(), context.namespace(), context.environment().getActiveProfiles());
62+
}.compute(source.labels(), source.prefix(), source.target(), source.failFast(), context.namespace());
6563
};
6664
}
6765

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.junit.jupiter.api.BeforeAll;
3636
import org.junit.jupiter.api.Test;
3737

38+
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
3839
import org.springframework.cloud.kubernetes.commons.config.LabeledSecretNormalizedSource;
3940
import org.springframework.cloud.kubernetes.commons.config.NamedSecretNormalizedSource;
4041
import org.springframework.cloud.kubernetes.commons.config.NormalizedSource;
@@ -193,7 +194,8 @@ void secretLabelsTest() {
193194
Map<String, String> labels = new HashMap<>();
194195
labels.put("spring.cloud.kubernetes.secret", "true");
195196

196-
NormalizedSource source = new LabeledSecretNormalizedSource("default", labels, false, false);
197+
NormalizedSource source = new LabeledSecretNormalizedSource("default", labels, false,
198+
ConfigUtils.Prefix.DEFAULT);
197199
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, "default",
198200
new MockEnvironment());
199201

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,8 @@ void searchWithLabelsOneConfigMapFound() {
409409

410410
/**
411411
* two configmaps are deployed: "color-configmap" with label: "{color:blue}" and
412-
* "color-configmap-k8s" with label: "{color:red}". We search by "{color:blue}" and
413-
* find one configmap. Since profiles are enabled, we will also be reading
414-
* "color-configmap-k8s", even if its labels do not match provided ones.
412+
* "color-configmap-k8s" with label: "{color:blue}". We search by "{color:blue}" and
413+
* find them both.
415414
*/
416415
@Test
417416
void searchWithLabelsOneConfigMapFoundAndOneFromProfileFound() {
@@ -426,7 +425,7 @@ void searchWithLabelsOneConfigMapFoundAndOneFromProfileFound() {
426425

427426
V1ConfigMap two = new V1ConfigMapBuilder()
428427
.withMetadata(new V1ObjectMetaBuilder().withName("color-configmap-k8s")
429-
.withLabels(RED_LABEL)
428+
.withLabels(BLUE_LABEL)
430429
.withNamespace(NAMESPACE)
431430
.build())
432431
.addToData("two", "2")
@@ -491,15 +490,15 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() {
491490

492491
V1ConfigMap colorConfigmapK8s = new V1ConfigMapBuilder()
493492
.withMetadata(new V1ObjectMetaBuilder().withName("color-configmap-k8s")
494-
.withLabels(RED_LABEL)
493+
.withLabels(BLUE_LABEL)
495494
.withNamespace(NAMESPACE)
496495
.build())
497496
.addToData("four", "4")
498497
.build();
499498

500499
V1ConfigMap shapeConfigmapK8s = new V1ConfigMapBuilder()
501500
.withMetadata(new V1ObjectMetaBuilder().withName("shape-configmap-k8s")
502-
.withLabels(Map.of("shape", "triangle"))
501+
.withLabels(BLUE_LABEL)
503502
.withNamespace(NAMESPACE)
504503
.build())
505504
.addToData("five", "5")
@@ -514,7 +513,6 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() {
514513
stubCall(configMapList);
515514
CoreV1Api api = new CoreV1Api();
516515
MockEnvironment environment = new MockEnvironment();
517-
environment.setActiveProfiles("k8s");
518516

519517
NormalizedSource source = new LabeledConfigMapNormalizedSource(NAMESPACE, BLUE_LABEL, true,
520518
ConfigUtils.Prefix.DELAYED, true);

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void noMatch() {
108108

109109
// blue does not match red
110110
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE,
111-
Collections.singletonMap("color", "blue"), false, false);
111+
Collections.singletonMap("color", "blue"), false, ConfigUtils.Prefix.DEFAULT);
112112
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
113113
new MockEnvironment());
114114

@@ -136,7 +136,8 @@ void singleSecretMatchAgainstLabels() {
136136
stubCall(secretList);
137137
CoreV1Api api = new CoreV1Api();
138138

139-
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, LABELS, false, false);
139+
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, LABELS, false,
140+
ConfigUtils.Prefix.DEFAULT);
140141
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
141142
new MockEnvironment());
142143

@@ -168,7 +169,8 @@ void twoSecretsMatchAgainstLabels() {
168169
stubCall(secretList);
169170
CoreV1Api api = new CoreV1Api();
170171

171-
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, RED_LABEL, false, false);
172+
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, RED_LABEL, false,
173+
ConfigUtils.Prefix.DEFAULT);
172174
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
173175
new MockEnvironment());
174176

@@ -193,7 +195,8 @@ void namespaceMatch() {
193195
stubCall(secretList);
194196
CoreV1Api api = new CoreV1Api();
195197

196-
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE + "nope", LABELS, false, false);
198+
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE + "nope", LABELS, false,
199+
ConfigUtils.Prefix.DEFAULT);
197200
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
198201
new MockEnvironment());
199202

@@ -226,8 +229,7 @@ void testWithPrefix() {
226229
CoreV1Api api = new CoreV1Api();
227230

228231
ConfigUtils.Prefix prefix = ConfigUtils.findPrefix("me", false, false, null);
229-
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false, prefix,
230-
false);
232+
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false, prefix);
231233
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
232234
new MockEnvironment());
233235

@@ -272,7 +274,7 @@ void testTwoSecretsWithPrefix() {
272274
CoreV1Api api = new CoreV1Api();
273275

274276
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
275-
ConfigUtils.Prefix.DELAYED, false);
277+
ConfigUtils.Prefix.DELAYED);
276278
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
277279
new MockEnvironment());
278280

@@ -304,7 +306,7 @@ void testTwoSecretsWithPrefix() {
304306
/**
305307
* two secrets are deployed: secret "color-secret" with label: "{color:blue}" and
306308
* "shape-secret" with label: "{shape:round}". We search by "{color:blue}" and find
307-
* one secret. profile based sources are enabled, but it has no effect.
309+
* one secret.
308310
*/
309311
@Test
310312
void searchWithLabelsOneSecretFound() {
@@ -331,7 +333,7 @@ void searchWithLabelsOneSecretFound() {
331333
CoreV1Api api = new CoreV1Api();
332334

333335
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
334-
ConfigUtils.Prefix.DEFAULT, true);
336+
ConfigUtils.Prefix.DEFAULT);
335337
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
336338
new MockEnvironment());
337339

@@ -347,8 +349,7 @@ void searchWithLabelsOneSecretFound() {
347349
/**
348350
* two secrets are deployed: secret "color-secret" with label: "{color:blue}" and
349351
* "color-secret-k8s" with label: "{color:red}". We search by "{color:blue}" and find
350-
* one secret. Since profiles are enabled, we will also be reading "color-secret-k8s",
351-
* even if its labels do not match provided ones.
352+
* both.
352353
*/
353354
@Test
354355
void searchWithLabelsOneSecretFoundAndOneFromProfileFound() {
@@ -362,7 +363,7 @@ void searchWithLabelsOneSecretFoundAndOneFromProfileFound() {
362363
.build();
363364

364365
V1Secret shapeSecret = new V1SecretBuilder()
365-
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "red"))
366+
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "blue"))
366367
.withNamespace(NAMESPACE)
367368
.withName("color-secret-k8s")
368369
.build())
@@ -377,7 +378,7 @@ void searchWithLabelsOneSecretFoundAndOneFromProfileFound() {
377378
environment.setActiveProfiles("k8s");
378379

379380
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
380-
ConfigUtils.Prefix.DELAYED, true);
381+
ConfigUtils.Prefix.DELAYED);
381382
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment);
382383

383384
KubernetesClientContextToSourceData data = new LabeledSecretContextToSourceDataProvider().get();
@@ -427,15 +428,15 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() {
427428
.build();
428429

429430
V1Secret colorSecretK8s = new V1SecretBuilder()
430-
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "red"))
431+
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "blue"))
431432
.withNamespace(NAMESPACE)
432433
.withName("color-secret-k8s")
433434
.build())
434435
.addToData("four", "4".getBytes())
435436
.build();
436437

437438
V1Secret shapeSecretK8s = new V1SecretBuilder()
438-
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("shape", "triangle"))
439+
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "blue"))
439440
.withNamespace(NAMESPACE)
440441
.withName("shape-secret-k8s")
441442
.build())
@@ -454,7 +455,7 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() {
454455
environment.setActiveProfiles("k8s");
455456

456457
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
457-
ConfigUtils.Prefix.DELAYED, true);
458+
ConfigUtils.Prefix.DELAYED);
458459
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment);
459460

460461
KubernetesClientContextToSourceData data = new LabeledSecretContextToSourceDataProvider().get();
@@ -490,7 +491,7 @@ void testYaml() {
490491
CoreV1Api api = new CoreV1Api();
491492

492493
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
493-
ConfigUtils.Prefix.DEFAULT, true);
494+
ConfigUtils.Prefix.DEFAULT);
494495
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
495496
new MockEnvironment());
496497

@@ -535,7 +536,7 @@ void cache(CapturedOutput output) {
535536
CoreV1Api api = new CoreV1Api();
536537

537538
NormalizedSource redSource = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "red"), false,
538-
ConfigUtils.Prefix.DEFAULT, false);
539+
ConfigUtils.Prefix.DEFAULT);
539540
KubernetesClientConfigContext redContext = new KubernetesClientConfigContext(api, redSource, NAMESPACE,
540541
new MockEnvironment());
541542
KubernetesClientContextToSourceData redData = new LabeledSecretContextToSourceDataProvider().get();
@@ -547,7 +548,7 @@ void cache(CapturedOutput output) {
547548
Assertions.assertThat(output.getOut()).contains("Loaded all secrets in namespace '" + NAMESPACE + "'");
548549

549550
NormalizedSource greenSource = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "green"), false,
550-
ConfigUtils.Prefix.DEFAULT, false);
551+
ConfigUtils.Prefix.DEFAULT);
551552
KubernetesClientConfigContext greenContext = new KubernetesClientConfigContext(api, greenSource, NAMESPACE,
552553
new MockEnvironment());
553554
KubernetesClientContextToSourceData greenData = new LabeledSecretContextToSourceDataProvider().get();

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/applications/labeled_secret_with_profile/LabeledSecretWithProfileTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ static void afterAll() {
8181

8282
/**
8383
* <pre>
84-
* this one is taken from : "blue.one". We find "color-secret" by labels, and
85-
* "color-secrets-k8s" exists, but "includeProfileSpecificSources=false", thus not taken.
86-
* Since "explicitPrefix=blue", we take "blue.one"
84+
* this one is taken from : "blue.one". We find "color-secret" by labels.
8785
* </pre>
8886
*/
8987
@Test

spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/bootstrap/stubs/LabeledConfigMapWithProfileConfigurationStub.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ public ApiClient apiClient(WireMockServer wireMockServer) {
7474
* - configmap with name "green-configmap-k8s", with labels : "{color: green-k8s}"
7575
* - configmap with name "green-configmap-prod", with labels : "{color: green-prod}"
7676
*
77-
* # a test that proves order: first read non-profile based configmaps, thus profile based
78-
* # configmaps override non-profile ones.
7977
* - configmap with name "green-purple-configmap", labels "{color: green, shape: round}", data: "{eight: 8}"
80-
* - configmap with name "green-purple-configmap-k8s", labels "{color: black}", data: "{eight: eight-ish}"
78+
* - configmap with name "green-purple-configmap-k8s", labels "{color: green}", data: "{eight: eight-ish}"
8179
* </pre>
8280
*/
8381
public static void stubData() {
@@ -112,7 +110,7 @@ public static void stubData() {
112110
V1ConfigMap greenConfigMapK8s = new V1ConfigMapBuilder()
113111
.withMetadata(new V1ObjectMetaBuilder().withName("green-configmap-k8s")
114112
.withNamespace("spring-k8s")
115-
.withLabels(Map.of("color", "green-k8s"))
113+
.withLabels(Map.of("color", "green"))
116114
.build())
117115
.addToData(Collections.singletonMap("six", "6"))
118116
.build();
@@ -121,7 +119,7 @@ public static void stubData() {
121119
V1ConfigMap greenConfigMapProd = new V1ConfigMapBuilder()
122120
.withMetadata(new V1ObjectMetaBuilder().withName("green-configmap-prod")
123121
.withNamespace("spring-k8s")
124-
.withLabels(Map.of("color", "green-prod"))
122+
.withLabels(Map.of("color", "green"))
125123
.build())
126124
.addToData(Collections.singletonMap("seven", "7"))
127125
.build();
@@ -157,7 +155,7 @@ public static void stubData() {
157155
V1ConfigMap greenPurpleConfigMapK8s = new V1ConfigMapBuilder()
158156
.withMetadata(new V1ObjectMetaBuilder().withName("green-purple-configmap-k8s")
159157
.withNamespace("spring-k8s")
160-
.withLabels(Map.of("color", "black"))
158+
.withLabels(Map.of("color", "green"))
161159
.build())
162160
.addToData(Collections.singletonMap("eight", "eight-ish"))
163161
.build();

0 commit comments

Comments
 (0)