From f56026fe5ef0ab6f7bb45e8d978156c3495599ae Mon Sep 17 00:00:00 2001 From: wind57 Date: Fri, 8 Nov 2024 19:01:21 +0200 Subject: [PATCH 1/3] dirty --- .../config/reload/ConfigReloadUtil.java | 18 +++++++++++++-- .../PollingConfigMapChangeDetector.java | 23 ++++--------------- .../reload/PollingSecretsChangeDetector.java | 23 ++++--------------- 3 files changed, 24 insertions(+), 40 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..78b8a941c0 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,9 +44,21 @@ 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); @@ -67,7 +79,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(); - } } } From 36aeb0207663a13a2f8d789e84e0235b94e671db Mon Sep 17 00:00:00 2001 From: wind57 Date: Fri, 8 Nov 2024 19:19:19 +0200 Subject: [PATCH 2/3] return early --- .../kubernetes/commons/config/reload/ConfigReloadUtil.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 78b8a941c0..d57b5feb17 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 @@ -60,9 +60,14 @@ public static boolean reload(String target, String sourceAsString, PropertySourc 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"); + } + + List sourceFromK8s = locateMapPropertySources(locator, environment); + boolean changed = changed(sourceFromK8s, existingSources); if (changed) { LOG.info("Detected change in config maps/secrets"); From 8a48b7ea6885f5306769e0ddfa659e122c5cab77 Mon Sep 17 00:00:00 2001 From: wind57 Date: Fri, 8 Nov 2024 19:22:18 +0200 Subject: [PATCH 3/3] add return --- .../cloud/kubernetes/commons/config/reload/ConfigReloadUtil.java | 1 + 1 file changed, 1 insertion(+) 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 d57b5feb17..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 @@ -64,6 +64,7 @@ public static boolean reload(PropertySourceLocator locator, ConfigurableEnvironm if (existingSources.isEmpty()) { LOG.debug(() -> "no existingSources found, reload will not happen"); + return false; } List sourceFromK8s = locateMapPropertySources(locator, environment);