diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java index b261ea28d2..4b5065b58e 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java @@ -87,6 +87,8 @@ public class ConfigurationWatcherConfigurationProperties { private Integer actuatorPort = -1; + private ConfigurationWatcherHaProperties ha = new ConfigurationWatcherHaProperties(); + public String getActuatorPath() { return actuatorPath; } @@ -134,6 +136,14 @@ public void setRefreshStrategy(RefreshStrategy refreshStrategy) { this.refreshStrategy = refreshStrategy; } + public ConfigurationWatcherHaProperties getHa() { + return ha; + } + + public void setHa(ConfigurationWatcherHaProperties ha) { + this.ha = ha; + } + public enum RefreshStrategy { /** diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherHaProperties.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherHaProperties.java new file mode 100644 index 0000000000..bcfc9f8d74 --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherHaProperties.java @@ -0,0 +1,56 @@ +/* + * Copyright 2013-present 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.configuration.watcher; + +/** + * Properties for watcher HA state persisted in a Lease. + * + * @author wind57 + */ +public class ConfigurationWatcherHaProperties { + + private boolean enabled; + + private String leaseName = "configuration-watcher-ha"; + + private String leaseNamespace; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getLeaseName() { + return leaseName; + } + + public void setLeaseName(String leaseName) { + this.leaseName = leaseName; + } + + public String getLeaseNamespace() { + return leaseNamespace; + } + + public void setLeaseNamespace(String leaseNamespace) { + this.leaseNamespace = leaseNamespace; + } + +} diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationPropertiesTests.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationPropertiesTests.java index c15e8c5e01..3d98146143 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationPropertiesTests.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationPropertiesTests.java @@ -18,6 +18,10 @@ import org.junit.jupiter.api.Test; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Configuration; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -34,4 +38,38 @@ void setActuatorPath() { assertThat(properties.getActuatorPath()).isEqualTo("/foo/bar"); } + @Test + void testWithDefaults() { + new ApplicationContextRunner().withUserConfiguration(Config.class).run(context -> { + ConfigurationWatcherConfigurationProperties props = context + .getBean(ConfigurationWatcherConfigurationProperties.class); + assertThat(props).isNotNull(); + assertThat(props.getHa().isEnabled()).isFalse(); + assertThat(props.getHa().getLeaseName()).isEqualTo("configuration-watcher-ha"); + assertThat(props.getHa().getLeaseNamespace()).isNull(); + }); + } + + @Test + void testWithNonDefaults() { + new ApplicationContextRunner().withUserConfiguration(Config.class) + .withPropertyValues("spring.cloud.kubernetes.configuration.watcher.ha.enabled=true", + "spring.cloud.kubernetes.configuration.watcher.ha.lease-name=custom-lease", + "spring.cloud.kubernetes.configuration.watcher.ha.lease-namespace=watcher-namespace") + .run(context -> { + ConfigurationWatcherConfigurationProperties props = context + .getBean(ConfigurationWatcherConfigurationProperties.class); + assertThat(props).isNotNull(); + assertThat(props.getHa().isEnabled()).isTrue(); + assertThat(props.getHa().getLeaseName()).isEqualTo("custom-lease"); + assertThat(props.getHa().getLeaseNamespace()).isEqualTo("watcher-namespace"); + }); + } + + @Configuration + @EnableConfigurationProperties(ConfigurationWatcherConfigurationProperties.class) + static class Config { + + } + }