From 24760a58366f60c1401c9534c9dea0f4f1361bdf Mon Sep 17 00:00:00 2001 From: erabii Date: Mon, 11 Sep 2023 15:29:15 +0300 Subject: [PATCH] Simply k8s client config it (6) (#1424) --- .../config/it/ConfigMapAndSecretIT.java | 73 +++++++++++++------ 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-client-config-it/src/test/java/org/springframework/cloud/kubernetes/client/config/it/ConfigMapAndSecretIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-client-config-it/src/test/java/org/springframework/cloud/kubernetes/client/config/it/ConfigMapAndSecretIT.java index 1137969afa..0be998e551 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-client-config-it/src/test/java/org/springframework/cloud/kubernetes/client/config/it/ConfigMapAndSecretIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-client-config-it/src/test/java/org/springframework/cloud/kubernetes/client/config/it/ConfigMapAndSecretIT.java @@ -17,8 +17,6 @@ package org.springframework.cloud.kubernetes.client.config.it; import java.time.Duration; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.Objects; @@ -26,13 +24,10 @@ import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.models.V1ConfigMap; import io.kubernetes.client.openapi.models.V1Deployment; -import io.kubernetes.client.openapi.models.V1EnvVar; -import io.kubernetes.client.openapi.models.V1EnvVarBuilder; import io.kubernetes.client.openapi.models.V1Ingress; import io.kubernetes.client.openapi.models.V1Secret; import io.kubernetes.client.openapi.models.V1Service; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -49,12 +44,34 @@ import org.springframework.web.reactive.function.client.WebClient; import static org.awaitility.Awaitility.await; +import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithReplace; /** * @author Ryan Baxter */ class ConfigMapAndSecretIT { + private static final String BODY = """ + { + "spec": { + "template": { + "spec": { + "containers": [{ + "name": "spring-cloud-kubernetes-client-config-it-deployment", + "image": "image_name_here", + "env": [ + { + "name": "SPRING_CLOUD_KUBERNETES_RELOAD_MODE", + "value": "POLLING" + } + ] + }] + } + } + } + } + """; + private static final String PROPERTY_URL = "http://localhost:80/myProperty"; private static final String SECRET_URL = "http://localhost:80/mySecret"; @@ -65,6 +82,8 @@ class ConfigMapAndSecretIT { private static final String APP_NAME = "spring-cloud-kubernetes-client-config-it"; + private static final String DOCKER_IMAGE = "docker.io/springcloud/" + APP_NAME + ":" + Commons.pomVersion(); + private static final K3sContainer K3S = Commons.container(); private static Util util; @@ -83,24 +102,22 @@ static void setup() throws Exception { @AfterAll static void afterAll() throws Exception { + configK8sClientIt(Phase.DELETE); Commons.cleanUp(K8S_CONFIG_CLIENT_IT_SERVICE_NAME, K3S); Commons.systemPrune(); } - @AfterEach - void after() { - configK8sClientIt(false, Phase.DELETE); - } - @Test void testConfigMapAndSecretWatchRefresh() { - configK8sClientIt(false, Phase.CREATE); + configK8sClientIt(Phase.CREATE); testConfigMapAndSecretRefresh(); + + testConfigMapAndSecretPollingRefresh(); } - @Test void testConfigMapAndSecretPollingRefresh() { - configK8sClientIt(true, Phase.CREATE); + recreateConfigMapAndSecret(); + patchForPollingReload(); testConfigMapAndSecretRefresh(); } @@ -149,18 +166,25 @@ void testConfigMapAndSecretRefresh() { .method(HttpMethod.GET).retrieve().bodyToMono(String.class).block().equals("p455w1rd")); } - private static void configK8sClientIt(boolean pooling, Phase phase) { - V1Deployment deployment = (V1Deployment) util.yaml("spring-cloud-kubernetes-client-config-it-deployment.yaml"); + void recreateConfigMapAndSecret() { + try { + coreV1Api.deleteNamespacedConfigMap("spring-cloud-kubernetes-client-config-it", NAMESPACE, + null, null, null, null, null, null); + coreV1Api.deleteNamespacedSecret("spring-cloud-kubernetes-client-config-it", NAMESPACE, + null, null, null, null, null, null); - if (pooling) { - V1EnvVar one = new V1EnvVarBuilder().withName("SPRING_CLOUD_KUBERNETES_RELOAD_MODE").withValue("polling") - .build(); - List existing = new ArrayList<>( - deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv()); - existing.add(one); - deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(existing); + V1ConfigMap configMap = (V1ConfigMap) util.yaml("spring-cloud-kubernetes-client-config-it-configmap.yaml"); + V1Secret secret = (V1Secret) util.yaml("spring-cloud-kubernetes-client-config-it-secret.yaml"); + util.createAndWait(NAMESPACE, configMap, secret); } + catch (ApiException e) { + throw new RuntimeException(e); + } + } + private static void configK8sClientIt(Phase phase) { + + V1Deployment deployment = (V1Deployment) util.yaml("spring-cloud-kubernetes-client-config-it-deployment.yaml"); V1Service service = (V1Service) util.yaml("spring-cloud-kubernetes-client-config-it-service.yaml"); V1Ingress ingress = (V1Ingress) util.yaml("spring-cloud-kubernetes-client-config-it-ingress.yaml"); @@ -185,4 +209,9 @@ private RetryBackoffSpec retrySpec() { return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull); } + private static void patchForPollingReload() { + patchWithReplace(ConfigMapAndSecretIT.DOCKER_IMAGE, ConfigMapAndSecretIT.APP_NAME + "-deployment", + ConfigMapAndSecretIT.NAMESPACE, BODY); + } + }