Merge branch '3.0.x'

This commit is contained in:
Ryan Baxter
2023-09-18 08:58:14 -04:00
26 changed files with 297 additions and 494 deletions

View File

@@ -61,7 +61,6 @@
<module>spring-cloud-kubernetes-client-reactive-discoveryclient-it</module>
<module>spring-cloud-kubernetes-configuration-watcher-it</module>
<module>spring-cloud-kubernetes-core-k8s-client-it</module>
<module>spring-cloud-kubernetes-client-secrets-event-reload</module>
<module>spring-cloud-kubernetes-client-event-and-polling-reload</module>
<module>spring-cloud-kubernetes-client-configmap-event-reload-multiple-apps</module>
<module>spring-cloud-kubernetes-client-secrets-event-reload-multiple-apps</module>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -25,7 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
*/
@SpringBootApplication
@EnableConfigurationProperties({ LeftProperties.class, RightProperties.class, RightWithLabelsProperties.class,
ConfigMapProperties.class })
ConfigMapProperties.class, SecretsProperties.class })
public class App {
public static void main(String[] args) {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.configmap.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.secrets.event.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.secrets.event.reload;
package org.springframework.cloud.kubernetes.reload;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -30,9 +30,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.patchSix;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.retrySpec;
/**
* @author wind57
@@ -59,7 +56,7 @@ final class BootstrapEnabledPollingReloadConfigMapMountDelegate {
Util util, String imageName) throws Exception {
recreateMountConfigMap(util);
patchSix(deploymentName, "default", imageName);
K8sClientConfigMapReloadITUtil.patchSix(deploymentName, "default", imageName);
// (1)
Commons.waitForLogStatement("paths property sources : [/tmp/application.properties]", k3sContainer,
@@ -70,9 +67,9 @@ final class BootstrapEnabledPollingReloadConfigMapMountDelegate {
deploymentName);
// (3)
WebClient webClient = builder().baseUrl("http://localhost/mount").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
WebClient webClient = K8sClientConfigMapReloadITUtil.builder().baseUrl("http://localhost/mount").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block();
// we first read the initial value from the configmap
Assertions.assertEquals("as-mount-initial", result);
@@ -84,8 +81,9 @@ final class BootstrapEnabledPollingReloadConfigMapMountDelegate {
new CoreV1Api().replaceNamespacedConfigMap("poll-reload-as-mount", NAMESPACE, configMap, null, null, null,
null);
await().timeout(Duration.ofSeconds(180)).until(() -> webClient.method(HttpMethod.GET).retrieve()
.bodyToMono(String.class).retryWhen(retrySpec()).block().equals("as-mount-changed"));
await().timeout(Duration.ofSeconds(180))
.until(() -> webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block().equals("as-mount-changed"));
}

View File

@@ -32,10 +32,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.logs;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.patchFour;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.retrySpec;
/**
* @author wind57
@@ -59,13 +55,14 @@ final class DataChangesInConfigMapReloadDelegate {
*/
static void testSimple(String dockerImage, String deploymentName, K3sContainer k3sContainer) {
patchFour(deploymentName, NAMESPACE, dockerImage);
K8sClientConfigMapReloadITUtil.patchFour(deploymentName, NAMESPACE, dockerImage);
Commons.assertReloadLogStatements("added configmap informer for namespace",
"added secret informer for namespace", deploymentName);
WebClient webClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
WebClient webClient = K8sClientConfigMapReloadITUtil.builder().baseUrl("http://localhost/" + LEFT_NAMESPACE)
.build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block();
// we first read the initial value from the left-configmap
Assertions.assertEquals("left-initial", result);
@@ -79,13 +76,14 @@ final class DataChangesInConfigMapReloadDelegate {
replaceConfigMap(configMap);
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
WebClient innerWebClient = K8sClientConfigMapReloadITUtil.builder()
.baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block();
return "left-initial".equals(innerResult);
});
String logs = logs(deploymentName, k3sContainer);
String logs = K8sClientConfigMapReloadITUtil.logs(deploymentName, k3sContainer);
Assertions.assertTrue(logs.contains("ConfigMap left-configmap was updated in namespace left"));
Assertions.assertTrue(logs.contains("data in configmap has not changed, will not reload"));
@@ -98,9 +96,10 @@ final class DataChangesInConfigMapReloadDelegate {
replaceConfigMap(configMap);
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
WebClient innerWebClient = K8sClientConfigMapReloadITUtil.builder()
.baseUrl("http://localhost/" + LEFT_NAMESPACE).build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block();
return "left-after-change".equals(innerResult);
});

View File

@@ -45,11 +45,11 @@ import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.client.configmap.reload.BootstrapEnabledPollingReloadConfigMapMountDelegate.testBootstrapEnabledPollingReloadConfigMapMount;
import static org.springframework.cloud.kubernetes.client.configmap.reload.DataChangesInConfigMapReloadDelegate.testSimple;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.patchOne;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.patchThree;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.patchTwo;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.retrySpec;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientConfigMapReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientConfigMapReloadITUtil.patchOne;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientConfigMapReloadITUtil.patchThree;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientConfigMapReloadITUtil.patchTwo;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientConfigMapReloadITUtil.retrySpec;
import static org.springframework.cloud.kubernetes.client.configmap.reload.PollingReloadConfigMapMountDelegate.testPollingReloadConfigMapMount;
/**

View File

@@ -34,12 +34,11 @@ import static org.springframework.cloud.kubernetes.integration.tests.commons.nat
/**
* @author wind57
*/
final class K8sClientReloadITUtil {
final class K8sClientConfigMapReloadITUtil {
private static final Map<String, String> POD_LABELS = Map.of("app", "spring-k8s-client-reload");
private K8sClientReloadITUtil() {
private K8sClientConfigMapReloadITUtil() {
}
private static final String BODY_ONE = """

View File

@@ -30,9 +30,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.patchFive;
import static org.springframework.cloud.kubernetes.client.configmap.reload.K8sClientReloadITUtil.retrySpec;
/**
* @author wind57
@@ -61,7 +58,7 @@ final class PollingReloadConfigMapMountDelegate {
static void testPollingReloadConfigMapMount(String deploymentName, K3sContainer k3sContainer, Util util,
String imageName) throws Exception {
patchFive(deploymentName, "default", imageName);
K8sClientConfigMapReloadITUtil.patchFive(deploymentName, "default", imageName);
// (1)
Commons.waitForLogStatement("paths property sources : [/tmp/application.properties]", k3sContainer,
@@ -72,9 +69,9 @@ final class PollingReloadConfigMapMountDelegate {
deploymentName);
// (3)
WebClient webClient = builder().baseUrl("http://localhost/mount").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
WebClient webClient = K8sClientConfigMapReloadITUtil.builder().baseUrl("http://localhost/mount").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block();
// we first read the initial value from the configmap
Assertions.assertEquals("as-mount-initial", result);
@@ -86,8 +83,9 @@ final class PollingReloadConfigMapMountDelegate {
new CoreV1Api().replaceNamespacedConfigMap("poll-reload-as-mount", "default", configMap, null, null, null,
null);
await().timeout(Duration.ofSeconds(180)).until(() -> webClient.method(HttpMethod.GET).retrieve()
.bodyToMono(String.class).retryWhen(retrySpec()).block().equals("as-mount-changed"));
await().timeout(Duration.ofSeconds(180))
.until(() -> webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(K8sClientConfigMapReloadITUtil.retrySpec()).block().equals("as-mount-changed"));
}

View File

@@ -0,0 +1,110 @@
/*
* 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.secrets.reload;
import java.time.Duration;
import java.util.Map;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1ObjectMetaBuilder;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1SecretBuilder;
import org.junit.jupiter.api.Assertions;
import org.testcontainers.k3s.K3sContainer;
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.http.HttpMethod;
import org.springframework.web.reactive.function.client.WebClient;
import static org.awaitility.Awaitility.await;
import static org.springframework.cloud.kubernetes.client.secrets.reload.K8sClientSecretsReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.secrets.reload.K8sClientSecretsReloadITUtil.retrySpec;
final class DataChangesInSecretsReloadDelegate {
private static final String NAMESPACE = "default";
/**
* <pre>
* - secret with no labels and data: from.properties.key = initial exists in namespace default
* - we assert that we can read it correctly first, by invoking localhost/key.
*
* - then we change the secret by adding a label, this in turn does not
* change the result of localhost/key, because the data has not changed.
*
* - then we change data inside the secret, and we must see the updated value.
* </pre>
*/
static void testDataChangesInSecretsReload(K3sContainer k3sContainer, String deploymentName) {
Commons.assertReloadLogStatements("added secret informer for namespace",
"added configmap informer for namespace", deploymentName);
WebClient webClient = builder().baseUrl("http://localhost/key").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
// we first read the initial value from the secret
Assertions.assertEquals("initial", result);
// then deploy a new version of left-configmap, but without changing its data,
// only add a label
V1Secret secret = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("new-label", "abc")).withNamespace(NAMESPACE)
.withName("event-reload").build())
.withData(Map.of("application.properties", "from.properties.key=initial".getBytes())).build();
replaceSecret(secret, "event-reload");
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/key").build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
return "initial".equals(innerResult);
});
Commons.waitForLogStatement("Secret event-reload was updated in namespace default", k3sContainer,
deploymentName);
Commons.waitForLogStatement("data in secret has not changed, will not reload", k3sContainer, deploymentName);
// change data
secret = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("new-label", "abc")).withNamespace(NAMESPACE)
.withName("event-reload").build())
.withData(Map.of("application.properties", "from.properties.key=change-initial".getBytes())).build();
replaceSecret(secret, "event-reload");
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/key").build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
return "change-initial".equals(innerResult);
});
}
private static void replaceSecret(V1Secret secret, String name) {
try {
new CoreV1Api().replaceNamespacedSecret(name, NAMESPACE, secret, null, null, null, null);
}
catch (ApiException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* 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.
@@ -14,18 +14,13 @@
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.client.secrets.event.reload;
package org.springframework.cloud.kubernetes.client.secrets.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 io.kubernetes.client.openapi.apis.CoreV1Api;
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.V1Secret;
import io.kubernetes.client.openapi.models.V1Service;
@@ -33,30 +28,34 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
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.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.secrets.reload.DataChangesInSecretsReloadDelegate.testDataChangesInSecretsReload;
import static org.springframework.cloud.kubernetes.client.secrets.reload.K8sClientSecretsReloadITUtil.builder;
import static org.springframework.cloud.kubernetes.client.secrets.reload.K8sClientSecretsReloadITUtil.patchOne;
import static org.springframework.cloud.kubernetes.client.secrets.reload.K8sClientSecretsReloadITUtil.retrySpec;
/**
* @author wind57
*/
class SecretsEventReloadIT {
class K8sClientSecretsReloadIT {
private static final String PROPERTY_URL = "http://localhost:80/key";
private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-secrets-event-reload";
private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-event-and-polling-reload";
private static final String NAMESPACE = "default";
private static final String DEPLOYMENT_NAME = "spring-k8s-client-reload";
private static final String DOCKER_IMAGE = "docker.io/springcloud/" + IMAGE_NAME + ":" + Commons.pomVersion();
private static final K3sContainer K3S = Commons.container();
private static Util util;
@@ -71,30 +70,39 @@ class SecretsEventReloadIT {
util = new Util(K3S);
coreV1Api = new CoreV1Api();
util.setUp(NAMESPACE);
configK8sClientIt(Phase.CREATE);
}
@AfterAll
static void afterAll() throws Exception {
configK8sClientIt(Phase.DELETE);
Commons.cleanUp(IMAGE_NAME, K3S);
Commons.systemPrune();
}
@Test
void testSecretReload() throws Exception {
configK8sClientIt(Phase.CREATE, false);
Commons.assertReloadLogStatements("added secret informer for namespace",
"added configmap informer for namespace", IMAGE_NAME);
"added configmap informer for namespace", DEPLOYMENT_NAME);
testSecretEventReload();
configK8sClientIt(Phase.DELETE, false);
testAllOther();
}
private void testAllOther() throws Exception {
recreateSecret();
patchOne(DEPLOYMENT_NAME, NAMESPACE, DOCKER_IMAGE);
testSecretReloadConfigDisabled();
recreateSecret();
patchOne(DEPLOYMENT_NAME, NAMESPACE, DOCKER_IMAGE);
testDataChangesInSecretsReload(K3S, DEPLOYMENT_NAME);
}
@Test
void testSecretReloadConfigDisabled() throws Exception {
configK8sClientIt(Phase.CREATE, true);
Commons.assertReloadLogStatements("added secret informer for namespace",
"added configmap informer for namespace", IMAGE_NAME);
"added configmap informer for namespace", DEPLOYMENT_NAME);
testSecretEventReload();
configK8sClientIt(Phase.DELETE, true);
}
void testSecretEventReload() throws Exception {
@@ -117,22 +125,18 @@ class SecretsEventReloadIT {
.retryWhen(retrySpec()).block().equals("after-change"));
}
private void configK8sClientIt(Phase phase, boolean configDisabled) {
V1Deployment deployment = (V1Deployment) util.yaml("deployment.yaml");
private void recreateSecret() {
V1Secret secret = (V1Secret) util.yaml("secret.yaml");
util.deleteAndWait(NAMESPACE, null, secret);
util.createAndWait(NAMESPACE, null, secret);
}
private static void configK8sClientIt(Phase phase) {
V1Deployment deployment = (V1Deployment) util.yaml("deployment-with-secret.yaml");
V1Service service = (V1Service) util.yaml("service.yaml");
V1Ingress ingress = (V1Ingress) util.yaml("ingress.yaml");
V1Secret secret = (V1Secret) util.yaml("secret.yaml");
List<V1EnvVar> envVars = new ArrayList<>(
Optional.ofNullable(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv())
.orElse(List.of()));
if (configDisabled) {
V1EnvVar disableConfig = new V1EnvVar().name("SPRING_CLOUD_KUBERNETES_CONFIG_ENABLED").value("FALSE");
envVars.add(disableConfig);
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(envVars);
}
if (phase.equals(Phase.CREATE)) {
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
util.createAndWait(NAMESPACE, null, secret);
@@ -143,12 +147,4 @@ class SecretsEventReloadIT {
}
}
private WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}
private RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(60, Duration.ofSeconds(2)).filter(Objects::nonNull);
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.secrets.reload;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import reactor.netty.http.client.HttpClient;
import reactor.util.retry.Retry;
import reactor.util.retry.RetryBackoffSpec;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithReplace;
/**
* @author wind57
*/
final class K8sClientSecretsReloadITUtil {
private static final Map<String, String> POD_LABELS = Map.of("app", "spring-k8s-client-reload");
private static final String BODY_ONE = """
{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "spring-k8s-client-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_CLOUD_KUBERNETES_CONFIG_ENABLED",
"value": "FALSE"
},
{
"name": "SPRING_PROFILES_ACTIVE",
"value": "with-secret"
}
]
}]
}
}
}
}
""";
private K8sClientSecretsReloadITUtil() {
}
static WebClient.Builder builder() {
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
}
static RetryBackoffSpec retrySpec() {
return Retry.fixedDelay(60, Duration.ofSeconds(2)).filter(Objects::nonNull);
}
static void patchOne(String deploymentName, String namespace, String imageName) {
patchWithReplace(imageName, deploymentName, namespace, BODY_ONE, POD_LABELS);
}
}

View File

@@ -1,20 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-cloud-kubernetes-client-secrets-deployment-event-reload
name: spring-k8s-client-reload
spec:
selector:
matchLabels:
app: spring-cloud-kubernetes-client-secrets-event-reload
app: spring-k8s-client-reload
template:
metadata:
labels:
app: spring-cloud-kubernetes-client-secrets-event-reload
app: spring-k8s-client-reload
spec:
serviceAccountName: spring-cloud-kubernetes-serviceaccount
containers:
- name: spring-cloud-kubernetes-client-secrets-event-reload
image: docker.io/springcloud/spring-cloud-kubernetes-client-secrets-event-reload
- name: spring-k8s-client-reload
image: docker.io/springcloud/spring-cloud-kubernetes-client-event-and-polling-reload
imagePullPolicy: IfNotPresent
readinessProbe:
httpGet:
@@ -29,3 +29,5 @@ spec:
env:
- name: LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_CLIENT_CONFIG_RELOAD
value: DEBUG
- name: SPRING_PROFILES_ACTIVE
value: "with-secret"

View File

@@ -1,107 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-kubernetes-client-secrets-event-reload</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-client-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-test-support</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>../src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- build image in the 'package' phase, and ignore plain tests -->
<!-- via maven-surefire-plugin::skipTests -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<imageName>docker.io/springcloud/${project.artifactId}:${project.version}</imageName>
</configuration>
<executions>
<execution>
<id>build-image</id>
<configuration>
<skip>${skip.build.image}</skip>
</configuration>
<phase>package</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
<execution>
<id>repackage</id>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- ignore plain tests (in the 'test' phase), so that we could build the image first, see above -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- run tests in the 'integration-tests' phase, one that is after 'package' (where we build the image) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>${testsToRun}</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2013-2022 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.secrets.event.reload;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* @author wind57
*/
@SpringBootApplication
@EnableConfigurationProperties(SecretsProperties.class)
public class SecretsApp {
public static void main(String[] args) {
SpringApplication.run(SecretsApp.class, args);
}
}

View File

@@ -1,213 +0,0 @@
/*
* 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.secrets.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.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.V1Secret;
import io.kubernetes.client.openapi.models.V1SecretBuilder;
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;
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;
class DataChangesInSecretsReloadIT {
private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-secrets-event-reload";
private static final String NAMESPACE = "default";
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.setUpClusterWide(NAMESPACE, Set.of(NAMESPACE));
}
@AfterAll
static void afterAll() throws Exception {
Commons.cleanUp(IMAGE_NAME, K3S);
Commons.systemPrune();
}
/**
* <pre>
* - secret with no labels and data: from.properties.key = initial exists in namespace default
* - we assert that we can read it correctly first, by invoking localhost/key.
*
* - then we change the secret by adding a label, this in turn does not
* change the result of localhost/key, because the data has not changed.
*
* - then we change data inside the secret, and we must see the updated value.
* </pre>
*/
@Test
void testSimple() {
manifests(Phase.CREATE);
Commons.assertReloadLogStatements("added secret informer for namespace",
"added configmap informer for namespace", IMAGE_NAME);
WebClient webClient = builder().baseUrl("http://localhost/key").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
// we first read the initial value from the secret
Assertions.assertEquals("initial", result);
// then deploy a new version of left-configmap, but without changing its data,
// only add a label
V1Secret secret = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("new-label", "abc")).withNamespace(NAMESPACE)
.withName("event-reload").build())
.withData(Map.of("application.properties", "from.properties.key=initial".getBytes())).build();
replaceSecret(secret, "event-reload");
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/key").build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
return "initial".equals(innerResult);
});
String logs = logs();
Assertions.assertTrue(logs.contains("Secret event-reload was updated in namespace default"));
Assertions.assertTrue(logs.contains("data in secret has not changed, will not reload"));
// change data
secret = new V1SecretBuilder()
.withMetadata(new V1ObjectMetaBuilder().withLabels(Map.of("new-label", "abc")).withNamespace(NAMESPACE)
.withName("event-reload").build())
.withData(Map.of("application.properties", "from.properties.key=change-initial".getBytes())).build();
replaceSecret(secret, "event-reload");
await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/key").build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
return "change-initial".equals(innerResult);
});
manifests(Phase.DELETE);
}
private static void manifests(Phase phase) {
try {
V1Secret secret = (V1Secret) util.yaml("secret.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()));
V1EnvVar configDisabledEnvVar = new V1EnvVar().name("SPRING_CLOUD_KUBERNETES_CONFIG_ENABLED")
.value("FALSE");
envVars.add(configDisabledEnvVar);
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(envVars);
if (phase.equals(Phase.CREATE)) {
util.createAndWait(NAMESPACE, null, secret);
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
}
if (phase.equals(Phase.DELETE)) {
util.deleteAndWait(NAMESPACE, null, secret);
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private String logs() {
try {
String appPodName = K3S.execInContainer("sh", "-c",
"kubectl get pods -l app=" + IMAGE_NAME + " -o=name --no-headers | tr -d '\n'").getStdout();
Container.ExecResult execResult = K3S.execInContainer("sh", "-c", "kubectl logs " + appPodName.trim());
return execResult.getStdout();
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
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 replaceSecret(V1Secret secret, String name) {
try {
api.replaceNamespacedSecret(name, NAMESPACE, secret, null, null, null, null);
}
catch (ApiException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,16 +0,0 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: spring-cloud-kubernetes-client-secrets-ingress-event-reload
namespace: default
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: spring-cloud-kubernetes-client-secrets-event-reload
port:
number: 8080

View File

@@ -1,14 +0,0 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.testcontainers" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
</configuration>

View File

@@ -1,14 +0,0 @@
apiVersion: v1
kind: Service
metadata:
labels:
app: spring-cloud-kubernetes-client-secrets-event-reload
name: spring-cloud-kubernetes-client-secrets-event-reload
spec:
ports:
- name: http
port: 8080
targetPort: 8080
selector:
app: spring-cloud-kubernetes-client-secrets-event-reload
type: ClusterIP