Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fabric8 it part 5 #1470

Merged
merged 30 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spring-cloud-kubernetes-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<module>spring-cloud-kubernetes-fabric8-istio-it</module>
<module>spring-cloud-kubernetes-fabric8-client-discovery</module>
<module>spring-cloud-kubernetes-fabric8-client-loadbalancer</module>
<module>spring-cloud-kubernetes-fabric8-client-secrets-event-reload</module>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have an integration test that deals with reload in fabric8, but only for configmaps. This PR takes the secrets reload tests and moves them to the common place.

<module>spring-cloud-kubernetes-fabric8-client-reload</module>

<module>spring-cloud-kubernetes-discoveryclient-it</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
@SpringBootApplication
@EnableConfigurationProperties({ LeftProperties.class, RightProperties.class, RightWithLabelsProperties.class,
ConfigMapProperties.class })
ConfigMapProperties.class, SecretProperties.class })
public class App {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ public class Controller {

private final ConfigMapProperties configMapProperties;

private final SecretProperties secretProperties;

public Controller(LeftProperties leftProperties, RightProperties rightProperties,
RightWithLabelsProperties rightWithLabelsProperties, ConfigMapProperties configMapProperties) {
RightWithLabelsProperties rightWithLabelsProperties, ConfigMapProperties configMapProperties,
SecretProperties secretProperties) {
this.leftProperties = leftProperties;
this.rightProperties = rightProperties;
this.rightWithLabelsProperties = rightWithLabelsProperties;
this.configMapProperties = configMapProperties;
this.secretProperties = secretProperties;
}

@GetMapping("/left")
Expand All @@ -61,4 +65,9 @@ public String key() {
return configMapProperties.getKey();
}

@GetMapping("/key-from-secret")
public String keyFromSecret() {
return secretProperties.getKey();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 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.
Expand All @@ -14,15 +14,15 @@
* limitations under the License.
*/

package org.springframework.cloud.kubernetes.fabric8.secrets.event.reload;
package org.springframework.cloud.kubernetes.fabric8.reload;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author wind57
*/
@ConfigurationProperties("from.properties")
public class SecretsProperties {
@ConfigurationProperties("from.secret.properties")
public class SecretProperties {

private String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static void testPollingReloadConfigMapWithBootstrap(KubernetesClient client, Uti
appLabelValue);
// (3)
WebClient webClient = TestUtil.builder().baseUrl("http://localhost/key").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();

// we first read the initial value from the configmap
Assertions.assertEquals("as-mount-initial", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ static void testConfigMapMountPollingReload(KubernetesClient client, Util util,
appLabelValue);
// (3)
WebClient webClient = TestUtil.builder().baseUrl("http://localhost/key").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();

// we first read the initial value from the configmap
Assertions.assertEquals("as-mount-initial", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ final class ConfigMapPollingReloadDelegate {

static void testConfigMapPollingReload(KubernetesClient client) {
WebClient webClient = TestUtil.builder().baseUrl("http://localhost/key").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();

// we first read the initial value from the configmap
Assertions.assertEquals("as-mount-initial", result);
Expand All @@ -53,8 +53,8 @@ static void testConfigMapPollingReload(KubernetesClient client) {
client.configMaps().inNamespace("default").resource(map).createOrReplace();

await().ignoreException(HttpServerErrorException.BadGateway.class).timeout(Duration.ofSeconds(120))
.until(() -> webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block().equals("after-change"));
.until(() -> webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block().equals("after-change"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
Expand Down Expand Up @@ -102,15 +103,16 @@ void testInformFromOneNamespaceEventNotTriggered() {
"added secret informer for namespace", IMAGE_NAME);

WebClient webClient = TestUtil.builder().baseUrl("http://localhost/left").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();

// we first read the initial value from the left-configmap
Assertions.assertEquals("left-initial", result);

// then read the value from the right-configmap
webClient = TestUtil.builder().baseUrl("http://localhost/right").build();
result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec()).block();
result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
Assertions.assertEquals("right-initial", result);

// then deploy a new version of right-configmap
Expand All @@ -137,6 +139,7 @@ void testInformFromOneNamespaceEventNotTriggered() {
testConfigMapPollingReload();
testConfigMapMountPollingReload();
testPollingReloadConfigMapWithBootstrap();
testSecretReload();
}

/**
Expand All @@ -150,16 +153,16 @@ void testInformFromOneNamespaceEventNotTriggered() {
*/
void testInformFromOneNamespaceEventTriggered() {

TestUtil.reCreateConfigMaps(util, client);
TestUtil.reCreateSources(util, client);
TestUtil.patchOne(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);

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

// read the value from the right-configmap
WebClient webClient = TestUtil.builder().baseUrl("http://localhost/right").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();
Assertions.assertEquals("right-initial", result);

// then deploy a new version of right-configmap
Expand Down Expand Up @@ -192,7 +195,7 @@ void testInformFromOneNamespaceEventTriggered() {
*/
void testInform() {

TestUtil.reCreateConfigMaps(util, client);
TestUtil.reCreateSources(util, client);
TestUtil.patchTwo(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);

Commons.assertReloadLogStatements("added configmap informer for namespace",
Expand Down Expand Up @@ -246,8 +249,8 @@ void testInform() {

// right-configmap now will see the new value also, but only because the other
// configmap has triggered the restart
rightResult = rightWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
rightResult = rightWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();
Assertions.assertEquals("right-after-change", rightResult);
}

Expand All @@ -263,16 +266,16 @@ void testInform() {
*/
void testInformFromOneNamespaceEventTriggeredSecretsDisabled() {

TestUtil.reCreateConfigMaps(util, client);
TestUtil.reCreateSources(util, client);
TestUtil.patchThree(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);

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

// read the value from the right-configmap
WebClient webClient = TestUtil.builder().baseUrl("http://localhost/right").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(TestUtil.retrySpec())
.block();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(TestUtil.retrySpec()).block();
Assertions.assertEquals("right-initial", result);

// then deploy a new version of right-configmap
Expand All @@ -295,7 +298,7 @@ void testInformFromOneNamespaceEventTriggeredSecretsDisabled() {
}

void testDataChangesInConfigMap() {
TestUtil.reCreateConfigMaps(util, client);
TestUtil.reCreateSources(util, client);
TestUtil.patchFour(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);
DataChangesInConfigMapReloadDelegate.testDataChangesInConfigMap(client, K3S, IMAGE_NAME);
}
Expand All @@ -306,18 +309,23 @@ void testConfigMapPollingReload() {
}

void testConfigMapMountPollingReload() {
TestUtil.reCreateConfigMaps(util, client);
TestUtil.reCreateSources(util, client);
TestUtil.patchSix(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);
ConfigMapMountPollingReloadDelegate.testConfigMapMountPollingReload(client, util, K3S, IMAGE_NAME);
}

void testPollingReloadConfigMapWithBootstrap() {
TestUtil.reCreateConfigMaps(util, client);
TestUtil.reCreateSources(util, client);
TestUtil.patchSeven(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);
BootstrapEnabledPollingReloadConfigMapMountDelegate.testPollingReloadConfigMapWithBootstrap(client, util, K3S,
IMAGE_NAME);
}

void testSecretReload() {
TestUtil.patchEight(util, DOCKER_IMAGE, IMAGE_NAME, NAMESPACE);
SecretsEventsReloadDelegate.testSecretReload(client, K3S, IMAGE_NAME);
}

private static void manifests(Phase phase) {

InputStream deploymentStream = util.inputStream("deployment.yaml");
Expand All @@ -327,6 +335,7 @@ private static void manifests(Phase phase) {
InputStream rightConfigMapStream = util.inputStream("right-configmap.yaml");
InputStream rightWithLabelConfigMapStream = util.inputStream("right-configmap-with-label.yaml");
InputStream configMapAsStream = util.inputStream("configmap.yaml");
InputStream secretAsStream = util.inputStream("secret.yaml");

Deployment deployment = Serialization.unmarshal(deploymentStream, Deployment.class);

Expand All @@ -336,19 +345,20 @@ private static void manifests(Phase phase) {
ConfigMap rightConfigMap = Serialization.unmarshal(rightConfigMapStream, ConfigMap.class);
ConfigMap rightWithLabelConfigMap = Serialization.unmarshal(rightWithLabelConfigMapStream, ConfigMap.class);
ConfigMap configMap = Serialization.unmarshal(configMapAsStream, ConfigMap.class);
Secret secret = Serialization.unmarshal(secretAsStream, Secret.class);

if (phase.equals(Phase.CREATE)) {
util.createAndWait("left", leftConfigMap, null);
util.createAndWait("right", rightConfigMap, null);
util.createAndWait("right", rightWithLabelConfigMap, null);
util.createAndWait(NAMESPACE, configMap, null);
util.createAndWait(NAMESPACE, configMap, secret);
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
}
else {
util.deleteAndWait("left", leftConfigMap, null);
util.deleteAndWait("right", rightConfigMap, null);
util.deleteAndWait("right", rightWithLabelConfigMap, null);
util.deleteAndWait(NAMESPACE, configMap, null);
util.deleteAndWait(NAMESPACE, configMap, secret);
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.fabric8.reload;

import java.time.Duration;
import java.util.Base64;
import java.util.Map;

import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
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.fabric8.reload.TestUtil.builder;
import static org.springframework.cloud.kubernetes.fabric8.reload.TestUtil.retrySpec;

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

/**
* <pre>
* - secret with no labels and data: from.secret.properties.key = secret-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 testSecretReload(KubernetesClient client, K3sContainer container, String appLabelValue) {
Copy link
Contributor Author

@wind57 wind57 Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was moved from the other module

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

WebClient webClient = builder().baseUrl("http://localhost/key-from-secret").build();
String result = webClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
.block();
Assertions.assertEquals("secret-initial", result);

Secret secret = new SecretBuilder()
.withMetadata(new ObjectMetaBuilder().withLabels(Map.of("letter", "a")).withNamespace("default")
.withName("event-reload").build())
.withData(Map.of("application.properties",
Base64.getEncoder().encodeToString("from.secret.properties.key=secret-initial".getBytes())))
.build();
client.secrets().inNamespace("default").resource(secret).createOrReplace();

await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/key-from-secret").build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
return "secret-initial".equals(innerResult);
});

Commons.waitForLogStatement("Secret event-reload was updated in namespace default", container, appLabelValue);
Commons.waitForLogStatement("data in secret has not changed, will not reload", container, appLabelValue);

// change data
secret = new SecretBuilder()
.withMetadata(new ObjectMetaBuilder().withNamespace("default").withName("event-reload").build())
.withData(Map.of("application.properties",
Base64.getEncoder()
.encodeToString("from.secret.properties.key=secret-initial-changed".getBytes())))
.build();

client.secrets().inNamespace("default").resource(secret).createOrReplace();

await().pollInterval(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(90)).until(() -> {
WebClient innerWebClient = builder().baseUrl("http://localhost/key-from-secret").build();
String innerResult = innerWebClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.retryWhen(retrySpec()).block();
return "secret-initial-changed".equals(innerResult);
});

}

}
Loading
Loading