Fabric8 configmap event reload patch refactor part 2 (#1466)

This commit is contained in:
erabii
2023-10-05 16:42:28 +03:00
committed by GitHub
parent 11fc5d1ebe
commit 282649343b
3 changed files with 53 additions and 106 deletions

View File

@@ -43,6 +43,7 @@ import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.builder;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.patchFour;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.patchOne;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.patchThree;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.patchTwo;
@@ -139,6 +140,7 @@ class ConfigMapEventReloadIT {
testInformFromOneNamespaceEventTriggered();
testInform();
testInformFromOneNamespaceEventTriggeredSecretsDisabled();
testDataChangesInConfigMap();
}
/**
@@ -296,6 +298,12 @@ class ConfigMapEventReloadIT {
}
void testDataChangesInConfigMap() {
reCreateConfigMaps(util, client);
patchFour(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);
DataChangesInConfigMapReloadDelegate.testDataChangesInConfigMap(client);
}
private static void manifests(Phase phase) {
InputStream deploymentStream = util.inputStream("deployment.yaml");

View File

@@ -16,77 +16,34 @@
package org.springframework.cloud.kubernetes.fabric8.configmap.event.reload;
import java.io.InputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Container;
import org.testcontainers.k3s.K3sContainer;
import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.builder;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.replaceConfigMap;
import static org.springframework.cloud.kubernetes.fabric8.configmap.event.reload.TestUtil.retrySpec;
class DataChangesInConfigMapReloadIT {
final class DataChangesInConfigMapReloadDelegate {
private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-client-configmap-event-reload";
private static final String NAMESPACE = "default";
private static final String LEFT_NAMESPACE = "left";
private static final K3sContainer K3S = Commons.container();
private static Util util;
private static KubernetesClient client;
@BeforeAll
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
util = new Util(K3S);
client = util.client();
util.createNamespace(LEFT_NAMESPACE);
util.setUpClusterWide(NAMESPACE, Set.of(LEFT_NAMESPACE));
}
@AfterAll
static void afterAll() throws Exception {
util.deleteNamespace(LEFT_NAMESPACE);
Commons.cleanUp(IMAGE_NAME, K3S);
Commons.systemPrune();
}
/**
* <pre>
* - configMap with no labels and data: left.value = left-initial exists in namespace left
@@ -98,9 +55,7 @@ class DataChangesInConfigMapReloadIT {
* - then we change data inside the config map, and we must see the updated value
* </pre>
*/
@Test
void testSimple() {
manifests(Phase.CREATE);
static void testDataChangesInConfigMap(KubernetesClient client) {
Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", IMAGE_NAME);
@@ -117,7 +72,7 @@ class DataChangesInConfigMapReloadIT {
.withLabels(Map.of("new-label", "abc")).withNamespace("left").withName("left-configmap").build())
.withData(Map.of("left.value", "left-initial")).build();
replaceConfigMap(configMap);
replaceConfigMap(client, configMap, "left");
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
@@ -136,7 +91,7 @@ class DataChangesInConfigMapReloadIT {
.withName("left-configmap").build())
.withData(Map.of("left.value", "left-after-change")).build();
replaceConfigMap(configMap);
replaceConfigMap(client, configMap, "left");
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
@@ -145,50 +100,9 @@ class DataChangesInConfigMapReloadIT {
return "left-after-change".equals(innerResult);
});
manifests(Phase.DELETE);
}
private static void manifests(Phase phase) {
InputStream deploymentStream = util.inputStream("deployment.yaml");
InputStream serviceStream = util.inputStream("service.yaml");
InputStream ingressStream = util.inputStream("ingress.yaml");
InputStream configmapAsStream = util.inputStream("left-configmap.yaml");
Deployment deployment = Serialization.unmarshal(deploymentStream, Deployment.class);
List<EnvVar> envVars = new ArrayList<>(
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv());
EnvVar activeProfileProperty = new EnvVarBuilder().withName("SPRING_PROFILES_ACTIVE").withValue("one").build();
envVars.add(activeProfileProperty);
EnvVar secretsDisabledEnvVar = new EnvVarBuilder().withName("SPRING_CLOUD_KUBERNETES_SECRETS_ENABLED")
.withValue("FALSE").build();
EnvVar debugLevel = new EnvVarBuilder()
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD").withName("DEBUG")
.build();
envVars.add(debugLevel);
envVars.add(secretsDisabledEnvVar);
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(envVars);
Service service = Serialization.unmarshal(serviceStream, Service.class);
Ingress ingress = Serialization.unmarshal(ingressStream, Ingress.class);
ConfigMap configMap = Serialization.unmarshal(configmapAsStream, ConfigMap.class);
if (phase.equals(Phase.CREATE)) {
util.createAndWait(LEFT_NAMESPACE, configMap, null);
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
}
else {
util.deleteAndWait(LEFT_NAMESPACE, configMap, null);
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
}
}
private String logs() {
private static String logs() {
try {
String appPodName = K3S.execInContainer("sh", "-c",
"kubectl get pods -l app=" + IMAGE_NAME + " -o=name --no-headers | tr -d '\n'").getStdout();
@@ -202,16 +116,4 @@ class DataChangesInConfigMapReloadIT {
}
}
private WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}
private RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(120, Duration.ofSeconds(2)).filter(Objects::nonNull);
}
private static void replaceConfigMap(ConfigMap configMap) {
client.configMaps().inNamespace(LEFT_NAMESPACE).resource(configMap).createOrReplace();
}
}

View File

@@ -119,6 +119,39 @@ final class TestUtil {
}
""";
private static final String BODY_FOUR = """
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "spring-cloud-kubernetes-fabric8-client-configmap-event-reload",
"image": "image_name_here",
"env": [
{
"name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_FABRIC8_CONFIG_RELOAD",
"value": "DEBUG"
},
{
"name": "SPRING_PROFILES_ACTIVE",
"value": "one"
},
{
"name": "SPRING_CLOUD_KUBERNETES_SECRETS_ENABLED",
"value": "FALSE"
},
{
"name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD",
"value": "DEBUG"
}
]
}]
}
}
}
}
""";
private TestUtil() {
}
@@ -150,6 +183,10 @@ final class TestUtil {
util.patchWithReplace(dockerImage, deploymentName, namespace, BODY_THREE, POD_LABELS);
}
static void patchFour(Util util, String dockerImage, String deploymentName, String namespace) {
util.patchWithReplace(dockerImage, deploymentName, namespace, BODY_FOUR, POD_LABELS);
}
static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}