Skip to content

Commit

Permalink
Simply k8s client configmap event reload it (7) (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Sep 11, 2023
1 parent 835e5b1 commit f957d04
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package org.springframework.cloud.kubernetes.client.catalog;

import java.util.Map;

import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithReplace;

/**
* @author wind57
*/
final class KubernetesClientCatalogWatchUtils {

private static final Map<String, String> POD_LABELS = Map.of("app",
"spring-cloud-kubernetes-client-catalog-watcher");

private KubernetesClientCatalogWatchUtils() {

}
Expand Down Expand Up @@ -119,15 +124,15 @@ private KubernetesClientCatalogWatchUtils() {
""";

static void patchForEndpointSlices(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_ONE);
patchWithReplace(imageName, deploymentName, namespace, BODY_ONE, POD_LABELS);
}

static void patchForEndpointsNamespaces(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_TWO);
patchWithReplace(imageName, deploymentName, namespace, BODY_TWO, POD_LABELS);
}

static void patchForEndpointSlicesNamespaces(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_THREE);
patchWithReplace(imageName, deploymentName, namespace, BODY_THREE, POD_LABELS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
*/
class ConfigMapAndSecretIT {

private static final Map<String, String> POD_LABELS = Map.of("app", "spring-cloud-kubernetes-client-config-it");

private static final String BODY = """
{
"spec": {
Expand Down Expand Up @@ -134,9 +136,8 @@ void testConfigMapAndSecretRefresh() throws Exception {
WebClient propertyClient = builder.baseUrl(PROPERTY_URL).build();

await().timeout(Duration.ofSeconds(120)).pollInterval(Duration.ofSeconds(2))
.ignoreException(WebClientResponseException.BadGateway.class)
.until(() -> propertyClient
.method(HttpMethod.GET).retrieve().bodyToMono(String.class).block().equals("from-config-map"));
.ignoreException(WebClientResponseException.BadGateway.class).until(() -> propertyClient
.method(HttpMethod.GET).retrieve().bodyToMono(String.class).block().equals("from-config-map"));

WebClient secretClient = builder.baseUrl(SECRET_URL).build();
String secret = secretClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
Expand Down Expand Up @@ -206,7 +207,7 @@ private RetryBackoffSpec retrySpec() {

private static void patchForPollingReload() {
patchWithReplace(ConfigMapAndSecretIT.DOCKER_IMAGE, ConfigMapAndSecretIT.APP_NAME + "-deployment",
ConfigMapAndSecretIT.NAMESPACE, BODY);
ConfigMapAndSecretIT.NAMESPACE, BODY, POD_LABELS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
package org.springframework.cloud.kubernetes.client.configmap.event.reload;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
Expand All @@ -31,7 +28,6 @@
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ConfigMapBuilder;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1EnvVar;
import io.kubernetes.client.openapi.models.V1Ingress;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Service;
Expand All @@ -52,6 +48,10 @@
import org.springframework.web.reactive.function.client.WebClient;

import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.client.configmap.event.reload.ConfigMapEventReloadITUtil.patchOne;
import static org.springframework.cloud.kubernetes.client.configmap.event.reload.ConfigMapEventReloadITUtil.patchThree;
import static org.springframework.cloud.kubernetes.client.configmap.event.reload.ConfigMapEventReloadITUtil.patchTwo;
import static org.springframework.cloud.kubernetes.client.configmap.event.reload.DataChangesInConfigMapReloadDelegate.testSimple;

/**
* @author wind57
Expand All @@ -60,6 +60,8 @@ class ConfigMapEventReloadIT {

private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-configmap-event-reload";

private static final String DOCKER_IMAGE = "docker.io/springcloud/" + IMAGE_NAME + ":" + Commons.pomVersion();

private static final String NAMESPACE = "default";

private static final K3sContainer K3S = Commons.container();
Expand All @@ -83,6 +85,7 @@ static void beforeAll() throws Exception {
@AfterAll
static void afterAll() throws Exception {
util.deleteClusterWide(NAMESPACE, Set.of("left", "right"));
manifests(Phase.DELETE);
util.deleteNamespace("left");
util.deleteNamespace("right");
Commons.cleanUp(IMAGE_NAME, K3S);
Expand All @@ -99,7 +102,7 @@ static void afterAll() throws Exception {
*/
@Test
void testInformFromOneNamespaceEventNotTriggered() throws Exception {
manifests("one", Phase.CREATE, false);
manifests(Phase.CREATE);
Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", IMAGE_NAME);

Expand Down Expand Up @@ -130,7 +133,11 @@ void testInformFromOneNamespaceEventNotTriggered() throws Exception {
// left configmap has not changed, no restart of app has happened
Assertions.assertEquals("left-initial", result);

manifests("one", Phase.DELETE, false);
testInformFromOneNamespaceEventTriggered();
testInform();
testInformFromOneNamespaceEventTriggeredSecretsDisabled();
testSimple(DOCKER_IMAGE);

}

/**
Expand All @@ -141,9 +148,9 @@ void testInformFromOneNamespaceEventNotTriggered() throws Exception {
* - as such, event is triggered and we see the updated value
* </pre>
*/
@Test
void testInformFromOneNamespaceEventTriggered() throws Exception {
manifests("two", Phase.CREATE, false);
recreateConfigMaps();
patchOne("spring-cloud-kubernetes-client-configmap-deployment-event-reload", NAMESPACE, DOCKER_IMAGE);
Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", IMAGE_NAME);

Expand All @@ -170,8 +177,6 @@ void testInformFromOneNamespaceEventTriggered() throws Exception {
return innerResult != null;
});
Assertions.assertEquals("right-after-change", resultAfterChange[0]);

manifests("two", Phase.DELETE, false);
}

/**
Expand All @@ -183,9 +188,12 @@ void testInformFromOneNamespaceEventTriggered() throws Exception {
* right-configmap-with-label triggers changes.
* </pre>
*/
@Test
void testInform() throws Exception {
manifests("three", Phase.CREATE, false);
recreateConfigMaps();
V1ConfigMap rightWithLabelConfigMap = (V1ConfigMap) util.yaml("right-configmap-with-label.yaml");
util.createAndWait("right", rightWithLabelConfigMap, null);
patchTwo("spring-cloud-kubernetes-client-configmap-deployment-event-reload", NAMESPACE, DOCKER_IMAGE);

Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", IMAGE_NAME);

Expand Down Expand Up @@ -240,8 +248,7 @@ void testInform() throws Exception {
rightResult = rightWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
Assertions.assertEquals("right-after-change", rightResult);

manifests("three", Phase.DELETE, false);
util.deleteAndWait("right", rightWithLabelConfigMap, null);
}

/**
Expand All @@ -252,9 +259,9 @@ void testInform() throws Exception {
* - as such, event is triggered and we see the updated value
* </pre>
*/
@Test
void testInformFromOneNamespaceEventTriggeredSecretsDisabled() throws Exception {
manifests("two", Phase.CREATE, true);
recreateConfigMaps();
patchThree("spring-cloud-kubernetes-client-configmap-deployment-event-reload", NAMESPACE, DOCKER_IMAGE);
Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", IMAGE_NAME);

Expand All @@ -281,49 +288,39 @@ void testInformFromOneNamespaceEventTriggeredSecretsDisabled() throws Exception
return innerResult != null;
});
Assertions.assertEquals("right-after-change", resultAfterChange[0]);
}

private void recreateConfigMaps() {
V1ConfigMap leftConfigMap = (V1ConfigMap) util.yaml("left-configmap.yaml");
V1ConfigMap rightConfigMap = (V1ConfigMap) util.yaml("right-configmap.yaml");

manifests("two", Phase.DELETE, true);
util.deleteAndWait("left", leftConfigMap, null);
util.deleteAndWait("right", rightConfigMap, null);

util.createAndWait("left", leftConfigMap, null);
util.createAndWait("right", rightConfigMap, null);
}

private static void manifests(String deploymentRoot, Phase phase, boolean secretsDisabled) {
private static void manifests(Phase phase) {

try {

V1ConfigMap leftConfigMap = (V1ConfigMap) util.yaml("left-configmap.yaml");
V1ConfigMap rightConfigMap = (V1ConfigMap) util.yaml("right-configmap.yaml");
V1ConfigMap rightWithLabelConfigMap = (V1ConfigMap) util.yaml("right-configmap-with-label.yaml");

V1Deployment deployment = (V1Deployment) util.yaml(deploymentRoot + "/deployment.yaml");
V1Deployment deployment = (V1Deployment) util.yaml("deployment.yaml");
V1Service service = (V1Service) util.yaml("service.yaml");
V1Ingress ingress = (V1Ingress) util.yaml("ingress.yaml");

List<V1EnvVar> envVars = new ArrayList<>(
Optional.ofNullable(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv())
.orElse(List.of()));

if (secretsDisabled) {
V1EnvVar secretsDisabledEnvVar = new V1EnvVar().name("SPRING_CLOUD_KUBERNETES_SECRETS_ENABLED")
.value("FALSE");
envVars.add(secretsDisabledEnvVar);
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(envVars);
}

if (phase.equals(Phase.CREATE)) {
util.createAndWait("left", leftConfigMap, null);
util.createAndWait("right", rightConfigMap, null);

if ("three".equals(deploymentRoot)) {
util.createAndWait("right", rightWithLabelConfigMap, null);
}
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
}

if (phase.equals(Phase.DELETE)) {
util.deleteAndWait("left", leftConfigMap, null);
util.deleteAndWait("right", rightConfigMap, null);
if ("three".equals(deploymentRoot)) {
util.deleteAndWait("right", rightWithLabelConfigMap, null);
}
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
}

Expand Down
Loading

0 comments on commit f957d04

Please sign in to comment.