Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,10 @@ spring:
----

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

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

[source,yaml]
----
Expand All @@ -539,6 +540,9 @@ spring:
includeProfileSpecificSources: false
----

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.


Notice that just like before, there are two levels where you can specify this property: for all config maps or
for individual ones; the latter having a higher priority.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,34 @@ public static Set<String> namespaces(KubernetesNamespaceProvider provider, Confi
* <pre>
* 1. read all secrets in the provided namespace
* 2. from the above, filter the ones that we care about (filter by labels)
* 3. with secret names from (2), find out if there are any profile based secrets (if profiles is not empty)
* 4. concat (2) and (3) and these are the secrets we are interested in
* 5. see if any of the secrets from (4) has a single yaml/properties file
* 6. gather all the names of the secrets (from 4) + data they hold
* 3. see if any of the secrets from (2) has a single yaml/properties file
* 4. gather all the names of the secrets + data they hold
* </pre>
*/
static MultipleSourcesContainer secretsDataByLabels(CoreV1Api coreV1Api, String namespace,
Map<String, String> labels, Environment environment, Set<String> profiles) {
Map<String, String> labels, Environment environment) {
List<StrippedSourceContainer> strippedSecrets = strippedSecrets(coreV1Api, namespace);
if (strippedSecrets.isEmpty()) {
return MultipleSourcesContainer.empty();
}
return ConfigUtils.processLabeledData(strippedSecrets, environment, labels, namespace, profiles, DECODE);
return ConfigUtils.processLabeledData(strippedSecrets, environment, labels, namespace, DECODE);
}

/**
* <pre>
* 1. read all config maps in the provided namespace
* 2. from the above, filter the ones that we care about (filter by labels)
* 3. with config maps names from (2), find out if there are any profile based ones (if profiles is not empty)
* 4. concat (2) and (3) and these are the config maps we are interested in
* 5. see if any from (4) has a single yaml/properties file
* 6. gather all the names of the config maps (from 4) + data they hold
* 3. see if any from (2) has a single yaml/properties file
* 4. gather all the names of the config maps + data they hold
* </pre>
*/
static MultipleSourcesContainer configMapsDataByLabels(CoreV1Api coreV1Api, String namespace,
Map<String, String> labels, Environment environment, Set<String> profiles) {
Map<String, String> labels, Environment environment) {
List<StrippedSourceContainer> strippedConfigMaps = strippedConfigMaps(coreV1Api, namespace);
if (strippedConfigMaps.isEmpty()) {
return MultipleSourcesContainer.empty();
}
return ConfigUtils.processLabeledData(strippedConfigMaps, environment, labels, namespace, profiles, DECODE);
return ConfigUtils.processLabeledData(strippedConfigMaps, environment, labels, namespace, DECODE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.cloud.kubernetes.client.config;

import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

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

return new LabeledSourceData() {
@Override
public MultipleSourcesContainer dataSupplier(Map<String, String> labels, Set<String> profiles) {
public MultipleSourcesContainer dataSupplier(Map<String, String> labels) {
return KubernetesClientConfigUtils.configMapsDataByLabels(context.client(), context.namespace(),
labels, context.environment(), profiles);
labels, context.environment());
}

}.compute(source.labels(), source.prefix(), source.target(), source.profileSpecificSources(),
source.failFast(), context.namespace(), context.environment().getActiveProfiles());
}.compute(source.labels(), source.prefix(), source.target(), source.failFast(), context.namespace());
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.cloud.kubernetes.client.config;

import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

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

return new LabeledSourceData() {
@Override
public MultipleSourcesContainer dataSupplier(Map<String, String> labels, Set<String> profiles) {
public MultipleSourcesContainer dataSupplier(Map<String, String> labels) {
return KubernetesClientConfigUtils.secretsDataByLabels(context.client(), context.namespace(),
labels, context.environment(), profiles);
labels, context.environment());
}

}.compute(source.labels(), source.prefix(), source.target(), source.profileSpecificSources(),
source.failFast(), context.namespace(), context.environment().getActiveProfiles());
}.compute(source.labels(), source.prefix(), source.target(), source.failFast(), context.namespace());
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
import org.springframework.cloud.kubernetes.commons.config.LabeledSecretNormalizedSource;
import org.springframework.cloud.kubernetes.commons.config.NamedSecretNormalizedSource;
import org.springframework.cloud.kubernetes.commons.config.NormalizedSource;
Expand Down Expand Up @@ -193,7 +194,8 @@ void secretLabelsTest() {
Map<String, String> labels = new HashMap<>();
labels.put("spring.cloud.kubernetes.secret", "true");

NormalizedSource source = new LabeledSecretNormalizedSource("default", labels, false, false);
NormalizedSource source = new LabeledSecretNormalizedSource("default", labels, false,
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, "default",
new MockEnvironment());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,8 @@ void searchWithLabelsOneConfigMapFound() {

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

V1ConfigMap two = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("color-configmap-k8s")
.withLabels(RED_LABEL)
.withLabels(BLUE_LABEL)
.withNamespace(NAMESPACE)
.build())
.addToData("two", "2")
Expand Down Expand Up @@ -491,15 +490,15 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() {

V1ConfigMap colorConfigmapK8s = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("color-configmap-k8s")
.withLabels(RED_LABEL)
.withLabels(BLUE_LABEL)
.withNamespace(NAMESPACE)
.build())
.addToData("four", "4")
.build();

V1ConfigMap shapeConfigmapK8s = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("shape-configmap-k8s")
.withLabels(Map.of("shape", "triangle"))
.withLabels(BLUE_LABEL)
.withNamespace(NAMESPACE)
.build())
.addToData("five", "5")
Expand All @@ -514,7 +513,6 @@ void searchWithLabelsTwoConfigMapsFoundAndOneFromProfileFound() {
stubCall(configMapList);
CoreV1Api api = new CoreV1Api();
MockEnvironment environment = new MockEnvironment();
environment.setActiveProfiles("k8s");

NormalizedSource source = new LabeledConfigMapNormalizedSource(NAMESPACE, BLUE_LABEL, true,
ConfigUtils.Prefix.DELAYED, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void noMatch() {

// blue does not match red
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE,
Collections.singletonMap("color", "blue"), false, false);
Collections.singletonMap("color", "blue"), false, ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

Expand Down Expand Up @@ -136,7 +136,8 @@ void singleSecretMatchAgainstLabels() {
stubCall(secretList);
CoreV1Api api = new CoreV1Api();

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, LABELS, false, false);
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, LABELS, false,
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

Expand Down Expand Up @@ -168,7 +169,8 @@ void twoSecretsMatchAgainstLabels() {
stubCall(secretList);
CoreV1Api api = new CoreV1Api();

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, RED_LABEL, false, false);
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, RED_LABEL, false,
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

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

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE + "nope", LABELS, false, false);
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE + "nope", LABELS, false,
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

Expand Down Expand Up @@ -226,8 +229,7 @@ void testWithPrefix() {
CoreV1Api api = new CoreV1Api();

ConfigUtils.Prefix prefix = ConfigUtils.findPrefix("me", false, false, null);
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false, prefix,
false);
NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false, prefix);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

Expand Down Expand Up @@ -272,7 +274,7 @@ void testTwoSecretsWithPrefix() {
CoreV1Api api = new CoreV1Api();

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
ConfigUtils.Prefix.DELAYED, false);
ConfigUtils.Prefix.DELAYED);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

Expand Down Expand Up @@ -304,7 +306,7 @@ void testTwoSecretsWithPrefix() {
/**
* two secrets are deployed: secret "color-secret" with label: "{color:blue}" and
* "shape-secret" with label: "{shape:round}". We search by "{color:blue}" and find
* one secret. profile based sources are enabled, but it has no effect.
* one secret.
*/
@Test
void searchWithLabelsOneSecretFound() {
Expand All @@ -331,7 +333,7 @@ void searchWithLabelsOneSecretFound() {
CoreV1Api api = new CoreV1Api();

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
ConfigUtils.Prefix.DEFAULT, true);
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

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

V1Secret shapeSecret = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "red"))
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "blue"))
.withNamespace(NAMESPACE)
.withName("color-secret-k8s")
.build())
Expand All @@ -377,7 +378,7 @@ void searchWithLabelsOneSecretFoundAndOneFromProfileFound() {
environment.setActiveProfiles("k8s");

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
ConfigUtils.Prefix.DELAYED, true);
ConfigUtils.Prefix.DELAYED);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment);

KubernetesClientContextToSourceData data = new LabeledSecretContextToSourceDataProvider().get();
Expand Down Expand Up @@ -427,15 +428,15 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() {
.build();

V1Secret colorSecretK8s = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "red"))
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "blue"))
.withNamespace(NAMESPACE)
.withName("color-secret-k8s")
.build())
.addToData("four", "4".getBytes())
.build();

