Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.kubernetes.commons.config.reload;

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

import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -27,15 +28,14 @@
*
* @param enabled Enables the Kubernetes configuration reload on change.
* @param monitoringConfigMaps Enables monitoring on secrets to detect changes.
* @param configMapsLabels Labels for which to watch config maps.
* @param monitoringSecrets Monitor secrets or not.
* @param secretsLabels Labels for which to watch secrets for.
* @param strategy Sets reload strategy for Kubernetes configuration reload on change.
* @param mode Sets the detection mode for Kubernetes configuration reload.
* @param period Sets the polling period to use when the detection mode is POLLING.
* @param namespaces namespaces where an informer will be set-up. this property is only
* relevant for event based reloading.
* @param enableReloadFiltering create an informer only for sources that have
* 'spring.cloud.kubernetes.config.informer.enabled=true' label. This property is only
* relevant for event based reloading.
* @param maxWaitForRestart Restart or Shutdown strategies are used, Spring Cloud
* Kubernetes waits a random amount of time before restarting. This is done in order to
* avoid having all instances of the same application restart at the same time. This
Expand All @@ -45,16 +45,18 @@
*/
@ConfigurationProperties(prefix = "spring.cloud.kubernetes.reload")
public record ConfigReloadProperties(boolean enabled, @DefaultValue("true") boolean monitoringConfigMaps,
boolean monitoringSecrets, @DefaultValue("REFRESH") ReloadStrategy strategy,
@DefaultValue Map<String, String> configMapsLabels,
boolean monitoringSecrets, @DefaultValue Map<String, String> secretsLabels,
@DefaultValue("REFRESH") ReloadStrategy strategy,
@DefaultValue("EVENT") ReloadDetectionMode mode, @DefaultValue("15000ms") Duration period,
@DefaultValue Set<String> namespaces, boolean enableReloadFiltering,
@DefaultValue Set<String> namespaces,
@DefaultValue("2s") Duration maxWaitForRestart) {

/**
* default instance.
*/
public static ConfigReloadProperties DEFAULT = new ConfigReloadProperties(false, true, false,
ReloadStrategy.REFRESH, ReloadDetectionMode.EVENT, Duration.ofMillis(15000), Set.of(), false,
public static final ConfigReloadProperties DEFAULT = new ConfigReloadProperties(false, true, Map.of(), false,
Map.of(), ReloadStrategy.REFRESH, ReloadDetectionMode.EVENT, Duration.ofMillis(15000), Set.of(),
Duration.ofSeconds(2));

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,43 @@
package org.springframework.cloud.kubernetes.commons.config;

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

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author wind57
*
* Tests binding, since we moved from a class to a record
*/
class ConfigReloadPropertiesTests {

@Test
void testDefaults() {
new ApplicationContextRunner().withUserConfiguration(Config.class).run(context -> {
ConfigReloadProperties properties = context.getBean(ConfigReloadProperties.class);
Assertions.assertThat(properties).isNotNull();
Assertions.assertThat(properties.enabled()).isFalse();
Assertions.assertThat(properties.monitoringConfigMaps()).isTrue();
Assertions.assertThat(properties.monitoringSecrets()).isFalse();
Assertions.assertThat(ConfigReloadProperties.ReloadStrategy.REFRESH).isEqualTo(properties.strategy());
Assertions.assertThat(ConfigReloadProperties.ReloadDetectionMode.EVENT).isEqualTo(properties.mode());
Assertions.assertThat(Duration.ofMillis(15000)).isEqualTo(properties.period());
Assertions.assertThat(properties.namespaces().isEmpty()).isTrue();
Assertions.assertThat(Duration.ofSeconds(2)).isEqualTo(properties.maxWaitForRestart());
ConfigReloadProperties properties = null;
try {
properties = context.getBean(ConfigReloadProperties.class);
} catch(Exception e) {
e.printStackTrace();
context.getStartupFailure().printStackTrace();
}
assertThat(properties).isNotNull();
assertThat(properties.enabled()).isFalse();
assertThat(properties.monitoringConfigMaps()).isTrue();
assertThat(properties.configMapsLabels()).isEmpty();
assertThat(properties.monitoringSecrets()).isFalse();
assertThat(properties.secretsLabels()).isEmpty();
assertThat(ConfigReloadProperties.ReloadStrategy.REFRESH).isEqualTo(properties.strategy());
assertThat(ConfigReloadProperties.ReloadDetectionMode.EVENT).isEqualTo(properties.mode());
assertThat(Duration.ofMillis(15000)).isEqualTo(properties.period());
assertThat(properties.namespaces().isEmpty()).isTrue();
assertThat(Duration.ofSeconds(2)).isEqualTo(properties.maxWaitForRestart());
});
}

Expand All @@ -54,22 +62,26 @@ void testNonDefaults() {
new ApplicationContextRunner().withUserConfiguration(Config.class)
.withPropertyValues("spring.cloud.kubernetes.reload.enabled=true",
"spring.cloud.kubernetes.reload.monitoring-config-maps=false",
"spring.cloud.kubernetes.reload.config-maps-labels[aa]=bb",
"spring.cloud.kubernetes.reload.monitoring-secrets=true",
"spring.cloud.kubernetes.reload.secrets-labels[cc]=dd",
"spring.cloud.kubernetes.reload.strategy=SHUTDOWN", "spring.cloud.kubernetes.reload.mode=POLLING",
"spring.cloud.kubernetes.reload.period=1000ms", "spring.cloud.kubernetes.reload.namespaces[0]=a",
"spring.cloud.kubernetes.reload.namespaces[1]=b",
"spring.cloud.kubernetes.reload.max-wait-for-restart=5s")
.run(context -> {
ConfigReloadProperties properties = context.getBean(ConfigReloadProperties.class);
Assertions.assertThat(properties).isNotNull();
Assertions.assertThat(properties.enabled()).isTrue();
Assertions.assertThat(properties.monitoringConfigMaps()).isFalse();
Assertions.assertThat(properties.monitoringSecrets()).isTrue();
Assertions.assertThat(ConfigReloadProperties.ReloadStrategy.SHUTDOWN).isEqualTo(properties.strategy());
Assertions.assertThat(ConfigReloadProperties.ReloadDetectionMode.POLLING).isEqualTo(properties.mode());
Assertions.assertThat(Duration.ofMillis(1000)).isEqualTo(properties.period());
Assertions.assertThat(properties.namespaces()).containsExactlyInAnyOrder("a", "b");
Assertions.assertThat(Duration.ofSeconds(5)).isEqualTo(properties.maxWaitForRestart());
assertThat(properties).isNotNull();
assertThat(properties.enabled()).isTrue();
assertThat(properties.monitoringConfigMaps()).isFalse();
assertThat(properties.configMapsLabels()).containsExactlyInAnyOrderEntriesOf(Map.of("aa", "bb"));
assertThat(properties.monitoringSecrets()).isTrue();
assertThat(properties.secretsLabels()).containsExactlyInAnyOrderEntriesOf(Map.of("cc", "dd"));
assertThat(ConfigReloadProperties.ReloadStrategy.SHUTDOWN).isEqualTo(properties.strategy());
assertThat(ConfigReloadProperties.ReloadDetectionMode.POLLING).isEqualTo(properties.mode());
assertThat(Duration.ofMillis(1000)).isEqualTo(properties.period());
assertThat(properties.namespaces()).containsExactlyInAnyOrder("a", "b");
assertThat(Duration.ofSeconds(5)).isEqualTo(properties.maxWaitForRestart());
});
}

Expand Down
Loading