Skip to content

Commit 581d9e9

Browse files
authored
Fix properties retrieval order from configmaps (#1291)
The properties defined by the configmap "application" should be the last in the environment list, so you can always override the properties values by profile and by application Fixes #1287
1 parent 8686dc0 commit 581d9e9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/main/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public Environment findOne(String application, String profile, String label, boo
6464
try {
6565
StandardEnvironment springEnv = new StandardEnvironment();
6666
springEnv.setActiveProfiles(profiles);
67-
addApplicationConfiguration(environment, springEnv, "application");
6867
if (!"application".equalsIgnoreCase(application)) {
6968
addApplicationConfiguration(environment, springEnv, application);
7069
}
70+
addApplicationConfiguration(environment, springEnv, "application");
7171
return environment;
7272
}
7373
catch (Exception e) {

spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configserver/src/test/java/org/springframework/cloud/kubernetes/configserver/KubernetesEnvironmentRepositoryTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,29 @@ public void testStoresProfileCase() throws ApiException {
244244
});
245245
}
246246

247+
@Test
248+
public void testApplicationPropertiesAnSecretsOverride() throws ApiException {
249+
CoreV1Api coreApi = mock(CoreV1Api.class);
250+
when(coreApi.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
251+
eq(null), eq(null), eq(null), eq(null))).thenReturn(CONFIGMAP_DEFAULT_LIST);
252+
when(coreApi.listNamespacedSecret(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
253+
eq(null), eq(null), eq(null), eq(null))).thenReturn(SECRET_LIST);
254+
when(coreApi.listNamespacedConfigMap(eq("dev"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
255+
eq(null), eq(null), eq(null), eq(null))).thenReturn(CONFIGMAP_DEV_LIST);
256+
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
257+
kubernetesPropertySourceSuppliers, "default");
258+
Environment environment = environmentRepository.findOne("stores-dev", "", "");
259+
environment.getPropertySources().stream().filter(propertySource -> propertySource.getName().startsWith("configmap")).reduce((first, second) -> second).ifPresent(propertySource -> {
260+
assertThat(propertySource.getName()).isEqualTo("configmap.application.default");
261+
});
262+
environment.getPropertySources().stream().filter(propertySource -> propertySource.getName().startsWith("configmap")).findFirst().ifPresent(propertySource -> {
263+
assertThat(propertySource.getSource().get("dummy.property.int2")).isEqualTo(2);
264+
assertThat(propertySource.getSource().get("dummy.property.bool2")).isEqualTo(false);
265+
assertThat(propertySource.getSource().get("dummy.property.string2")).isEqualTo("b");
266+
});
267+
environment.getPropertySources().stream().filter(propertySource -> propertySource.getName().startsWith("secrets")).findFirst().ifPresent(propertySource -> {
268+
assertThat(propertySource.getSource().get("username")).isEqualTo("stores-dev");
269+
assertThat(propertySource.getSource().get("password")).isEqualTo("p455w0rd");
270+
});
271+
}
247272
}

0 commit comments

Comments
 (0)