Skip to content

Commit 830a86c

Browse files
Add KubernetesConfigServerProperties support to KubernetesEnvironmentRepository and related tests
1 parent aca106c commit 830a86c

9 files changed

+111
-20
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ public class KubernetesConfigServerAutoConfiguration {
6464
@ConditionalOnMissingBean
6565
public KubernetesEnvironmentRepository kubernetesEnvironmentRepository(CoreV1Api coreV1Api,
6666
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers,
67-
KubernetesNamespaceProvider kubernetesNamespaceProvider) {
67+
KubernetesNamespaceProvider kubernetesNamespaceProvider,
68+
KubernetesConfigServerProperties kubernetesConfigServerProperties) {
6869
return new KubernetesEnvironmentRepository(coreV1Api, kubernetesPropertySourceSuppliers,
69-
kubernetesNamespaceProvider.getNamespace());
70+
kubernetesNamespaceProvider.getNamespace(), kubernetesConfigServerProperties);
7071
}
7172

7273
@Bean

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.cloud.config.environment.Environment;
2929
import org.springframework.cloud.config.environment.PropertySource;
3030
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
31+
import org.springframework.core.Ordered;
3132
import org.springframework.core.env.MapPropertySource;
3233
import org.springframework.core.env.MutablePropertySources;
3334
import org.springframework.core.env.StandardEnvironment;
@@ -36,7 +37,7 @@
3637
/**
3738
* @author Ryan Baxter
3839
*/
39-
public class KubernetesEnvironmentRepository implements EnvironmentRepository {
40+
public class KubernetesEnvironmentRepository implements EnvironmentRepository, Ordered {
4041

4142
private static final Log LOG = LogFactory.getLog(KubernetesEnvironmentRepository.class);
4243

@@ -46,11 +47,15 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {
4647

4748
private final String namespace;
4849

50+
private int order;
51+
4952
public KubernetesEnvironmentRepository(CoreV1Api coreApi,
50-
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers, String namespace) {
53+
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers, String namespace,
54+
KubernetesConfigServerProperties properties) {
5155
this.coreApi = coreApi;
5256
this.kubernetesPropertySourceSuppliers = kubernetesPropertySourceSuppliers;
5357
this.namespace = namespace;
58+
this.order = properties.getOrder();
5459
}
5560

5661
@Override
@@ -118,4 +123,13 @@ private void addApplicationConfiguration(Environment environment, StandardEnviro
118123
});
119124
}
120125

126+
@Override
127+
public int getOrder() {
128+
return this.order;
129+
}
130+
131+
public void setOrder(int order) {
132+
this.order = order;
133+
}
134+
121135
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* Factory class for creating instances of {@link KubernetesEnvironmentRepository}.
24+
*
25+
* @author Arjav Dongaonkar
2426
*/
2527
public class KubernetesEnvironmentRepositoryFactory
2628
implements EnvironmentRepositoryFactory<KubernetesEnvironmentRepository, KubernetesConfigServerProperties> {

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

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
2727

28+
/**
29+
* @author Arjav Dongaonkar
30+
*/
2831
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
2932
classes = { KubernetesConfigServerApplication.class },
3033
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",

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

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
2626

27+
/**
28+
* @author Arjav Dongaonkar
29+
*/
2730
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
2831
classes = { KubernetesConfigServerApplication.class },
2932
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",

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

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
2626

27+
/**
28+
* @author Arjav Dongaonkar
29+
*/
2730
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
2831
classes = { KubernetesConfigServerApplication.class },
2932
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void testApplicationCase() throws ApiException {
169169
eq(null), eq(null), eq(null), eq(null), eq(null)))
170170
.thenReturn(CONFIGMAP_DEV_LIST);
171171
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
172-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
172+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", new KubernetesConfigServerProperties());
173173
Environment environment = environmentRepository.findOne("application", "", "");
174174
assertThat(environment.getPropertySources().size()).isEqualTo(2);
175175
environment.getPropertySources().forEach(propertySource -> {
@@ -203,7 +203,7 @@ public void testStoresCase() throws ApiException {
203203
eq(null), eq(null), eq(null), eq(null), eq(null)))
204204
.thenReturn(SECRET_LIST);
205205
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
206-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
206+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", new KubernetesConfigServerProperties());
207207
Environment environment = environmentRepository.findOne("stores", "", "");
208208
assertThat(environment.getPropertySources().size()).isEqualTo(4);
209209
environment.getPropertySources().forEach(propertySource -> {
@@ -250,7 +250,7 @@ public void testStoresProfileCase() throws ApiException {
250250
eq(null), eq(null), eq(null), eq(null), eq(null)))
251251
.thenReturn(CONFIGMAP_DEV_LIST);
252252
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
253-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
253+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", new KubernetesConfigServerProperties());
254254
Environment environment = environmentRepository.findOne("stores", "dev", "");
255255
assertThat(environment.getPropertySources().size()).isEqualTo(6);
256256
environment.getPropertySources().forEach(propertySource -> {
@@ -314,7 +314,7 @@ public void testApplicationPropertiesAnSecretsOverride() throws ApiException {
314314
eq(null), eq(null), eq(null), eq(null), eq(null)))
315315
.thenReturn(CONFIGMAP_DEV_LIST);
316316
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
317-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
317+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", new KubernetesConfigServerProperties());
318318
Environment environment = environmentRepository.findOne("stores-dev", "", "");
319319
environment.getPropertySources()
320320
.stream()
@@ -362,7 +362,7 @@ public void testSingleConfigMapMultipleSources() throws ApiException {
362362
return propertySources;
363363
});
364364
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi, suppliers,
365-
"default");
365+
"default", new KubernetesConfigServerProperties());
366366
Environment environment = environmentRepository.findOne("storessingle", "", "");
367367
assertThat(environment.getPropertySources().size()).isEqualTo(1);
368368
assertThat(environment.getPropertySources().get(0).getName())

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
102102
.configMapPropertySourceSupplier(kubernetesConfigServerProperties);
103103

104104
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
105-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
105+
Collections.singletonList(kubernetesPropertySourceSupplier), "default",
106+
new KubernetesConfigServerProperties());
106107

