Skip to content

Fix 1757 #1914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 6, 2025
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 @@ -34,11 +34,10 @@ class LabeledConfigMapContextToSourceDataProvider implements Supplier<Kubernetes
* There could be many sources that are read based on incoming labels, for which we
* will be computing a single Map<String, Object> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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;

/**
* @author wind57
Expand Down Expand Up @@ -318,10 +319,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");
}
Expand Down Expand Up @@ -445,11 +446,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");

}

Expand Down Expand Up @@ -523,25 +523,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");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;

/**
* @author wind57
Expand Down Expand Up @@ -292,10 +293,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");
}
Expand Down Expand Up @@ -383,8 +384,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");

}
Expand Down Expand Up @@ -459,21 +460,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");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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();
Expand All @@ -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");
}

/**
* <pre>
* 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.
* </pre>
*/
// 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");
}

}

This file was deleted.

Loading
Loading