Fix 1310 for ConfigMaps (#1332)
This commit is contained in:
@@ -183,8 +183,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is empty map
|
||||
* - right is null
|
||||
* - left is empty map - right is null
|
||||
*
|
||||
* treat as equal, that is: no change
|
||||
*/
|
||||
@@ -198,8 +197,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is empty map
|
||||
* - right is null
|
||||
* - left is empty map - right is null
|
||||
*
|
||||
* treat as equal, that is: no change
|
||||
*/
|
||||
@@ -213,8 +211,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is null
|
||||
* - right is empty map
|
||||
* - left is null - right is empty map
|
||||
*
|
||||
* treat as equal, that is: no change
|
||||
*/
|
||||
@@ -228,8 +225,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is empty map
|
||||
* - right is empty map
|
||||
* - left is empty map - right is empty map
|
||||
*
|
||||
* treat as equal, that is: no change
|
||||
*/
|
||||
@@ -243,8 +239,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is empty map
|
||||
* - right is [1, b]
|
||||
* - left is empty map - right is [1, b]
|
||||
*
|
||||
* treat as non-equal, that is change
|
||||
*/
|
||||
@@ -258,8 +253,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is [1, a]
|
||||
* - right is [1, b]
|
||||
* - left is [1, a] - right is [1, b]
|
||||
*
|
||||
* treat as non-equal, that is change
|
||||
*/
|
||||
@@ -273,8 +267,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests {
|
||||
}
|
||||
|
||||
/**
|
||||
* - left is [1, a, 2 aa]
|
||||
* - right is [1, b, 2, aa]
|
||||
* - left is [1, a, 2 aa] - right is [1, b, 2, aa]
|
||||
*
|
||||
* treat as non-equal, that is change
|
||||
*/
|
||||
|
||||
@@ -99,6 +99,7 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo
|
||||
|
||||
private void addPropertySourcesFromPaths(Environment environment, CompositePropertySource composite) {
|
||||
Set<String> uniquePaths = new LinkedHashSet<>(properties.paths());
|
||||
LOG.debug("paths property sources : " + uniquePaths);
|
||||
uniquePaths.stream().map(Paths::get).filter(p -> {
|
||||
boolean exists = Files.exists(p);
|
||||
if (!exists) {
|
||||
@@ -139,7 +140,8 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo
|
||||
LOG.warn("Property source: " + name + "will be ignored because no properties could be found");
|
||||
}
|
||||
else {
|
||||
composite.addFirstPropertySource(new MapPropertySource(name, map));
|
||||
LOG.debug("will add file-based property source : " + name);
|
||||
composite.addFirstPropertySource(new MountConfigMapPropertySource(name, map));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,9 @@ import java.util.Map;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
public final class MountConfigMapPropertySource extends MapPropertySource {
|
||||
|
||||
public MountConfigMapPropertySource(String name, Map<String, Object> source) {
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
|
||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
@@ -73,8 +74,8 @@ public final class ConfigReloadUtil {
|
||||
|
||||
List<PropertySource<?>> sources = environment.getPropertySources().stream()
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
LOG.debug(() -> "environment: " + environment);
|
||||
LOG.debug(() -> "environment sources: " + sources);
|
||||
LOG.debug(() -> "environment from findPropertySources: " + environment);
|
||||
LOG.debug(() -> "environment sources from findPropertySources : " + sources);
|
||||
|
||||
while (!sources.isEmpty()) {
|
||||
PropertySource<?> source = sources.remove(0);
|
||||
@@ -84,14 +85,24 @@ public final class ConfigReloadUtil {
|
||||
else if (sourceClass.isInstance(source)) {
|
||||
managedSources.add(sourceClass.cast(source));
|
||||
}
|
||||
else if (source instanceof MountConfigMapPropertySource mountConfigMapPropertySource) {
|
||||
// we know that the type is correct here
|
||||
managedSources.add((S) mountConfigMapPropertySource);
|
||||
}
|
||||
else if (source instanceof BootstrapPropertySource<?> bootstrapPropertySource) {
|
||||
PropertySource<?> propertySource = bootstrapPropertySource.getDelegate();
|
||||
LOG.debug(() -> "bootstrap delegate class : " + propertySource.getClass());
|
||||
if (sourceClass.isInstance(propertySource)) {
|
||||
sources.add(propertySource);
|
||||
}
|
||||
else if (propertySource instanceof MountConfigMapPropertySource mountConfigMapPropertySource) {
|
||||
// we know that the type is correct here
|
||||
managedSources.add((S) mountConfigMapPropertySource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG.debug(() -> "findPropertySources : " + managedSources.stream().map(PropertySource::getName).toList());
|
||||
return managedSources;
|
||||
}
|
||||
|
||||
@@ -123,17 +134,14 @@ public final class ConfigReloadUtil {
|
||||
LOG.debug(() -> "Found property source that cannot be handled: " + propertySource.getClass());
|
||||
}
|
||||
|
||||
LOG.debug(() -> "environment: " + environment);
|
||||
LOG.debug(() -> "sources: " + result);
|
||||
LOG.debug(() -> "environment from locateMapPropertySources : " + environment);
|
||||
LOG.debug(() -> "sources from locateMapPropertySources : " + result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static boolean changed(List<? extends MapPropertySource> left, List<? extends MapPropertySource> right) {
|
||||
if (left.size() != right.size()) {
|
||||
LOG.warn(() -> "The current number of ConfigMap PropertySources does not match "
|
||||
+ "the ones loaded from the Kubernetes - No reload will take place");
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("left size: " + left.size());
|
||||
left.forEach(item -> LOG.debug(item.toString()));
|
||||
@@ -141,14 +149,20 @@ public final class ConfigReloadUtil {
|
||||
LOG.debug("right size: " + right.size());
|
||||
right.forEach(item -> LOG.debug(item.toString()));
|
||||
}
|
||||
LOG.warn(() -> "The current number of ConfigMap PropertySources does not match "
|
||||
+ "the ones loaded from Kubernetes - No reload will take place");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < left.size(); i++) {
|
||||
if (changed(left.get(i), right.get(i))) {
|
||||
MapPropertySource leftPropertySource = left.get(i);
|
||||
MapPropertySource rightPropertySource = right.get(i);
|
||||
if (changed(leftPropertySource, rightPropertySource)) {
|
||||
LOG.debug(() -> "found change in : " + leftPropertySource);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
LOG.debug(() -> "no changes found, reload will not happen");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cloud.bootstrap.config.BootstrapPropertySource;
|
||||
import org.springframework.cloud.kubernetes.commons.config.MountConfigMapPropertySource;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
@@ -138,12 +139,15 @@ class ConfigReloadUtilTests {
|
||||
return null;
|
||||
}
|
||||
}));
|
||||
propertySources.addFirst(new MountConfigMapPropertySource("mounted", Map.of("a", "b")));
|
||||
|
||||
List<PlainPropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class, environment);
|
||||
Assertions.assertEquals(3, result.size());
|
||||
Assertions.assertEquals("plain", result.get(0).getProperty(""));
|
||||
Assertions.assertEquals("from-bootstrap", result.get(1).getProperty(""));
|
||||
Assertions.assertEquals("from-inner-two-composite", result.get(2).getProperty(""));
|
||||
List<? extends PropertySource> result = ConfigReloadUtil.findPropertySources(PlainPropertySource.class,
|
||||
environment);
|
||||
Assertions.assertEquals(4, result.size());
|
||||
Assertions.assertEquals("b", result.get(0).getProperty("a"));
|
||||
Assertions.assertEquals("plain", result.get(1).getProperty(""));
|
||||
Assertions.assertEquals("from-bootstrap", result.get(2).getProperty(""));
|
||||
Assertions.assertEquals("from-inner-two-composite", result.get(3).getProperty(""));
|
||||
}
|
||||
|
||||
private static final class OneComposite extends CompositePropertySource {
|
||||
|
||||
@@ -70,5 +70,6 @@
|
||||
<module>spring-cloud-kubernetes-client-catalog-watcher</module>
|
||||
<module>spring-cloud-kubernetes-client-discovery-it</module>
|
||||
<module>spring-cloud-kubernetes-fabric8-client-discovery-with-bootstrap</module>
|
||||
</modules>
|
||||
<module>spring-cloud-kubernetes-client-configmap-polling-reload</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-kubernetes-integration-tests</artifactId>
|
||||
<version>3.0.3-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-cloud-kubernetes-client-configmap-polling-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>
|
||||
|
||||
<!-- not bootstrap starter, because we want to be able to disable bootstrap per-test -->
|
||||
<!-- and ConditionalOnBootstrapEnabled has a @ConditionalOnClass on a Marker -->
|
||||
<!-- that is present in bootstrap-starter, but not in this one -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter</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>
|
||||
<imageBuilder>paketobuildpacks/builder</imageBuilder>
|
||||
</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>
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.polling.reload;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(ConfigMapProperties.class)
|
||||
public class ConfigMapApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConfigMapApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.polling.reload;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@RestController
|
||||
public class ConfigMapController {
|
||||
|
||||
private final ConfigMapProperties properties;
|
||||
|
||||
public ConfigMapController(ConfigMapProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@GetMapping("/key")
|
||||
public String key() {
|
||||
return properties.getKey();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.polling.reload;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@ConfigurationProperties("from.properties")
|
||||
public class ConfigMapProperties {
|
||||
|
||||
private String key;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key1) {
|
||||
this.key = key1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
spring:
|
||||
application:
|
||||
name: poll-reload-mount
|
||||
cloud:
|
||||
kubernetes:
|
||||
reload:
|
||||
enabled: true
|
||||
monitoring-config-maps: true
|
||||
strategy: shutdown
|
||||
mode: polling
|
||||
period: 5000
|
||||
config:
|
||||
paths:
|
||||
- /tmp/application.properties
|
||||
config:
|
||||
import: "kubernetes:"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
spring:
|
||||
application:
|
||||
name: poll-reload-mount-boostrap
|
||||
cloud:
|
||||
kubernetes:
|
||||
reload:
|
||||
enabled: true
|
||||
monitoring-config-maps: true
|
||||
strategy: shutdown
|
||||
mode: polling
|
||||
period: 5000
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
cloud:
|
||||
kubernetes:
|
||||
config:
|
||||
paths:
|
||||
- /tmp/application.properties
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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.polling.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.V1ConfigMap;
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
class BootstrapEnabledPollingReloadConfigMapMountIT {
|
||||
|
||||
private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-configmap-polling-reload";
|
||||
|
||||
private static final String NAMESPACE = "default";
|
||||
|
||||
private static Util util;
|
||||
|
||||
private static CoreV1Api coreV1Api;
|
||||
|
||||
private static final K3sContainer K3S = Commons.container();
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() throws Exception {
|
||||
K3S.start();
|
||||
Commons.validateImage(IMAGE_NAME, K3S);
|
||||
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
|
||||
util = new Util(K3S);
|
||||
coreV1Api = new CoreV1Api();
|
||||
util.setUp(NAMESPACE);
|
||||
manifests(Phase.CREATE);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void after() throws Exception {
|
||||
manifests(Phase.DELETE);
|
||||
Commons.cleanUp(IMAGE_NAME, K3S);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - we have bootstrap enabled, which means we will 'locate' property sources
|
||||
* from config maps.
|
||||
* - there are no explicit config maps to search for, but what we will also read,
|
||||
* is 'spring.cloud.kubernetes.config.paths', which we have set to
|
||||
* '/tmp/application.properties'
|
||||
* in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
|
||||
* - we first assert that we are actually reading the path based source via (1), (2) and (3).
|
||||
*
|
||||
* - we then change the config map content, wait for k8s to pick it up and replace them
|
||||
* - our polling will then detect that change, and trigger a reload.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void test() throws Exception {
|
||||
String logs = logs();
|
||||
// (1)
|
||||
Assertions.assertTrue(logs.contains("paths property sources : [/tmp/application.properties]"));
|
||||
// (2)
|
||||
Assertions.assertTrue(logs.contains("will add file-based property source : /tmp/application.properties"));
|
||||
// (3)
|
||||
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 configmap
|
||||
Assertions.assertEquals("as-mount-initial", result);
|
||||
|
||||
// replace data in configmap and wait for k8s to pick it up
|
||||
// our polling will detect that and restart the app
|
||||
V1ConfigMap configMap = (V1ConfigMap) util.yaml("configmap-mount.yaml");
|
||||
configMap.setData(Map.of("application.properties", "from.properties.key=as-mount-changed"));
|
||||
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"));
|
||||
|
||||
}
|
||||
|
||||
private static void manifests(Phase phase) {
|
||||
|
||||
V1Deployment deployment = (V1Deployment) util.yaml("deployment-mount.yaml");
|
||||
V1Service service = (V1Service) util.yaml("service.yaml");
|
||||
V1Ingress ingress = (V1Ingress) util.yaml("ingress.yaml");
|
||||
V1ConfigMap configMap = (V1ConfigMap) util.yaml("configmap-mount.yaml");
|
||||
|
||||
List<V1EnvVar> existing = new ArrayList<>(
|
||||
Optional.ofNullable(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv())
|
||||
.orElse(new ArrayList<>()));
|
||||
|
||||
// bootstrap is enabled, which means that in 'application-with-bootstrap.yaml',
|
||||
// config-data support is disabled.
|
||||
V1EnvVar mountActiveProfile = new V1EnvVar().name("SPRING_PROFILES_ACTIVE").value("with-bootstrap");
|
||||
V1EnvVar disableBootstrap = new V1EnvVar().name("SPRING_CLOUD_BOOTSTRAP_ENABLED").value("TRUE");
|
||||
|
||||
V1EnvVar debugLevelReloadCommons = new V1EnvVar()
|
||||
.name("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD").value("DEBUG");
|
||||
V1EnvVar debugLevelConfig = new V1EnvVar()
|
||||
.name("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG").value("DEBUG");
|
||||
V1EnvVar debugLevelCommons = new V1EnvVar().name("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS")
|
||||
.value("DEBUG");
|
||||
|
||||
existing.add(mountActiveProfile);
|
||||
existing.add(disableBootstrap);
|
||||
existing.add(debugLevelReloadCommons);
|
||||
existing.add(debugLevelCommons);
|
||||
existing.add(debugLevelConfig);
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(existing);
|
||||
|
||||
if (phase.equals(Phase.CREATE)) {
|
||||
util.createAndWait(NAMESPACE, configMap, null);
|
||||
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
|
||||
}
|
||||
else {
|
||||
util.deleteAndWait(NAMESPACE, configMap, null);
|
||||
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private WebClient.Builder builder() {
|
||||
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
|
||||
}
|
||||
|
||||
private RetryBackoffSpec retrySpec() {
|
||||
return Retry.fixedDelay(60, Duration.ofSeconds(1)).filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* 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.polling.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.V1ConfigMap;
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
class PollingReloadConfigMapMountIT {
|
||||
|
||||
private static final String IMAGE_NAME = "spring-cloud-kubernetes-client-configmap-polling-reload";
|
||||
|
||||
private static final String NAMESPACE = "default";
|
||||
|
||||
private static Util util;
|
||||
|
||||
private static CoreV1Api coreV1Api;
|
||||
|
||||
private static final K3sContainer K3S = Commons.container();
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() throws Exception {
|
||||
K3S.start();
|
||||
Commons.validateImage(IMAGE_NAME, K3S);
|
||||
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
|
||||
util = new Util(K3S);
|
||||
coreV1Api = new CoreV1Api();
|
||||
util.setUp(NAMESPACE);
|
||||
manifests(Phase.CREATE);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void after() throws Exception {
|
||||
manifests(Phase.DELETE);
|
||||
Commons.cleanUp(IMAGE_NAME, K3S);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - we have "spring.config.import: kubernetes", which means we will 'locate' property sources
|
||||
* from config maps.
|
||||
* - the property above means that at the moment we will be searching for config maps that only
|
||||
* match the application name, in this specific test there is no such config map.
|
||||
* - what we will also read, is 'spring.cloud.kubernetes.config.paths', which we have set to
|
||||
* '/tmp/application.properties'
|
||||
* in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
|
||||
* - we first assert that we are actually reading the path based source via (1), (2) and (3).
|
||||
*
|
||||
* - we then change the config map content, wait for k8s to pick it up and replace them
|
||||
* - our polling will then detect that change, and trigger a reload.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void test() throws Exception {
|
||||
String logs = logs();
|
||||
// (1)
|
||||
Assertions.assertTrue(logs.contains("paths property sources : [/tmp/application.properties]"));
|
||||
// (2)
|
||||
Assertions.assertTrue(logs.contains("will add file-based property source : /tmp/application.properties"));
|
||||
// (3)
|
||||
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 configmap
|
||||
Assertions.assertEquals("as-mount-initial", result);
|
||||
|
||||
// replace data in configmap and wait for k8s to pick it up
|
||||
// our polling will detect that and restart the app
|
||||
V1ConfigMap configMap = (V1ConfigMap) util.yaml("configmap-mount.yaml");
|
||||
configMap.setData(Map.of("application.properties", "from.properties.key=as-mount-changed"));
|
||||
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"));
|
||||
|
||||
}
|
||||
|
||||
private static void manifests(Phase phase) {
|
||||
|
||||
V1Deployment deployment = (V1Deployment) util.yaml("deployment-mount.yaml");
|
||||
V1Service service = (V1Service) util.yaml("service.yaml");
|
||||
V1Ingress ingress = (V1Ingress) util.yaml("ingress.yaml");
|
||||
V1ConfigMap configMap = (V1ConfigMap) util.yaml("configmap-mount.yaml");
|
||||
|
||||
List<V1EnvVar> existing = new ArrayList<>(
|
||||
Optional.ofNullable(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv())
|
||||
.orElse(new ArrayList<>()));
|
||||
|
||||
// bootstrap is disabled, which means that in 'application-mount.yaml',
|
||||
// config-data support is enabled.
|
||||
V1EnvVar mountActiveProfile = new V1EnvVar().name("SPRING_PROFILES_ACTIVE").value("mount");
|
||||
V1EnvVar disableBootstrap = new V1EnvVar().name("SPRING_CLOUD_BOOTSTRAP_ENABLED").value("FALSE");
|
||||
|
||||
V1EnvVar debugLevelReloadCommons = new V1EnvVar()
|
||||
.name("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD").value("DEBUG");
|
||||
V1EnvVar debugLevelConfig = new V1EnvVar()
|
||||
.name("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG").value("DEBUG");
|
||||
V1EnvVar debugLevelCommons = new V1EnvVar().name("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS")
|
||||
.value("DEBUG");
|
||||
|
||||
existing.add(mountActiveProfile);
|
||||
existing.add(disableBootstrap);
|
||||
existing.add(debugLevelReloadCommons);
|
||||
existing.add(debugLevelCommons);
|
||||
existing.add(debugLevelConfig);
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(existing);
|
||||
|
||||
if (phase.equals(Phase.CREATE)) {
|
||||
util.createAndWait(NAMESPACE, configMap, null);
|
||||
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
|
||||
}
|
||||
else {
|
||||
util.deleteAndWait(NAMESPACE, configMap, null);
|
||||
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private WebClient.Builder builder() {
|
||||
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
|
||||
}
|
||||
|
||||
private RetryBackoffSpec retrySpec() {
|
||||
return Retry.fixedDelay(60, Duration.ofSeconds(1)).filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: poll-reload-as-mount
|
||||
namespace: default
|
||||
data:
|
||||
application.properties: |
|
||||
from.properties.key=as-mount-initial
|
||||
@@ -0,0 +1,37 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: spring-cloud-kubernetes-client-configmap-deployment-polling-reload
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
spec:
|
||||
serviceAccountName: spring-cloud-kubernetes-serviceaccount
|
||||
containers:
|
||||
- name: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
image: docker.io/springcloud/spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
imagePullPolicy: IfNotPresent
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
port: 8080
|
||||
path: /actuator/health/readiness
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
port: 8080
|
||||
path: /actuator/health/liveness
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
|
||||
volumeMounts:
|
||||
- name: config-map-volume
|
||||
mountPath: /tmp
|
||||
|
||||
volumes:
|
||||
- name: config-map-volume
|
||||
configMap:
|
||||
name: poll-reload-as-mount
|
||||
@@ -0,0 +1,16 @@
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: spring-cloud-kubernetes-client-configmap-ingress-polling-reload
|
||||
namespace: default
|
||||
spec:
|
||||
rules:
|
||||
- http:
|
||||
paths:
|
||||
- path: /
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
port:
|
||||
number: 8080
|
||||
@@ -0,0 +1,15 @@
|
||||
<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"/>
|
||||
<logger name="io.fabric8.kubernetes.client" level="ERROR"/>
|
||||
</configuration>
|
||||
@@ -0,0 +1,14 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
labels:
|
||||
app: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
name: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
spec:
|
||||
ports:
|
||||
- name: http
|
||||
port: 8080
|
||||
targetPort: 8080
|
||||
selector:
|
||||
app: spring-cloud-kubernetes-client-configmap-polling-reload
|
||||
type: ClusterIP
|
||||
@@ -26,6 +26,14 @@
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- not bootstrap starter, because we want to be able to disable bootstrap per-test -->
|
||||
<!-- and ConditionalOnBootstrapEnabled has a @ConditionalOnClass on a Marker -->
|
||||
<!-- that is present in bootstrap-starter, but not in this one -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-kubernetes-test-support</artifactId>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
spring:
|
||||
application:
|
||||
name: poll-reload-mount
|
||||
cloud:
|
||||
kubernetes:
|
||||
reload:
|
||||
enabled: true
|
||||
monitoring-config-maps: true
|
||||
strategy: shutdown
|
||||
mode: polling
|
||||
period: 5000
|
||||
config:
|
||||
paths:
|
||||
- /tmp/application.properties
|
||||
config:
|
||||
import: "kubernetes:"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
spring:
|
||||
application:
|
||||
name: poll-reload-mount-boostrap
|
||||
cloud:
|
||||
kubernetes:
|
||||
reload:
|
||||
enabled: true
|
||||
monitoring-config-maps: true
|
||||
strategy: shutdown
|
||||
mode: polling
|
||||
period: 5000
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
cloud:
|
||||
kubernetes:
|
||||
config:
|
||||
paths:
|
||||
- /tmp/application.properties
|
||||
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* 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.fabric8.configmap.polling.reload;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||
import io.fabric8.kubernetes.api.model.EnvVar;
|
||||
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
|
||||
import io.fabric8.kubernetes.api.model.Service;
|
||||
import io.fabric8.kubernetes.api.model.apps.Deployment;
|
||||
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.Container;
|
||||
import org.testcontainers.k3s.K3sContainer;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.util.retry.Retry;
|
||||
import reactor.util.retry.RetryBackoffSpec;
|
||||
|
||||
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
|
||||
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
|
||||
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
|
||||
public class BootstrapEnabledPollingReloadConfigMapMountIT {
|
||||
|
||||
private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-client-configmap-polling-reload";
|
||||
|
||||
private static final String NAMESPACE = "default";
|
||||
|
||||
private static Util util;
|
||||
|
||||
private static KubernetesClient client;
|
||||
|
||||
private static final K3sContainer K3S = Commons.container();
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() throws Exception {
|
||||
K3S.start();
|
||||
Commons.validateImage(IMAGE_NAME, K3S);
|
||||
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
|
||||
util = new Util(K3S);
|
||||
client = util.client();
|
||||
util.setUp(NAMESPACE);
|
||||
manifests(Phase.CREATE);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void after() throws Exception {
|
||||
manifests(Phase.DELETE);
|
||||
Commons.cleanUp(IMAGE_NAME, K3S);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - we have bootstrap enabled, which means we will 'locate' property sources
|
||||
* from config maps.
|
||||
* - there are no explicit config maps to search for, but what we will also read,
|
||||
* is 'spring.cloud.kubernetes.config.paths', which we have set to
|
||||
* '/tmp/application.properties'
|
||||
* in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
|
||||
* - we first assert that we are actually reading the path based source via (1), (2) and (3).
|
||||
*
|
||||
* - we then change the config map content, wait for k8s to pick it up and replace them
|
||||
* - our polling will then detect that change, and trigger a reload.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void test() {
|
||||
String logs = logs();
|
||||
// (1)
|
||||
Assertions.assertTrue(logs.contains("paths property sources : [/tmp/application.properties]"));
|
||||
// (2)
|
||||
Assertions.assertTrue(logs.contains("will add file-based property source : /tmp/application.properties"));
|
||||
// (3)
|
||||
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 configmap
|
||||
Assertions.assertEquals("as-mount-initial", result);
|
||||
|
||||
// replace data in configmap and wait for k8s to pick it up
|
||||
// our polling will detect that and restart the app
|
||||
InputStream configMapStream = util.inputStream("mount/configmap-mount.yaml");
|
||||
ConfigMap configMap = client.configMaps().load(configMapStream).get();
|
||||
configMap.setData(Map.of("application.properties", "from.properties.key=as-mount-changed"));
|
||||
client.configMaps().inNamespace("default").resource(configMap).createOrReplace();
|
||||
|
||||
await().timeout(Duration.ofSeconds(360)).until(() -> webClient.method(HttpMethod.GET).retrieve()
|
||||
.bodyToMono(String.class).retryWhen(retrySpec()).block().equals("as-mount-changed"));
|
||||
|
||||
}
|
||||
|
||||
private static void manifests(Phase phase) {
|
||||
|
||||
InputStream deploymentStream = util.inputStream("mount/deployment-mount.yaml");
|
||||
InputStream serviceStream = util.inputStream("service.yaml");
|
||||
InputStream ingressStream = util.inputStream("ingress.yaml");
|
||||
InputStream configMapStream = util.inputStream("mount/configmap-mount.yaml");
|
||||
|
||||
Deployment deployment = client.apps().deployments().load(deploymentStream).get();
|
||||
Service service = client.services().load(serviceStream).get();
|
||||
Ingress ingress = client.network().v1().ingresses().load(ingressStream).get();
|
||||
ConfigMap configMap = client.configMaps().load(configMapStream).get();
|
||||
|
||||
List<EnvVar> existing = new ArrayList<>(
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv());
|
||||
|
||||
// bootstrap is enabled, which means that in 'application-with-bootstrap.yaml',
|
||||
// config-data support is disabled.
|
||||
EnvVar withBootstrapActiveProfile = new EnvVarBuilder().withName("SPRING_PROFILES_ACTIVE")
|
||||
.withValue("with-bootstrap").build();
|
||||
EnvVar enabledBootstrap = new EnvVarBuilder().withName("SPRING_CLOUD_BOOTSTRAP_ENABLED").withValue("TRUE")
|
||||
.build();
|
||||
|
||||
EnvVar debugLevelReloadCommons = new EnvVarBuilder()
|
||||
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD").withValue("DEBUG")
|
||||
.build();
|
||||
EnvVar debugLevelConfig = new EnvVarBuilder()
|
||||
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG").withValue("DEBUG")
|
||||
.build();
|
||||
EnvVar debugLevelCommons = new EnvVarBuilder()
|
||||
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS").withValue("DEBUG").build();
|
||||
|
||||
existing.add(withBootstrapActiveProfile);
|
||||
existing.add(enabledBootstrap);
|
||||
existing.add(debugLevelReloadCommons);
|
||||
existing.add(debugLevelCommons);
|
||||
existing.add(debugLevelConfig);
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(existing);
|
||||
|
||||
if (phase.equals(Phase.CREATE)) {
|
||||
util.createAndWait(NAMESPACE, configMap, null);
|
||||
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
|
||||
}
|
||||
else {
|
||||
util.deleteAndWait(NAMESPACE, configMap, null);
|
||||
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private WebClient.Builder builder() {
|
||||
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
|
||||
}
|
||||
|
||||
private RetryBackoffSpec retrySpec() {
|
||||
return Retry.fixedDelay(60, Duration.ofSeconds(1)).filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,11 +18,15 @@ package org.springframework.cloud.kubernetes.fabric8.configmap.polling.reload;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
|
||||
import io.fabric8.kubernetes.api.model.EnvVar;
|
||||
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
|
||||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
|
||||
import io.fabric8.kubernetes.api.model.Service;
|
||||
import io.fabric8.kubernetes.api.model.apps.Deployment;
|
||||
@@ -112,6 +116,11 @@ class ConfigMapPollingReloadIT {
|
||||
Ingress ingress = client.network().v1().ingresses().load(ingressStream).get();
|
||||
ConfigMap configMap = client.configMaps().load(configMapStream).get();
|
||||
|
||||
List<EnvVar> existing = new ArrayList<>(
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv());
|
||||
existing.add(new EnvVarBuilder().withName("SPRING_PROFILES_ACTIVE").withValue("no-mount").build());
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(existing);
|
||||
|
||||
if (phase.equals(Phase.CREATE)) {
|
||||
util.createAndWait(NAMESPACE, configMap, null);
|
||||
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* 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.fabric8.configmap.polling.reload;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||
import io.fabric8.kubernetes.api.model.EnvVar;
|
||||
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
|
||||
import io.fabric8.kubernetes.api.model.Service;
|
||||
import io.fabric8.kubernetes.api.model.apps.Deployment;
|
||||
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.Container;
|
||||
import org.testcontainers.k3s.K3sContainer;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.util.retry.Retry;
|
||||
import reactor.util.retry.RetryBackoffSpec;
|
||||
|
||||
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
|
||||
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
|
||||
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
class PollingReloadConfigMapMountIT {
|
||||
|
||||
private static final String IMAGE_NAME = "spring-cloud-kubernetes-fabric8-client-configmap-polling-reload";
|
||||
|
||||
private static final String NAMESPACE = "default";
|
||||
|
||||
private static Util util;
|
||||
|
||||
private static KubernetesClient client;
|
||||
|
||||
private static final K3sContainer K3S = Commons.container();
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() throws Exception {
|
||||
K3S.start();
|
||||
Commons.validateImage(IMAGE_NAME, K3S);
|
||||
Commons.loadSpringCloudKubernetesImage(IMAGE_NAME, K3S);
|
||||
util = new Util(K3S);
|
||||
client = util.client();
|
||||
util.setUp(NAMESPACE);
|
||||
manifests(Phase.CREATE);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void after() throws Exception {
|
||||
manifests(Phase.DELETE);
|
||||
Commons.cleanUp(IMAGE_NAME, K3S);
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - we have "spring.config.import: kubernetes", which means we will 'locate' property sources
|
||||
* from config maps.
|
||||
* - the property above means that at the moment we will be searching for config maps that only
|
||||
* match the application name, in this specific test there is no such config map.
|
||||
* - what we will also read, is 'spring.cloud.kubernetes.config.paths', which we have set to
|
||||
* '/tmp/application.properties'
|
||||
* in this test. That is populated by the volumeMounts (see deployment-mount.yaml)
|
||||
* - we first assert that we are actually reading the path based source via (1), (2) and (3).
|
||||
*
|
||||
* - we then change the config map content, wait for k8s to pick it up and replace them
|
||||
* - our polling will then detect that change, and trigger a reload.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
void test() {
|
||||
String logs = logs();
|
||||
// (1)
|
||||
Assertions.assertTrue(logs.contains("paths property sources : [/tmp/application.properties]"));
|
||||
// (2)
|
||||
Assertions.assertTrue(logs.contains("will add file-based property source : /tmp/application.properties"));
|
||||
// (3)
|
||||
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 configmap
|
||||
Assertions.assertEquals("as-mount-initial", result);
|
||||
|
||||
// replace data in configmap and wait for k8s to pick it up
|
||||
// our polling will detect that and restart the app
|
||||
InputStream configMapStream = util.inputStream("mount/configmap-mount.yaml");
|
||||
ConfigMap configMap = client.configMaps().load(configMapStream).get();
|
||||
configMap.setData(Map.of("application.properties", "from.properties.key=as-mount-changed"));
|
||||
client.configMaps().inNamespace("default").resource(configMap).createOrReplace();
|
||||
|
||||
await().timeout(Duration.ofSeconds(360)).until(() -> webClient.method(HttpMethod.GET).retrieve()
|
||||
.bodyToMono(String.class).retryWhen(retrySpec()).block().equals("as-mount-changed"));
|
||||
|
||||
}
|
||||
|
||||
private static void manifests(Phase phase) {
|
||||
|
||||
InputStream deploymentStream = util.inputStream("mount/deployment-mount.yaml");
|
||||
InputStream serviceStream = util.inputStream("service.yaml");
|
||||
InputStream ingressStream = util.inputStream("ingress.yaml");
|
||||
InputStream configMapStream = util.inputStream("mount/configmap-mount.yaml");
|
||||
|
||||
Deployment deployment = client.apps().deployments().load(deploymentStream).get();
|
||||
Service service = client.services().load(serviceStream).get();
|
||||
Ingress ingress = client.network().v1().ingresses().load(ingressStream).get();
|
||||
ConfigMap configMap = client.configMaps().load(configMapStream).get();
|
||||
|
||||
List<EnvVar> existing = new ArrayList<>(
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv());
|
||||
|
||||
// bootstrap is disabled, which means that in 'application-mount.yaml',
|
||||
// config-data support is enabled.
|
||||
EnvVar mountActiveProfile = new EnvVarBuilder().withName("SPRING_PROFILES_ACTIVE").withValue("mount").build();
|
||||
EnvVar disableBootstrap = new EnvVarBuilder().withName("SPRING_CLOUD_BOOTSTRAP_ENABLED").withValue("FALSE")
|
||||
.build();
|
||||
|
||||
EnvVar debugLevelReloadCommons = new EnvVarBuilder()
|
||||
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG_RELOAD").withValue("DEBUG")
|
||||
.build();
|
||||
EnvVar debugLevelConfig = new EnvVarBuilder()
|
||||
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS_CONFIG").withValue("DEBUG")
|
||||
.build();
|
||||
EnvVar debugLevelCommons = new EnvVarBuilder()
|
||||
.withName("LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_CLOUD_KUBERNETES_COMMONS").withValue("DEBUG").build();
|
||||
|
||||
existing.add(mountActiveProfile);
|
||||
existing.add(disableBootstrap);
|
||||
existing.add(debugLevelReloadCommons);
|
||||
existing.add(debugLevelCommons);
|
||||
existing.add(debugLevelConfig);
|
||||
deployment.getSpec().getTemplate().getSpec().getContainers().get(0).setEnv(existing);
|
||||
|
||||
if (phase.equals(Phase.CREATE)) {
|
||||
util.createAndWait(NAMESPACE, configMap, null);
|
||||
util.createAndWait(NAMESPACE, null, deployment, service, ingress, true);
|
||||
}
|
||||
else {
|
||||
util.deleteAndWait(NAMESPACE, configMap, null);
|
||||
util.deleteAndWait(NAMESPACE, deployment, service, ingress);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private WebClient.Builder builder() {
|
||||
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
|
||||
}
|
||||
|
||||
private RetryBackoffSpec retrySpec() {
|
||||
return Retry.fixedDelay(60, Duration.ofSeconds(1)).filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: poll-reload-as-mount
|
||||
namespace: default
|
||||
data:
|
||||
application.properties: |
|
||||
from.properties.key=as-mount-initial
|
||||
@@ -0,0 +1,37 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: spring-cloud-kubernetes-fabric8-client-configmap-deployment-polling-reload
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: spring-cloud-kubernetes-fabric8-client-configmap-polling-reload
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: spring-cloud-kubernetes-fabric8-client-configmap-polling-reload
|
||||
spec:
|
||||
serviceAccountName: spring-cloud-kubernetes-serviceaccount
|
||||
containers:
|
||||
- name: spring-cloud-kubernetes-fabric8-client-configmap-polling-reload
|
||||
image: docker.io/springcloud/spring-cloud-kubernetes-fabric8-client-configmap-polling-reload
|
||||
imagePullPolicy: IfNotPresent
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
port: 8080
|
||||
path: /actuator/health/readiness
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
port: 8080
|
||||
path: /actuator/health/liveness
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
|
||||
volumeMounts:
|
||||
- name: config-map-volume
|
||||
mountPath: /tmp
|
||||
|
||||
volumes:
|
||||
- name: config-map-volume
|
||||
configMap:
|
||||
name: poll-reload-as-mount
|
||||
Reference in New Issue
Block a user