107108
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
108109
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(1);
@@ -123,7 +124,8 @@ void whenExtraNamespacesAddedThenConfigMapsInCurrentNamespaceAreNotIncluded() {
123124
.configMapPropertySourceSupplier(kubernetesConfigServerProperties);
124125

125126
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
126-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
127+
Collections.singletonList(kubernetesPropertySourceSupplier), "default",
128+
new KubernetesConfigServerProperties());
127129

128130
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
129131
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(0);
@@ -144,7 +146,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllSecretsAreIncluded() {
144146
.secretsPropertySourceSupplier(kubernetesConfigServerProperties);
145147

146148
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
147-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
149+
Collections.singletonList(kubernetesPropertySourceSupplier), "default",
150+
new KubernetesConfigServerProperties());
148151

149152
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
150153
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(1);
@@ -165,7 +168,8 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
165168
.secretsPropertySourceSupplier(kubernetesConfigServerProperties);
166169

167170
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
168-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
171+
Collections.singletonList(kubernetesPropertySourceSupplier), "default",
172+
new KubernetesConfigServerProperties());
169173

170174
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
171175
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(0);

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

+68-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.kubernetes.client.openapi.models.V1SecretBuilder;
3131
import io.kubernetes.client.openapi.models.V1SecretList;
3232
import io.kubernetes.client.openapi.models.V1SecretListBuilder;
33+
import org.junit.jupiter.api.AfterEach;
3334
import org.junit.jupiter.api.BeforeAll;
3435
import org.junit.jupiter.api.Nested;
3536
import org.junit.jupiter.api.Test;
@@ -43,6 +44,7 @@
4344
import org.springframework.cloud.config.server.environment.NativeEnvironmentRepository;
4445
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigContext;
4546
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySource;
47+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapsCache;
4648
import org.springframework.cloud.kubernetes.commons.config.Constants;
4749
import org.springframework.cloud.kubernetes.commons.config.NamedConfigMapNormalizedSource;
4850
import org.springframework.cloud.kubernetes.commons.config.NormalizedSource;
@@ -60,6 +62,9 @@
6062
import static org.mockito.ArgumentMatchers.eq;
6163
import static org.mockito.Mockito.when;
6264

65+
/**
66+
* @author Arjav Dongaonkar
67+
*/
6368
public class CompositeKubernetesIntegrationTests {
6469

6570
private static final List<KubernetesPropertySourceSupplier> KUBERNETES_PROPERTY_SOURCE_SUPPLIER = new ArrayList<>();
@@ -102,6 +107,11 @@ public static void before() {
102107
});
103108
}
104109

