Refactor reload (2) (#1790)

* dirty

* return early

* add return
This commit is contained in:
erabii
2024-11-08 21:21:30 +02:00
committed by GitHub
parent aad8cd096f
commit 19ee913af4
3 changed files with 31 additions and 41 deletions

View File

@@ -44,12 +44,30 @@ public final class 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<? extends MapPropertySource> 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<? extends MapPropertySource> existingSourcesType) {
List<? extends MapPropertySource> existingSources = findPropertySources(existingSourcesType, environment);
if (existingSources.isEmpty()) {
LOG.debug(() -> "no existingSources found, reload will not happen");
return false;
}
List<? extends MapPropertySource> sourceFromK8s = locateMapPropertySources(locator, environment);
List<? extends MapPropertySource> existingSources = findPropertySources(existingSourcesType, environment);
boolean changed = changed(sourceFromK8s, existingSources);
if (changed) {
@@ -67,7 +85,9 @@ public final class ConfigReloadUtil {
* @param <S> 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 <S extends PropertySource<?>> List<S> findPropertySources(Class<S> sourceClass,
ConfigurableEnvironment environment) {
List<S> managedSources = new ArrayList<>();

View File

@@ -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.core.env.MapPropertySource;
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 @@ public class PollingConfigMapChangeDetector extends ConfigurationChangeDetector
}
private void executeCycle() {
boolean changedConfigMap = false;
if (monitorConfigMaps) {
log.debug("Polling for changes in config maps");
List<? extends MapPropertySource> 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();
}
}
}

View File

@@ -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.core.env.MapPropertySource;
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 @@ public class PollingSecretsChangeDetector extends ConfigurationChangeDetector {
}
private void executeCycle() {
boolean changedSecrets = false;
if (monitorSecrets) {
log.debug("Polling for changes in secrets");
List<MapPropertySource> currentSecretSources = locateMapPropertySources(this.propertySourceLocator,
this.environment);
if (!currentSecretSources.isEmpty()) {
List<? extends MapPropertySource> 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();
}
}
}