Skip to content

Commit d735a3a

Browse files
committed
Merge branch '3.1.x' into fix-1757
2 parents abd7d46 + 6e1e387 commit d735a3a

File tree

15 files changed

+414
-319
lines changed

15 files changed

+414
-319
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.cloud.kubernetes.configserver.configurations.MockConfig;
2324
import org.springframework.context.ConfigurableApplicationContext;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
@@ -28,7 +29,7 @@
2829
* @author Ryan Baxter
2930
*/
3031
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
31-
properties = { "spring.profiles.include=kubernetes,kubernetesdisabled" },
32+
properties = { "spring.profiles.include=kubernetes,kubernetesdisabled", "test.first.config.enabled=true" },
3233
classes = { KubernetesConfigServerApplication.class, MockConfig.class })
3334
class ConfigServerAutoConfigurationKubernetesDisabledTests {
3435

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.cloud.kubernetes.configserver.configurations.MockConfig;
2324
import org.springframework.context.ConfigurableApplicationContext;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.boot.test.context.SpringBootTest;
22-
import org.springframework.boot.test.mock.mockito.MockBean;
2323
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
24-
import org.springframework.context.annotation.Bean;
25-
import org.springframework.context.annotation.Configuration;
26-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
24+
import org.springframework.cloud.kubernetes.configserver.configurations.ThirdConfig;
2725

2826
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.mockito.Mockito.mock;
3027

31-
@SpringJUnitConfig
32-
@SpringBootTest
28+
@SpringBootTest(classes = ThirdConfig.class, properties = "test.third.config.enabled=true")
3329
class KubernetesEnvironmentRepositoryFactoryTests {
3430

35-
@MockBean
31+
@Autowired
3632
private KubernetesEnvironmentRepository mockRepository;
3733

3834
@Test
@@ -47,20 +43,4 @@ void testBuild() {
4743
assertThat(repository).isSameAs(mockRepository);
4844
}
4945

50-
@Configuration
51-
static class TestConfig {
52-
53-
@Bean
54-
public KubernetesEnvironmentRepository kubernetesEnvironmentRepository() {
55-
return mock(KubernetesEnvironmentRepository.class);
56-
}
57-
58-
@Bean
59-
public KubernetesEnvironmentRepositoryFactory kubernetesEnvironmentRepositoryFactory(
60-
KubernetesEnvironmentRepository kubernetesEnvironmentRepository) {
61-
return new KubernetesEnvironmentRepositoryFactory(kubernetesEnvironmentRepository);
62-
}
63-
64-
}
65-
6646
}

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigContext;
3737
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySource;
3838
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapsCache;
39+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsCache;
3940
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySource;
4041
import org.springframework.cloud.kubernetes.commons.config.ConfigUtils;
4142
import org.springframework.cloud.kubernetes.commons.config.Constants;
@@ -89,39 +90,33 @@ class KubernetesEnvironmentRepositoryTests {
8990

9091
private static final V1ConfigMapList CONFIGMAP_DEV_LIST = new V1ConfigMapList()
9192
.addItemsItem(new V1ConfigMapBuilder()
92-
.withMetadata(
93-
new V1ObjectMetaBuilder().withName("stores").withNamespace("dev").withResourceVersion("1").build())
93+
.withMetadata(new V1ObjectMetaBuilder().withName("stores").withNamespace("dev").build())
9494
.addToData(Constants.APPLICATION_YAML,
9595
"dummy:\n property:\n string2: \"dev\"\n int2: 1\n bool2: true\n")
9696
.build());
9797

9898
private static final V1SecretList SECRET_LIST = new V1SecretListBuilder()
99-
.addToItems(
100-
new V1SecretBuilder()
101-
.withMetadata(new V1ObjectMetaBuilder().withName("application")
102-
.withResourceVersion("0")
103-
.withNamespace("default")
104-
.build())
105-
.addToData("password", "p455w0rd".getBytes())
106-
.addToData("username", "user".getBytes())
107-
.build())
108-
.addToItems(new V1SecretBuilder().withMetadata(
109-
new V1ObjectMetaBuilder().withName("stores").withResourceVersion("0").withNamespace("default").build())
99+
.addToItems(new V1SecretBuilder()
100+
.withMetadata(new V1ObjectMetaBuilder().withName("application").withNamespace("default").build())
101+
.addToData("password", "p455w0rd".getBytes())
102+
.addToData("username", "user".getBytes())
103+
.build())
104+
.addToItems(new V1SecretBuilder()
105+
.withMetadata(new V1ObjectMetaBuilder().withName("stores").withNamespace("default").build())
110106
.addToData("password", "password-from-stores".getBytes())
111107
.addToData("username", "stores".getBytes())
112108
.build())
113109
.addToItems(new V1SecretBuilder()
114-
.withMetadata(new V1ObjectMetaBuilder().withName("stores-dev")
115-
.withResourceVersion("0")
116-
.withNamespace("default")
117-
.build())
110+
.withMetadata(new V1ObjectMetaBuilder().withName("stores-dev").withNamespace("default").build())
118111
.addToData("password", "password-from-stores-dev".getBytes())
119112
.addToData("username", "stores-dev".getBytes())
120113
.build())
121114
.build();
122115

116+
private static final KubernetesConfigServerProperties PROPERTIES = properties();
117+
123118
@BeforeAll
124-
public static void before() {
119+
static void before() {
125120
KUBERNETES_PROPERTY_SOURCE_SUPPLIER.add((coreApi, applicationName, namespace, springEnv) -> {
126121
List<MapPropertySource> propertySources = new ArrayList<>();
127122

@@ -152,8 +147,9 @@ public static void before() {
152147
}
153148

154149
@AfterEach
155-
public void after() {
150+
void after() {
156151
new KubernetesClientConfigMapsCache().discardAll();
152+
new KubernetesClientSecretsCache().discardAll();
157153
}
158154

159155
@Test
@@ -169,7 +165,7 @@ public void testApplicationCase() throws ApiException {
169165
eq(null), eq(null), eq(null), eq(null), eq(null)))
170166
.thenReturn(CONFIGMAP_DEV_LIST);
171167
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
172-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
168+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
173169
Environment environment = environmentRepository.findOne("application", "", "");
174170
assertThat(environment.getPropertySources().size()).isEqualTo(2);
175171
environment.getPropertySources().forEach(propertySource -> {
@@ -245,7 +241,7 @@ public void testStoresCase() throws ApiException {
245241
eq(null), eq(null), eq(null), eq(null), eq(null)))
246242
.thenReturn(SECRET_LIST);
247243
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
248-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
244+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
249245
Environment environment = environmentRepository.findOne("stores", "", "");
250246
assertThat(environment.getPropertySources().size()).isEqualTo(4);
251247
environment.getPropertySources().forEach(propertySource -> {
@@ -292,7 +288,7 @@ public void testStoresProfileCase() throws ApiException {
292288
eq(null), eq(null), eq(null), eq(null), eq(null)))
293289
.thenReturn(CONFIGMAP_DEV_LIST);
294290
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
295-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
291+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
296292
Environment environment = environmentRepository.findOne("stores", "dev", "");
297293
assertThat(environment.getPropertySources().size()).isEqualTo(6);
298294
environment.getPropertySources().forEach(propertySource -> {
@@ -356,7 +352,7 @@ public void testApplicationPropertiesAnSecretsOverride() throws ApiException {
356352
eq(null), eq(null), eq(null), eq(null), eq(null)))
357353
.thenReturn(CONFIGMAP_DEV_LIST);
358354
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
359-
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default");
355+
KUBERNETES_PROPERTY_SOURCE_SUPPLIER, "default", PROPERTIES);
360356
Environment environment = environmentRepository.findOne("stores-dev", "", "");
361357
environment.getPropertySources()
362358
.stream()
@@ -404,7 +400,7 @@ public void testSingleConfigMapMultipleSources() throws ApiException {
404400
return propertySources;
405401
});
406402
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi, suppliers,
407-
"default");
403+
"default", PROPERTIES);
408404
Environment environment = environmentRepository.findOne("storessingle", "", "");
409405
assertThat(environment.getPropertySources().size()).isEqualTo(1);
410406
assertThat(environment.getPropertySources().get(0).getName())
@@ -434,4 +430,10 @@ public void testSingleConfigMapMultipleSources() throws ApiException {
434430

435431
}
436432

433+
private static KubernetesConfigServerProperties properties() {
434+
KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties();
435+
properties.setOrder(1);
436+
return properties;
437+
}
438+
437439
}

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

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@
2727
import io.kubernetes.client.openapi.models.V1Secret;
2828
import io.kubernetes.client.openapi.models.V1SecretBuilder;
2929
import io.kubernetes.client.openapi.models.V1SecretList;
30-
import io.kubernetes.client.openapi.models.V1SecretListBuilder;
30+
import org.junit.jupiter.api.AfterAll;
31+
import org.junit.jupiter.api.AfterEach;
3132
import org.junit.jupiter.api.BeforeAll;
3233
import org.junit.jupiter.api.Test;
34+
import org.mockito.Mockito;
3335

3436
import org.springframework.cloud.config.environment.Environment;
37+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapsCache;
38+
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsCache;
3539
import org.springframework.cloud.kubernetes.commons.config.Constants;
3640

3741
import static org.assertj.core.api.Assertions.assertThat;
38-
import static org.mockito.ArgumentMatchers.eq;
3942
import static org.mockito.Mockito.mock;
4043
import static org.mockito.Mockito.when;
4144

@@ -47,7 +50,7 @@
4750
*/
4851
class KubernetesPropertySourceSupplierTests {
4952

50-
private static final CoreV1Api coreApi = mock(CoreV1Api.class);
53+
private static final CoreV1Api CORE_V1_API = mock(CoreV1Api.class);
5154

5255
private static final V1ConfigMapList CONFIGMAP_DEFAULT_LIST = new V1ConfigMapList()
5356
.addItemsItem(buildConfigMap("gateway", "default"));
@@ -58,41 +61,51 @@ class KubernetesPropertySourceSupplierTests {
5861
private static final V1ConfigMapList CONFIGMAP_TEAM_B_LIST = new V1ConfigMapList()
5962
.addItemsItem(buildConfigMap("orders", "team-b"));
6063

61-
private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretListBuilder()
62-
.addToItems(buildSecret("gateway", "default"))
63-
.build();
64+
private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretList()
65+
.addItemsItem(buildSecret("gateway", "default"));
6466

65-
private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretListBuilder()
66-
.addToItems(buildSecret("stores", "team-a"))
67-
.build();
67+
private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretList()
68+
.addItemsItem(buildSecret("stores", "team-a"));
6869

69-
private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretListBuilder()
70-
.addToItems(buildSecret("orders", "team-b"))
71-
.build();
70+
private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretList()
71+
.addItemsItem(buildSecret("orders", "team-b"));
72+
73+
private static final KubernetesConfigServerProperties PROPERTIES = properties();
7274

7375
@BeforeAll
74-
public static void before() throws ApiException {
75-
when(coreApi.listNamespacedConfigMap(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
76-
eq(null), eq(null), eq(null), eq(null), eq(null)))
76+
static void beforeAll() throws ApiException {
77+
when(CORE_V1_API.listNamespacedConfigMap("default", null, null, null, null, null, null, null, null, null, null,
78+
null))
7779
.thenReturn(CONFIGMAP_DEFAULT_LIST);
78-
when(coreApi.listNamespacedConfigMap(eq("team-a"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
79-
eq(null), eq(null), eq(null), eq(null), eq(null)))
80+
when(CORE_V1_API.listNamespacedConfigMap("team-a", null, null, null, null, null, null, null, null, null, null,
81+
null))
8082
.thenReturn(CONFIGMAP_TEAM_A_LIST);
81-
when(coreApi.listNamespacedConfigMap(eq("team-b"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
82-
eq(null), eq(null), eq(null), eq(null), eq(null)))
83+
when(CORE_V1_API.listNamespacedConfigMap("team-b", null, null, null, null, null, null, null, null, null, null,
84+
null))
8385
.thenReturn(CONFIGMAP_TEAM_B_LIST);
8486

85-
when(coreApi.listNamespacedSecret(eq("default"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
86-
eq(null), eq(null), eq(null), eq(null), eq(null)))
87+
when(CORE_V1_API.listNamespacedSecret("default", null, null, null, null, null, null, null, null, null, null,
88+
null))
8789
.thenReturn(SECRET_DEFAULT_LIST);
88-
when(coreApi.listNamespacedSecret(eq("team-a"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
89-
eq(null), eq(null), eq(null), eq(null), eq(null)))
90+
when(CORE_V1_API.listNamespacedSecret("team-a", null, null, null, null, null, null, null, null, null, null,
91+
null))
9092
.thenReturn(SECRET_TEAM_A_LIST);
91-
when(coreApi.listNamespacedSecret(eq("team-b"), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null),
92-
eq(null), eq(null), eq(null), eq(null), eq(null)))
93+
when(CORE_V1_API.listNamespacedSecret("team-b", null, null, null, null, null, null, null, null, null, null,
94+
null))
9395
.thenReturn(SECRET_TEAM_B_LIST);
9496
}
9597

98+
@AfterAll
99+
static void afterAll() {
100+
Mockito.reset(CORE_V1_API);
101+
}
102+
103+
@AfterEach
104+
void afterEach() {
105+
new KubernetesClientConfigMapsCache().discardAll();
106+
new KubernetesClientSecretsCache().discardAll();
107+
}
108+
96109
@Test
97110
void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
98111
KubernetesConfigServerProperties kubernetesConfigServerProperties = new KubernetesConfigServerProperties();
@@ -101,8 +114,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
101114
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
102115
.configMapPropertySourceSupplier(kubernetesConfigServerProperties);
103116

104-
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
105-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
117+
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
118+
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);
106119

107120
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
108121
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(1);
@@ -122,8 +135,8 @@ void whenExtraNamespacesAddedThenConfigMapsInCurrentNamespaceAreNotIncluded() {
122135
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
123136
.configMapPropertySourceSupplier(kubernetesConfigServerProperties);
124137

125-
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
126-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
138+
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
139+
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);
127140

128141
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
129142
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(0);
@@ -143,8 +156,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllSecretsAreIncluded() {
143156
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
144157
.secretsPropertySourceSupplier(kubernetesConfigServerProperties);
145158

146-
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
147-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
159+
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
160+
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);
148161

149162
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
150163
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(1);
@@ -164,8 +177,8 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
164177
KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration()
165178
.secretsPropertySourceSupplier(kubernetesConfigServerProperties);
166179

167-
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(coreApi,
168-
Collections.singletonList(kubernetesPropertySourceSupplier), "default");
180+
KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository(CORE_V1_API,
181+
Collections.singletonList(kubernetesPropertySourceSupplier), "default", PROPERTIES);
169182

170183
Environment environmentGateway = environmentRepository.findOne("gateway", "", "");
171184
assertThat(environmentGateway.getPropertySources().size()).isEqualTo(0);
@@ -179,19 +192,25 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
179192

180193
private static V1ConfigMap buildConfigMap(String name, String namespace) {
181194
return new V1ConfigMapBuilder()
182-
.withMetadata(
183-
new V1ObjectMetaBuilder().withName(name).withNamespace(namespace).withResourceVersion("1").build())
184-
.addToData(Constants.APPLICATION_YAML, "dummy:\n property:\n string: \"" + name + "\"\n")
195+
.withMetadata(new V1ObjectMetaBuilder().withName(name).withNamespace(namespace).build())
196+
.addToData(Constants.APPLICATION_PROPERTIES, """
197+
dummy.property.string=%s
198+
""".formatted(name))
185199
.build();
186200
}
187201

188202
private static V1Secret buildSecret(String name, String namespace) {
189203
return new V1SecretBuilder()
190-
.withMetadata(
191-
new V1ObjectMetaBuilder().withName(name).withResourceVersion("0").withNamespace(namespace).build())
204+
.withMetadata(new V1ObjectMetaBuilder().withName(name).withNamespace(namespace).build())
192205
.addToData("password", "p455w0rd".getBytes())
193206
.addToData("username", "user".getBytes())
194207
.build();
195208
}
196209

210+
private static KubernetesConfigServerProperties properties() {
211+
KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties();
212+
properties.setOrder(1);
213+
return properties;
214+
}
215+
197216
}

0 commit comments

Comments
 (0)