Skip to content

Commit 19ee913

Browse files
authored
Refactor reload (2) (#1790)
* dirty * return early * add return
1 parent aad8cd0 commit 19ee913

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,31 @@ private ConfigReloadUtil() {
4444

4545
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(ConfigReloadUtil.class));
4646

47-
public static boolean reload(String target, String eventSourceType, PropertySourceLocator locator,
47+
/**
48+
* used for the event based reloading.
49+
*/
50+
public static boolean reload(String target, String sourceAsString, PropertySourceLocator locator,
4851
ConfigurableEnvironment environment, Class<? extends MapPropertySource> existingSourcesType) {
49-
LOG.debug(() -> "onEvent " + target + ": " + eventSourceType);
52+
LOG.debug(() -> "onEvent " + target + ": " + sourceAsString);
53+
54+
return reload(locator, environment, existingSourcesType);
55+
}
56+
57+
/**
58+
* used for the poll based reloading.
59+
*/
60+
public static boolean reload(PropertySourceLocator locator, ConfigurableEnvironment environment,
61+
Class<? extends MapPropertySource> existingSourcesType) {
5062

51-
List<? extends MapPropertySource> sourceFromK8s = locateMapPropertySources(locator, environment);
5263
List<? extends MapPropertySource> existingSources = findPropertySources(existingSourcesType, environment);
5364

65+
if (existingSources.isEmpty()) {
66+
LOG.debug(() -> "no existingSources found, reload will not happen");
67+
return false;
68+
}
69+
70+
List<? extends MapPropertySource> sourceFromK8s = locateMapPropertySources(locator, environment);
71+
5472
boolean changed = changed(sourceFromK8s, existingSources);
5573
if (changed) {
5674
LOG.info("Detected change in config maps/secrets");
@@ -67,7 +85,9 @@ public static boolean reload(String target, String eventSourceType, PropertySour
6785
* @param <S> property source type
6886
* @param sourceClass class for which property sources will be found
6987
* @return finds all registered property sources of the given type
88+
* @deprecated this method will not be public in the next major release.
7089
*/
90+
@Deprecated(forRemoval = false)
7191
public static <S extends PropertySource<?>> List<S> findPropertySources(Class<S> sourceClass,
7292
ConfigurableEnvironment environment) {
7393
List<S> managedSources = new ArrayList<>();

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingConfigMapChangeDetector.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.kubernetes.commons.config.reload;
1818

1919
import java.time.Duration;
20-
import java.util.List;
2120

2221
import jakarta.annotation.PostConstruct;
2322
import org.apache.commons.logging.Log;
@@ -29,10 +28,6 @@
2928
import org.springframework.scheduling.TaskScheduler;
3029
import org.springframework.scheduling.support.PeriodicTrigger;
3130

32-
import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.changed;
33-
import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.findPropertySources;
34-
import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.locateMapPropertySources;
35-
3631
/**
3732
* A change detector that periodically retrieves configmaps and fire a reload when
3833
* something changes.
@@ -75,23 +70,13 @@ private void init() {
7570
}
7671

7772
private void executeCycle() {
78-
79-
boolean changedConfigMap = false;
8073
if (monitorConfigMaps) {
81-
log.debug("Polling for changes in config maps");
82-
List<? extends MapPropertySource> currentConfigMapSources = findPropertySources(propertySourceClass,
83-
environment);
84-
85-
if (!currentConfigMapSources.isEmpty()) {
86-
changedConfigMap = changed(locateMapPropertySources(this.propertySourceLocator, this.environment),
87-
currentConfigMapSources);
74+
boolean changedConfigMap = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass);
75+
if (changedConfigMap) {
76+
log.info("Detected change in config maps");
77+
reloadProperties();
8878
}
8979
}
90-
91-
if (changedConfigMap) {
92-
log.info("Detected change in config maps");
93-
reloadProperties();
94-
}
9580
}
9681

9782
}

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingSecretsChangeDetector.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.cloud.kubernetes.commons.config.reload;
1818

1919
import java.time.Duration;
20-
import java.util.List;
2120

2221
import jakarta.annotation.PostConstruct;
2322
import org.apache.commons.logging.Log;
@@ -29,10 +28,6 @@
2928
import org.springframework.scheduling.TaskScheduler;
3029
import org.springframework.scheduling.support.PeriodicTrigger;
3130

32-
import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.changed;
33-
import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.findPropertySources;
34-
import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.locateMapPropertySources;
35-
3631
/**
3732
* A change detector that periodically retrieves secrets and fires a reload when something
3833
* changes.
@@ -75,23 +70,13 @@ private void init() {
7570
}
7671

7772
private void executeCycle() {
78-
79-
boolean changedSecrets = false;
8073
if (monitorSecrets) {
81-
log.debug("Polling for changes in secrets");
82-
List<MapPropertySource> currentSecretSources = locateMapPropertySources(this.propertySourceLocator,
83-
this.environment);
84-
if (!currentSecretSources.isEmpty()) {
85-
List<? extends MapPropertySource> propertySources = findPropertySources(propertySourceClass,
86-
environment);
87-
changedSecrets = changed(currentSecretSources, propertySources);
74+
boolean changedSecrets = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass);
75+
if (changedSecrets) {
76+
log.info("Detected change in secrets");
77+
reloadProperties();
8878
}
8979
}
90-
91-
if (changedSecrets) {
92-
log.info("Detected change in secrets");
93-
reloadProperties();
94-
}
9580
}
9681

9782
}

0 commit comments

Comments
 (0)