V1Secret shapeSecretK8s = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("shape", "triangle"))
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("color", "blue"))
.withNamespace(NAMESPACE)
.withName("shape-secret-k8s")
.build())
Expand All @@ -454,7 +455,7 @@ void searchWithLabelsTwoSecretsFoundAndOneFromProfileFound() {
environment.setActiveProfiles("k8s");

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
ConfigUtils.Prefix.DELAYED, true);
ConfigUtils.Prefix.DELAYED);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE, environment);

KubernetesClientContextToSourceData data = new LabeledSecretContextToSourceDataProvider().get();
Expand Down Expand Up @@ -490,7 +491,7 @@ void testYaml() {
CoreV1Api api = new CoreV1Api();

NormalizedSource source = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "blue"), false,
ConfigUtils.Prefix.DEFAULT, true);
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext context = new KubernetesClientConfigContext(api, source, NAMESPACE,
new MockEnvironment());

Expand Down Expand Up @@ -535,7 +536,7 @@ void cache(CapturedOutput output) {
CoreV1Api api = new CoreV1Api();

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

NormalizedSource greenSource = new LabeledSecretNormalizedSource(NAMESPACE, Map.of("color", "green"), false,
ConfigUtils.Prefix.DEFAULT, false);
ConfigUtils.Prefix.DEFAULT);
KubernetesClientConfigContext greenContext = new KubernetesClientConfigContext(api, greenSource, NAMESPACE,
new MockEnvironment());
KubernetesClientContextToSourceData greenData = new LabeledSecretContextToSourceDataProvider().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ static void afterAll() {

/**
* <pre>
* this one is taken from : "blue.one". We find "color-secret" by labels, and
* "color-secrets-k8s" exists, but "includeProfileSpecificSources=false", thus not taken.
* Since "explicitPrefix=blue", we take "blue.one"
* this one is taken from : "blue.one". We find "color-secret" by labels.
* </pre>
*/
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ public ApiClient apiClient(WireMockServer wireMockServer) {
* - configmap with name "green-configmap-k8s", with labels : "{color: green-k8s}"
* - configmap with name "green-configmap-prod", with labels : "{color: green-prod}"
*
* # a test that proves order: first read non-profile based configmaps, thus profile based
* # configmaps override non-profile ones.
* - configmap with name "green-purple-configmap", labels "{color: green, shape: round}", data: "{eight: 8}"
* - configmap with name "green-purple-configmap-k8s", labels "{color: black}", data: "{eight: eight-ish}"
* - configmap with name "green-purple-configmap-k8s", labels "{color: green}", data: "{eight: eight-ish}"
* </pre>
*/
public static void stubData() {
Expand Down Expand Up @@ -112,7 +110,7 @@ public static void stubData() {
V1ConfigMap greenConfigMapK8s = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("green-configmap-k8s")
.withNamespace("spring-k8s")
.withLabels(Map.of("color", "green-k8s"))
.withLabels(Map.of("color", "green"))
.build())
.addToData(Collections.singletonMap("six", "6"))
.build();
Expand All @@ -121,7 +119,7 @@ public static void stubData() {
V1ConfigMap greenConfigMapProd = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("green-configmap-prod")
.withNamespace("spring-k8s")
.withLabels(Map.of("color", "green-prod"))
.withLabels(Map.of("color", "green"))
.build())
.addToData(Collections.singletonMap("seven", "7"))
.build();
Expand Down Expand Up @@ -157,7 +155,7 @@ public static void stubData() {
V1ConfigMap greenPurpleConfigMapK8s = new V1ConfigMapBuilder()
.withMetadata(new V1ObjectMetaBuilder().withName("green-purple-configmap-k8s")
.withNamespace("spring-k8s")
.withLabels(Map.of("color", "black"))
.withLabels(Map.of("color", "green"))
.build())
.addToData(Collections.singletonMap("eight", "eight-ish"))
.build();
Expand Down
Loading
Loading