Skip to content

Commit b1c3c38

Browse files
committed
fix
1 parent f1e0774 commit b1c3c38

File tree

14 files changed

+185
-621
lines changed

14 files changed

+185
-621
lines changed

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/main/resources/application-mount.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ spring:
1212
config:
1313
paths:
1414
- /tmp/application.properties
15-
15+
config:
16+
import: "kubernetes:"

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/BootstrapEnabledPollingReloadConfigMapMountDelegate.java

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.cloud.kubernetes.fabric8.client.reload.it;
17+
package org.springframework.cloud.kubernetes.fabric8.client.reload;
1818

1919
import java.io.InputStream;
2020
import java.time.Duration;
2121
import java.util.Map;
2222

2323
import io.fabric8.kubernetes.api.model.ConfigMap;
24-
import io.fabric8.kubernetes.api.model.Service;
25-
import io.fabric8.kubernetes.api.model.apps.Deployment;
26-
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
2724
import io.fabric8.kubernetes.client.KubernetesClient;
2825
import io.fabric8.kubernetes.client.utils.Serialization;
2926
import org.junit.jupiter.api.AfterAll;
30-
import org.junit.jupiter.api.Assertions;
3127
import org.junit.jupiter.api.BeforeAll;
3228
import org.junit.jupiter.api.Test;
3329
import org.testcontainers.k3s.K3sContainer;
@@ -39,9 +35,11 @@
3935
import org.springframework.http.HttpMethod;
4036
import org.springframework.web.reactive.function.client.WebClient;
4137

38+
import static org.assertj.core.api.Assertions.assertThat;
4239
import static org.awaitility.Awaitility.await;
43-
import static org.springframework.cloud.kubernetes.fabric8.client.reload.it.TestAssertions.builder;
44-
import static org.springframework.cloud.kubernetes.fabric8.client.reload.it.TestAssertions.retrySpec;
40+
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.builder;
41+
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.manifests;
42+
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.retrySpec;
4543