110+
@AfterEach
111+
public void after() {
112+
new KubernetesClientConfigMapsCache().discardAll();
113+
}
114+
105115
@Nested
106116
@SpringBootTest(classes = { KubernetesConfigServerApplication.class },
107117
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.client.namespace=default",
@@ -110,7 +120,7 @@ public static void before() {
110120
"spring.cloud.kubernetes.secrets.enableApi=true" },
111121
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
112122
@ActiveProfiles({ "test", "composite", "kubernetes" })
113-
class KubernetesConfigServerTest {
123+
class KubernetesCompositeConfigServerTest {
114124

115125
@LocalServerPort
116126
private int port;
@@ -149,7 +159,7 @@ public void contextLoads() throws ApiException {
149159
"spring.cloud.kubernetes.secrets.enableApi=true" },
150160
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
151161
@ActiveProfiles({ "test", "composite", "kubernetes", "native" })
152-
class KubernetesNativeConfigServerTest {
162+
class KubernetesSecretsEnabledCompositeConfigServerTest {
153163

154164
@LocalServerPort
155165
private int port;
@@ -161,7 +171,7 @@ class KubernetesNativeConfigServerTest {
161171
private NativeEnvironmentRepository nativeEnvironmentRepository;
162172

163173
@Test
164-
public void testKubernetesAndNativeConfig() throws Exception {
174+
public void contextLoads() throws Exception {
165175
when(coreV1Api.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null),
166176
eq(null), eq(null), eq(null), eq(null), eq(null), eq(null)))
167177
.thenReturn(CONFIGMAP_DEFAULT_LIST);
@@ -201,7 +211,7 @@ public void testKubernetesAndNativeConfig() throws Exception {
201211
"spring.cloud.kubernetes.secrets.enableApi=true" },
202212
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
203213
@ActiveProfiles({ "test", "composite", "kubernetes", "native" })
204-
class KubernetesConfigMapDisabledNativeConfigServerTest {
214+
class KubernetesConfigMapDisabledCompositeConfigServerTest {
205215

206216
@LocalServerPort
207217
private int port;
@@ -213,7 +223,7 @@ class KubernetesConfigMapDisabledNativeConfigServerTest {
213223
private NativeEnvironmentRepository nativeEnvironmentRepository;
214224

215225
@Test
216-
public void testKubernetesAndNativeConfig() throws Exception {
226+
public void contextLoads() throws Exception {
217227
when(coreV1Api.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null),
218228
eq(null), eq(null), eq(null), eq(null), eq(null), eq(null)))
219229
.thenReturn(CONFIGMAP_DEFAULT_LIST);
@@ -250,7 +260,7 @@ public void testKubernetesAndNativeConfig() throws Exception {
250260
"spring.cloud.config.server.composite[1].location=file:./native-config" },
251261
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
252262
@ActiveProfiles({ "test", "composite", "kubernetes", "native" })
253-
class KubernetesSecretsDisabledNativeConfigServerTest {
263+
class KubernetesSecretsDisabledCompositeConfigServerTest {
254264

255265
@LocalServerPort
256266
private int port;
@@ -262,7 +272,7 @@ class KubernetesSecretsDisabledNativeConfigServerTest {
262272
private NativeEnvironmentRepository nativeEnvironmentRepository;
263273

264274
@Test
265-
public void testKubernetesAndNativeConfig() throws Exception {
275+
public void contextLoads() throws Exception {
266276
when(coreV1Api.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null),
267277
eq(null), eq(null), eq(null), eq(null), eq(null), eq(null)))
268278
.thenReturn(CONFIGMAP_DEFAULT_LIST);
@@ -292,4 +302,55 @@ public void testKubernetesAndNativeConfig() throws Exception {
292302

293303
}
294304

305+
@Nested
306+
@SpringBootTest(classes = { KubernetesConfigServerApplication.class },
307+
properties = { "spring.config.name:compositeconfigserver", "spring.main.cloud-platform=KUBERNETES",
308+
"spring.cloud.kubernetes.client.namespace=default",
309+
"spring.cloud.config.server.native.search-locations=file:./native-config",
310+
"spring.cloud.config.server.native.order=1", "spring.cloud.kubernetes.configserver.order=2",
311+
"spring.cloud.kubernetes.secrets.enableApi=true" },
312+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
313+
@ActiveProfiles({ "test", "native", "kubernetes" })
314+
class NativeAndKubernetesConfigServerTest {
315+
316+
@LocalServerPort
317+
private int port;
318+
319+
@MockBean
320+
private CoreV1Api coreV1Api;
321+
322+
@SpyBean
323+
private NativeEnvironmentRepository nativeEnvironmentRepository;
324+
325+
@Test
326+
public void contextLoads() throws Exception {
327+
when(coreV1Api.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null),
328+
eq(null), eq(null), eq(null), eq(null), eq(null), eq(null)))
329+
.thenReturn(CONFIGMAP_DEFAULT_LIST);
330+
when(coreV1Api.listNamespacedSecret(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null),
331+
eq(null), eq(null), eq(null), eq(null), eq(null), eq(null)))
332+
.thenReturn(SECRET_DEFAULT_LIST);
333+
334+
Environment mockNativeEnvironment = new Environment("gateway", "default");
335+
mockNativeEnvironment.add(new PropertySource("nativeProperties", Map.of("key1", "value1")));
336+
337+
when(nativeEnvironmentRepository.findOne(anyString(), anyString(), eq(null), anyBoolean()))
338+
.thenReturn(mockNativeEnvironment);
339+
340+
ResponseEntity<Environment> response = new RestTemplate().exchange(
341+
"http://localhost:" + this.port + "/gateway/default", HttpMethod.GET, null, Environment.class);
342+
343+
Environment environment = response.getBody();
344+
345+
assert environment != null;
346+
assertThat(3).isEqualTo(environment.getPropertySources().size());
347+
assertThat("nativeProperties").isEqualTo(environment.getPropertySources().get(0).getName());
348+
assertThat(environment.getPropertySources().get(1).getName().contains("configmap.gateway.default.default")
349+
&& !environment.getPropertySources().get(1).getName().contains("nativeProperties"))
350+
.isTrue();
351+
assertThat(environment.getPropertySources().get(2).getName()).contains("secret.gateway.default.default");
352+
}
353+
354+
}
355+
295356
}

0 commit comments

Comments
 (0)