From 19ee913af401ca02e27f09faeb9ffcb097e009ad Mon Sep 17 00:00:00 2001 From: erabii Date: Fri, 8 Nov 2024 21:21:30 +0200 Subject: [PATCH] Refactor reload (2) (#1790) * dirty * return early * add return --- .../config/reload/ConfigReloadUtil.java | 26 ++++++++++++++++--- .../PollingConfigMapChangeDetector.java | 23 +++------------- .../reload/PollingSecretsChangeDetector.java | 23 +++------------- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java index c0be3d8caf..4279694418 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java @@ -44,13 +44,31 @@ private ConfigReloadUtil() { private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(ConfigReloadUtil.class)); - public static boolean reload(String target, String eventSourceType, PropertySourceLocator locator, + /** + * used for the event based reloading. + */ + public static boolean reload(String target, String sourceAsString, PropertySourceLocator locator, ConfigurableEnvironment environment, Class existingSourcesType) { - LOG.debug(() -> "onEvent " + target + ": " + eventSourceType); + LOG.debug(() -> "onEvent " + target + ": " + sourceAsString); + + return reload(locator, environment, existingSourcesType); + } + + /** + * used for the poll based reloading. + */ + public static boolean reload(PropertySourceLocator locator, ConfigurableEnvironment environment, + Class existingSourcesType) { - List sourceFromK8s = locateMapPropertySources(locator, environment); List existingSources = findPropertySources(existingSourcesType, environment); + if (existingSources.isEmpty()) { + LOG.debug(() -> "no existingSources found, reload will not happen"); + return false; + } + + List sourceFromK8s = locateMapPropertySources(locator, environment); + boolean changed = changed(sourceFromK8s, existingSources); if (changed) { LOG.info("Detected change in config maps/secrets"); @@ -67,7 +85,9 @@ public static boolean reload(String target, String eventSourceType, PropertySour * @param property source type * @param sourceClass class for which property sources will be found * @return finds all registered property sources of the given type + * @deprecated this method will not be public in the next major release. */ + @Deprecated(forRemoval = false) public static > List findPropertySources(Class sourceClass, ConfigurableEnvironment environment) { List managedSources = new ArrayList<>(); diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingConfigMapChangeDetector.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingConfigMapChangeDetector.java index bcd568b178..e26aa85673 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingConfigMapChangeDetector.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingConfigMapChangeDetector.java @@ -17,7 +17,6 @@ package org.springframework.cloud.kubernetes.commons.config.reload; import java.time.Duration; -import java.util.List; import jakarta.annotation.PostConstruct; import org.apache.commons.logging.Log; @@ -29,10 +28,6 @@ import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.support.PeriodicTrigger; -import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.changed; -import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.findPropertySources; -import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.locateMapPropertySources; - /** * A change detector that periodically retrieves configmaps and fire a reload when * something changes. @@ -75,23 +70,13 @@ private void init() { } private void executeCycle() { - - boolean changedConfigMap = false; if (monitorConfigMaps) { - log.debug("Polling for changes in config maps"); - List currentConfigMapSources = findPropertySources(propertySourceClass, - environment); - - if (!currentConfigMapSources.isEmpty()) { - changedConfigMap = changed(locateMapPropertySources(this.propertySourceLocator, this.environment), - currentConfigMapSources); + boolean changedConfigMap = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass); + if (changedConfigMap) { + log.info("Detected change in config maps"); + reloadProperties(); } } - - if (changedConfigMap) { - log.info("Detected change in config maps"); - reloadProperties(); - } } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingSecretsChangeDetector.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingSecretsChangeDetector.java index b70975a5c7..da0422bb68 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingSecretsChangeDetector.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/reload/PollingSecretsChangeDetector.java @@ -17,7 +17,6 @@ package org.springframework.cloud.kubernetes.commons.config.reload; import java.time.Duration; -import java.util.List; import jakarta.annotation.PostConstruct; import org.apache.commons.logging.Log; @@ -29,10 +28,6 @@ import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.support.PeriodicTrigger; -import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.changed; -import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.findPropertySources; -import static org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadUtil.locateMapPropertySources; - /** * A change detector that periodically retrieves secrets and fires a reload when something * changes. @@ -75,23 +70,13 @@ private void init() { } private void executeCycle() { - - boolean changedSecrets = false; if (monitorSecrets) { - log.debug("Polling for changes in secrets"); - List currentSecretSources = locateMapPropertySources(this.propertySourceLocator, - this.environment); - if (!currentSecretSources.isEmpty()) { - List propertySources = findPropertySources(propertySourceClass, - environment); - changedSecrets = changed(currentSecretSources, propertySources); + boolean changedSecrets = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass); + if (changedSecrets) { + log.info("Detected change in secrets"); + reloadProperties(); } } - - if (changedSecrets) { - log.info("Detected change in secrets"); - reloadProperties(); - } } }