4644
/**
4745
* @author wind57
@@ -67,13 +65,13 @@ static void beforeAll() throws Exception {
6765
util = new Util(K3S);
6866
client = util.client();
6967
util.setUp(NAMESPACE);
70-
manifests(Phase.CREATE);
68+
manifests(Phase.CREATE, util, NAMESPACE);
7169
}
7270

7371
@AfterAll
7472
static void afterAll() throws Exception {
7573
Commons.cleanUp(IMAGE_NAME, K3S);
76-
manifests(Phase.DELETE);
74+
manifests(Phase.DELETE, util, NAMESPACE);
7775
}
7876

7977
/**
@@ -106,11 +104,11 @@ void test() {
106104
.block();
107105

108106
// we first read the initial value from the configmap
109-
Assertions.assertEquals("as-mount-initial", result);
107+
assertThat(result).isEqualTo("as-mount-initial");
110108

111109
// replace data in configmap and wait for k8s to pick it up
112110
// our polling will detect that and restart the app
113-
InputStream configMapStream = util.inputStream("configmap.yaml");
111+
InputStream configMapStream = util.inputStream("manifests/configmap.yaml");
114112
ConfigMap configMap = Serialization.unmarshal(configMapStream, ConfigMap.class);
115113
configMap.setData(Map.of(Constants.APPLICATION_PROPERTIES, "from.properties.key=as-mount-changed"));
116114
client.configMaps().inNamespace("default").resource(configMap).createOrReplace();
@@ -124,28 +122,4 @@ void test() {
124122
.equals("as-mount-changed"));
125123
}
126124

127-
private static void manifests(Phase phase) {
128-
129-
InputStream deploymentStream = util.inputStream("manifests/deployment.yaml");
130-
InputStream serviceStream = util.inputStream("manifests/service.yaml");
131-
InputStream ingressStream = util.inputStream("manifests/ingress.yaml");
132-
InputStream configMapAsStream = util.inputStream("manifests/configmap.yaml");
133-
134-
Deployment deployment = Serialization.unmarshal(deploymentStream, Deployment.class);
135-
136-
Service service = Serialization.unmarshal(serviceStream, Service.class);
137-
Ingress ingress = Serialization.unmarshal(ingressStream, Ingress.class);
138-
ConfigMap configMap = Serialization.unmarshal(configMapAsStream, ConfigMap.class);
139-
140-
if (phase.equals(Phase.CREATE)) {
141-
util.createAndWait(NAMESPACE, configMap, null);
142-
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
143-
}
144-
else {
145-
util.deleteAndWait(NAMESPACE, configMap, null);
146-
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
147-
}
148-
149-
}
150-
151125
}

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-reload/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/reload/ConfigMapMountPollingReloadDelegate.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.client.reload;
18+
19+
import java.io.InputStream;
20+
import java.time.Duration;
21+
import java.util.Map;
22+
23+
import io.fabric8.kubernetes.api.model.ConfigMap;
24+
import io.fabric8.kubernetes.client.KubernetesClient;
25+
import io.fabric8.kubernetes.client.utils.Serialization;
26+
import org.junit.jupiter.api.AfterAll;
27+
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.Test;
29+
import org.testcontainers.k3s.K3sContainer;
30+
31+
import org.springframework.cloud.kubernetes.commons.config.Constants;
32+
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
33+
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
34+
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
35+
import org.springframework.http.HttpMethod;
36+
import org.springframework.web.reactive.function.client.WebClient;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.awaitility.Awaitility.await;
40+
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.builder;
41+
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.manifests;
42+
import static org.springframework.cloud.kubernetes.fabric8.client.reload.TestAssertions.retrySpec;
43+
44+
/**
45+
* @author wind57
46+
*/
47+
class ConfigMapMountPollingReloadDelegateIT {
48+
49+
private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-client-reload";
50+
51+
private static final String NAMESPACE = "default";
52+
53+
private static final K3sContainer K3S = Commons.container();
54+
55+
private static Util util;
56+
57+
private static KubernetesClient client;
58+
59+
@BeforeAll
60+
static void beforeAll() throws Exception {
61+
K3S.start();
62+
Commons.validateImage(IMAGE_NAME, K3S);
63+
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
64+
65+
util = new Util(K3S);
66+
client = util.client();
67+
util.setUp(NAMESPACE);
68+
manifests(Phase.CREATE, util, NAMESPACE);
69+
}
70+
71+
@AfterAll
72+
static void afterAll() throws Exception {
73+
Commons.cleanUp(IMAGE_NAME, K3S);
74+
manifests(Phase.DELETE, util, NAMESPACE);
75+
}
76+
77+
/**
78+
* <pre>
79+
* - we have "spring.config.import: kubernetes", which means we will 'locate' property sources
80+
* from config maps.
81+
* - the property above means that at the moment we will be searching for config maps that only
82+
* match the application name, in this specific test there is no such config map.
83+
* - what we will also read, is 'spring.cloud.kubernetes.config.paths', which we have set to
84+
* '/tmp/application.properties'
85+
* in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
86+
* - we first assert that we are actually reading the path based source via (1), (2) and (3).
87+
*
88+
* - we then change the config map content, wait for k8s to pick it up and replace them
89+
* - our polling will then detect that change, and trigger a reload.
90+
* </pre>
91+
*/
92+
@Test
93+
void test() {
94+
// (1)
95+
Commons.waitForLogStatement("paths property sources : [/tmp/application.properties]", K3S, IMAGE_NAME);
96+
// (2)
97+
Commons.waitForLogStatement("will add file-based property source : /tmp/application.properties", K3S,
98+
IMAGE_NAME);
99+
// (3)
100+
WebClient webClient = builder().baseUrl("http://localhost/key").build();
101+
String result = webClient.method(HttpMethod.GET)
102+
.retrieve()
103+
.bodyToMono(String.class)
104+
.retryWhen(retrySpec())
105+
.block();
106+
107+
// we first read the initial value from the configmap
108+
assertThat(result).isEqualTo("as-mount-initial");
109+
110+
// replace data in configmap and wait for k8s to pick it up
111+
// our polling will detect that and restart the app
112+
InputStream configMapStream = util.inputStream("manifests/configmap.yaml");
113+
ConfigMap configMap = Serialization.unmarshal(configMapStream, ConfigMap.class);
114+
configMap.setData(Map.of(Constants.APPLICATION_PROPERTIES, "from.properties.key=as-mount-changed"));
115+
client.configMaps().inNamespace("default").resource(configMap).createOrReplace();
116+
117+
await().timeout(Duration.ofSeconds(360))
118+
.until(() -> webClient.method(HttpMethod.GET)
119+
.retrieve()
120+
.bodyToMono(String.class)
121+
.retryWhen(retrySpec())
122+
.block()
123+
.equals("as-mount-changed"));
124+
}
125+
126+
}

0 commit comments

Comments
 (0)