|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 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.k8s.client.reload.it; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import io.kubernetes.client.openapi.apis.CoreV1Api; |
| 24 | +import io.kubernetes.client.openapi.models.V1Secret; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.testcontainers.k3s.K3sContainer; |
| 29 | + |
| 30 | +import org.springframework.cloud.kubernetes.integration.tests.commons.Commons; |
| 31 | +import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; |
| 32 | +import org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util; |
| 33 | +import org.springframework.http.HttpMethod; |
| 34 | +import org.springframework.web.reactive.function.client.WebClient; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.awaitility.Awaitility.await; |
| 38 | +import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder; |
| 39 | +import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author wind57 |
| 43 | + */ |
| 44 | +class K8sClientSecretMountBootstrapPollingIT extends K8sClientReloadBase { |
| 45 | + |
| 46 | + private static final String IMAGE_NAME = "spring-cloud-kubernetes-k8s-client-reload"; |
| 47 | + |
| 48 | + private static final String NAMESPACE = "default"; |
| 49 | + |
| 50 | + private static final K3sContainer K3S = Commons.container(); |
| 51 | + |
| 52 | + private static Util util; |
| 53 | + |
| 54 | + private static CoreV1Api coreV1Api; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void beforeAllLocal() throws Exception { |
| 58 | + K3S.start(); |
| 59 | + Commons.validateImage(IMAGE_NAME, K3S); |
| 60 | + Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S); |
| 61 | + |
| 62 | + util = new Util(K3S); |
| 63 | + coreV1Api = new CoreV1Api(); |
| 64 | + util.setUp(NAMESPACE); |
| 65 | + manifestsSecret(Phase.CREATE, util, NAMESPACE, IMAGE_NAME); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterAll |
| 69 | + static void afterAll() { |
| 70 | + manifestsSecret(Phase.DELETE, util, NAMESPACE, IMAGE_NAME); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * <pre> |
| 75 | + * - we have bootstrap enabled |
| 76 | + * - we will 'locate' property sources from secrets. |
| 77 | + * - there are no explicit secrets to search for, but what we will also read, |
| 78 | + * is 'spring.cloud.kubernetes.secret.paths', which we have set to |
| 79 | + * '/tmp/application.properties' |
| 80 | + * in this test. That is populated by the volumeMounts (see mount/deployment-with-secret.yaml) |
| 81 | + * - we first assert that we are actually reading the path based source |
| 82 | + * |
| 83 | + * - we then change the secret content, wait for k8s to pick it up and replace them |
| 84 | + * - our polling will then detect that change, and trigger a reload. |
| 85 | + * </pre> |
| 86 | + */ |
| 87 | + @Test |
| 88 | + void test() throws Exception { |
| 89 | + WebClient webClient = builder().baseUrl("http://localhost:32321/secret").build(); |
| 90 | + String result = webClient.method(HttpMethod.GET) |
| 91 | + .retrieve() |
| 92 | + .bodyToMono(String.class) |
| 93 | + .retryWhen(retrySpec()) |
| 94 | + .block(); |
| 95 | + |
| 96 | + // we first read the initial value from the configmap |
| 97 | + assertThat(result).isEqualTo("initial"); |
| 98 | + |
| 99 | + // replace data in secret and wait for k8s to pick it up |
| 100 | + // our polling will detect that and restart the app |
| 101 | + V1Secret secret = (V1Secret) util.yaml("mount/secret.yaml"); |
| 102 | + secret.setData(Map.of("from.properties.secret.key", "as-mount-changed".getBytes(StandardCharsets.UTF_8))); |
| 103 | + coreV1Api.replaceNamespacedSecret("secret-reload", NAMESPACE, secret, null, null, null, null); |
| 104 | + |
| 105 | + Commons.waitForLogStatement("Detected change in config maps/secrets, reload will be triggered", K3S, |
| 106 | + IMAGE_NAME); |
| 107 | + |
| 108 | + await().atMost(Duration.ofSeconds(120)) |
| 109 | + .pollInterval(Duration.ofSeconds(1)) |
| 110 | + .until(() -> webClient.method(HttpMethod.GET) |
| 111 | + .retrieve() |
| 112 | + .bodyToMono(String.class) |
| 113 | + .retryWhen(retrySpec()) |
| 114 | + .block() |
| 115 | + .equals("as-mount-changed")); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments