Simply k8s client configmap event reload it (7) (#1425)

This commit is contained in:
erabii
2023-09-12 00:04:21 +03:00
committed by GitHub
parent 835e5b14db
commit f957d0453b
13 changed files with 345 additions and 224 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.kubernetes.client.catalog;
import java.util.Map;
import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithReplace;
/**
@@ -23,6 +25,9 @@ import static org.springframework.cloud.kubernetes.integration.tests.commons.nat
*/
final class KubernetesClientCatalogWatchUtils {
private static final Map<String, String> POD_LABELS = Map.of("app",
"spring-cloud-kubernetes-client-catalog-watcher");
private KubernetesClientCatalogWatchUtils() {
}
@@ -119,15 +124,15 @@ final class 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);
}
}

View File

@@ -52,6 +52,8 @@ import static org.springframework.cloud.kubernetes.integration.tests.commons.nat
*/
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": {
@@ -134,9 +136,8 @@ class ConfigMapAndSecretIT {
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())
@@ -206,7 +207,7 @@ class ConfigMapAndSecretIT {
private static void patchForPollingReload() {
patchWithReplace(ConfigMapAndSecretIT.DOCKER_IMAGE, ConfigMapAndSecretIT.APP_NAME + "-deployment",
ConfigMapAndSecretIT.NAMESPACE, BODY);
ConfigMapAndSecretIT.NAMESPACE, BODY, POD_LABELS);
}
}

View File

@@ -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;
@@ -31,7 +28,6 @@ import io.kubernetes.client.openapi.apis.CoreV1Api;
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;
@@ -52,6 +48,10 @@ 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.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
@@ -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();
@@ -83,6 +85,7 @@ class ConfigMapEventReloadIT {
@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);
@@ -99,7 +102,7 @@ class ConfigMapEventReloadIT {
*/
@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);
@@ -130,7 +133,11 @@ class ConfigMapEventReloadIT {
// 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);
}
/**
@@ -141,9 +148,9 @@ class ConfigMapEventReloadIT {
* - 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);
@@ -170,8 +177,6 @@ class ConfigMapEventReloadIT {
return innerResult != null;
});
Assertions.assertEquals("right-after-change", resultAfterChange[0]);
manifests("two", Phase.DELETE, false);
}
/**
@@ -183,9 +188,12 @@ class ConfigMapEventReloadIT {
* 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);
@@ -240,8 +248,7 @@ class ConfigMapEventReloadIT {
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);
}
/**
@@ -252,9 +259,9 @@ class ConfigMapEventReloadIT {
* - 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);
@@ -281,49 +288,39 @@ class ConfigMapEventReloadIT {
return innerResult != null;
});
Assertions.assertEquals("right-after-change", resultAfterChange[0]);
manifests("two", Phase.DELETE, true);
}
private static void manifests(String deploymentRoot, Phase phase, boolean secretsDisabled) {
private void recreateConfigMaps() {
V1ConfigMap leftConfigMap = (V1ConfigMap) util.yaml("left-configmap.yaml");
V1ConfigMap rightConfigMap = (V1ConfigMap) util.yaml("right-configmap.yaml");
util.deleteAndWait("left", leftConfigMap, null);
util.deleteAndWait("right", rightConfigMap, null);
util.createAndWait("left", leftConfigMap, null);
util.createAndWait("right", rightConfigMap, null);
}
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);
}

View File

@@ -0,0 +1,247 @@
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.event.reload;
import java.util.Map;
import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithReplace;
/**
* @author wind57
*/
final class ConfigMapEventReloadITUtil {
private static final Map<String, String> POD_LABELS = Map.of("app",
"spring-cloud-kubernetes-client-configmap-event-reload");
private ConfigMapEventReloadITUtil() {
}
private static final String BODY_ONE = """
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "spring-cloud-kubernetes-client-configmap-event-reload",
"image": "image_name_here",
"livenessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/liveness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"readinessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/readiness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"env": [
{
"name": "SPRING_PROFILES_ACTIVE",
"value": "two"
},
{
"name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD",
"value": "DEBUG"
}
]
}]
}
}
}
}
""";
private static final String BODY_TWO = """
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "spring-cloud-kubernetes-client-configmap-event-reload",
"image": "image_name_here",
"livenessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/liveness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"readinessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/readiness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"env": [
{
"name": "SPRING_PROFILES_ACTIVE",
"value": "three"
},
{
"name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD",
"value": "DEBUG"
}
]
}]
}
}
}
}
""";
private static final String BODY_THREE = """
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "spring-cloud-kubernetes-client-configmap-event-reload",
"image": "image_name_here",
"livenessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/liveness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"readinessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/readiness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"env": [
{
"name": "SPRING_PROFILES_ACTIVE",
"value": "two"
},
{
"name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD",
"value": "DEBUG"
},
{
"name": "SPRING_CLOUD_KUBERNETES_SECRETS_ENABLED",
"value": "FALSE"
}
]
}]
}
}
}
}
""";
private static final String BODY_FOUR = """
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "spring-cloud-kubernetes-client-configmap-event-reload",
"image": "image_name_here",
"livenessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/liveness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"readinessProbe": {
"failureThreshold": 3,
"httpGet": {
"path": "/actuator/health/readiness",
"port": 8080,
"scheme": "HTTP"
},
"periodSeconds": 10,
"successThreshold": 1,
"timeoutSeconds": 1
},
"env": [
{
"name": "SPRING_PROFILES_ACTIVE",
"value": "one"
},
{
"name": "LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD",
"value": "DEBUG"
},
{
"name": "SPRING_CLOUD_KUBERNETES_SECRETS_ENABLED",
"value": "FALSE"
}
]
}]
}
}
}
}
""";
static void patchOne(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_ONE, POD_LABELS);
}
static void patchTwo(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_TWO, POD_LABELS);
}
static void patchThree(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_THREE, POD_LABELS);
}
static void patchFour(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_FOUR, POD_LABELS);
}
}

View File

@@ -17,26 +17,15 @@
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 io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
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.V1ObjectMetaBuilder;
import io.kubernetes.client.openapi.models.V1Service;
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;
@@ -44,15 +33,14 @@ 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.native_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.client.configmap.event.reload.ConfigMapEventReloadITUtil.patchFour;
class DataChangesInConfigMapReloadIT {
class DataChangesInConfigMapReloadDelegate {
private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-configmap-event-reload";
@@ -62,30 +50,6 @@ class DataChangesInConfigMapReloadIT {
private static final K3sContainer K3S = Commons.container();
private static Util util;
private static CoreV1Api api;
@BeforeAll
static void beforeAll() throws Exception {
K3S.start();
Commons.validateImage(IMAGE_NAME, K3S);
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
util = new Util(K3S);
api = new CoreV1Api();
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
@@ -97,9 +61,9 @@ 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 testSimple(String dockerImage) {
patchFour("spring-cloud-kubernetes-client-configmap-deployment-event-reload", NAMESPACE, dockerImage);
Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", IMAGE_NAME);
@@ -116,7 +80,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, "left-configmap");
replaceConfigMap(configMap);
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
@@ -135,7 +99,7 @@ class DataChangesInConfigMapReloadIT {
.withName("left-configmap").build())
.withData(Map.of("left.value", "left-after-change")).build();
replaceConfigMap(configMap, "left-configmap");
replaceConfigMap(configMap);
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
@@ -144,46 +108,9 @@ class DataChangesInConfigMapReloadIT {
return "left-after-change".equals(innerResult);
});
manifests(Phase.DELETE);
}
private static void manifests(Phase phase) {
try {
V1ConfigMap leftConfigMap = (V1ConfigMap) util.yaml("left-configmap.yaml");
V1Deployment deployment = (V1Deployment) util.yaml("one/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()));
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_NAMESPACE, leftConfigMap, null);
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
}
if (phase.equals(Phase.DELETE)) {
util.deleteAndWait(LEFT_NAMESPACE, leftConfigMap, null);
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
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();
@@ -197,17 +124,18 @@ class DataChangesInConfigMapReloadIT {
}
}
private WebClient.Builder builder() {
private static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}
private RetryBackoffSpec retrySpec() {
private static RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(120, Duration.ofSeconds(2)).filter(Objects::nonNull);
}
private static void replaceConfigMap(V1ConfigMap configMap, String name) {
private static void replaceConfigMap(V1ConfigMap configMap) {
try {
api.replaceNamespacedConfigMap(name, LEFT_NAMESPACE, configMap, null, null, null, null);
new CoreV1Api().replaceNamespacedConfigMap("left-configmap", LEFT_NAMESPACE, configMap, null, null, null,
null);
}
catch (ApiException e) {
throw new RuntimeException(e);

View File

@@ -4,4 +4,4 @@ metadata:
name: left-configmap
namespace: left
data:
left.value: left-initial
left.value: "left-initial"

View File

@@ -6,4 +6,4 @@ metadata:
labels:
spring.cloud.kubernetes.config.informer.enabled: true
data:
right.with.label.value: right-with-label-initial
right.with.label.value: "right-with-label-initial"

View File

@@ -4,4 +4,4 @@ metadata:
name: right-configmap
namespace: right
data:
right.value: right-initial
right.value: "right-initial"

View File

@@ -1,33 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-cloud-kubernetes-client-configmap-deployment-event-reload
spec:
selector:
matchLabels:
app: spring-cloud-kubernetes-client-configmap-event-reload
template:
metadata:
labels:
app: spring-cloud-kubernetes-client-configmap-event-reload
spec:
serviceAccountName: spring-cloud-kubernetes-serviceaccount
containers:
- name: spring-cloud-kubernetes-client-configmap-event-reload
image: docker.io/springcloud/spring-cloud-kubernetes-client-configmap-event-reload
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 8080
path: /actuator/health/readiness
livenessProbe:
httpGet:
port: 8080
path: /actuator/health/liveness
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: three
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD
value: DEBUG

View File

@@ -1,33 +0,0 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-cloud-kubernetes-client-configmap-deployment-event-reload
spec:
selector:
matchLabels:
app: spring-cloud-kubernetes-client-configmap-event-reload
template:
metadata:
labels:
app: spring-cloud-kubernetes-client-configmap-event-reload
spec:
serviceAccountName: spring-cloud-kubernetes-serviceaccount
containers:
- name: spring-cloud-kubernetes-client-configmap-event-reload
image: docker.io/springcloud/spring-cloud-kubernetes-client-configmap-event-reload
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
port: 8080
path: /actuator/health/readiness
livenessProbe:
httpGet:
port: 8080
path: /actuator/health/liveness
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: two
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD
value: DEBUG

View File

@@ -16,9 +16,7 @@
package org.springframework.cloud.kubernetes.client.discovery.it;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogAccessor;
import java.util.Map;
import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithMerge;
import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithReplace;
@@ -28,8 +26,7 @@ import static org.springframework.cloud.kubernetes.integration.tests.commons.nat
*/
final class KubernetesClientDiscoveryClientUtils {
private static final LogAccessor LOG = new LogAccessor(
LogFactory.getLog(KubernetesClientDiscoveryClientUtils.class));
private static final Map<String, String> POD_LABELS = Map.of("app", "spring-cloud-kubernetes-client-discovery-it");
// patch the filter so that it matches both namespaces
private static final String BODY_ONE = """
@@ -358,54 +355,54 @@ final class KubernetesClientDiscoveryClientUtils {
}
static void patchForTwoNamespacesMatchViaThePredicate(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_ONE);
patchWithMerge(deploymentName, namespace, BODY_ONE, POD_LABELS);
}
static void patchForReactiveHealth(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_TWO);
patchWithMerge(deploymentName, namespace, BODY_TWO, POD_LABELS);
}
static void patchForBlockingAndReactiveHealth(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_THREE);
patchWithMerge(deploymentName, namespace, BODY_THREE, POD_LABELS);
}
// notice the usage of 'PATCH_FORMAT_JSON_MERGE_PATCH' here, it will not merge
// env variables
static void patchForBlockingHealth(String image, String deploymentName, String namespace) {
patchWithReplace(image, deploymentName, namespace, BODY_FOUR);
patchWithReplace(image, deploymentName, namespace, BODY_FOUR, POD_LABELS);
}
// add SPRING_CLOUD_KUBERNETES_DISCOVERY_ALL_NAMESPACES=TRUE
static void patchForAllNamespaces(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_FIVE);
patchWithMerge(deploymentName, namespace, BODY_FIVE, POD_LABELS);
}
static void patchForSingleNamespace(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_SIX);
patchWithMerge(deploymentName, namespace, BODY_SIX, POD_LABELS);
}
static void patchForPodMetadata(String imageName, String deploymentName, String namespace) {
patchWithReplace(imageName, deploymentName, namespace, BODY_SEVEN);
patchWithReplace(imageName, deploymentName, namespace, BODY_SEVEN, POD_LABELS);
}
static void patchForReactiveOnly(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_EIGHT);
patchWithMerge(deploymentName, namespace, BODY_EIGHT, POD_LABELS);
}
static void patchForBlockingAndReactive(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_NINE);
patchWithMerge(deploymentName, namespace, BODY_NINE, POD_LABELS);
}
static void patchForTwoNamespacesBlockingOnly(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_TEN);
patchWithMerge(deploymentName, namespace, BODY_TEN, POD_LABELS);
}
static void patchToAddBlockingSupport(String deploymentName, String namespace) {
patchWithMerge(deploymentName, namespace, BODY_ELEVEN);
patchWithMerge(deploymentName, namespace, BODY_ELEVEN, POD_LABELS);
}
static void patchForUATNamespacesTests(String image, String deploymentName, String namespace) {
patchWithReplace(image, deploymentName, namespace, BODY_TWELVE);
patchWithReplace(image, deploymentName, namespace, BODY_TWELVE, POD_LABELS);
}
}

View File

@@ -438,7 +438,8 @@ public final class Util {
}
public static void patchWithMerge(String deploymentName, String namespace, String patchBody) {
public static void patchWithMerge(String deploymentName, String namespace, String patchBody,
Map<String, String> podLabels) {
try {
PatchUtils.patch(V1Deployment.class,
() -> new AppsV1Api().patchNamespacedDeploymentCall(deploymentName, namespace,
@@ -450,10 +451,11 @@ public final class Util {
throw new RuntimeException(e);
}
waitForDeploymentAfterPatch(deploymentName, namespace);
waitForDeploymentAfterPatch(deploymentName, namespace, podLabels);
}
public static void patchWithReplace(String imageName, String deploymentName, String namespace, String patchBody) {
public static void patchWithReplace(String imageName, String deploymentName, String namespace, String patchBody,
Map<String, String> podLabels) {
String body = patchBody.replace("image_name_here", imageName);
try {
@@ -467,7 +469,7 @@ public final class Util {
throw new RuntimeException(e);
}
waitForDeploymentAfterPatch(deploymentName, namespace);
waitForDeploymentAfterPatch(deploymentName, namespace, podLabels);
}
@@ -625,10 +627,11 @@ public final class Util {
return availableReplicas != null && availableReplicas >= 1;
}
private static void waitForDeploymentAfterPatch(String deploymentName, String namespace) {
private static void waitForDeploymentAfterPatch(String deploymentName, String namespace,
Map<String, String> podLabels) {
try {
await().pollDelay(Duration.ofSeconds(4)).pollInterval(Duration.ofSeconds(3)).atMost(60, TimeUnit.SECONDS)
.until(() -> isDeploymentReadyAfterPatch(deploymentName, namespace));
.until(() -> isDeploymentReadyAfterPatch(deploymentName, namespace, podLabels));
}
catch (Exception e) {
if (e instanceof ApiException apiException) {
@@ -640,7 +643,8 @@ public final class Util {
}
private static boolean isDeploymentReadyAfterPatch(String deploymentName, String namespace) throws ApiException {
private static boolean isDeploymentReadyAfterPatch(String deploymentName, String namespace,
Map<String, String> podLabels) throws ApiException {
V1DeploymentList deployments = new AppsV1Api().listNamespacedDeployment(namespace, null, null, null,
"metadata.name=" + deploymentName, null, null, null, null, null, null);
@@ -654,6 +658,14 @@ public final class Util {
int readyReplicas = Optional.ofNullable(deployment.getStatus().getReadyReplicas()).orElse(0);
if (readyReplicas != replicas) {
LOG.info("ready replicas not yet same as replicas");
return false;
}
int pods = new CoreV1Api().listNamespacedPod(namespace, null, null, null, null, labelSelector(podLabels), null,
null, null, null, null).getItems().size();
if (pods != replicas) {
LOG.info("number of pods not yet stabilized");
return false;
}