|
| 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