Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload functionality clean-up part 4 #1036

Merged
merged 36 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
bf7b971
first
wind57 Jun 11, 2022
cf4fca4
more changes
wind57 Jun 12, 2022
0ace7cd
fix test
wind57 Jun 12, 2022
923895f
fix test
wind57 Jun 12, 2022
6379ed9
make both final
wind57 Jun 12, 2022
ad85d11
first
wind57 Jun 12, 2022
c99f760
more clean-up
wind57 Jun 12, 2022
36e1bae
some simplifications
wind57 Jun 12, 2022
e3725f5
same logging
wind57 Jun 12, 2022
9face5a
Merge branch 'reload-conditionals-nitpick' into reload-conditionals-n…
wind57 Jun 12, 2022
1eefcb9
more clean-up, still I am in the blur a little
wind57 Jun 14, 2022
f60830a
clean-up
wind57 Jun 14, 2022
9f953ab
clean-up part 4
wind57 Jun 15, 2022
d148d2f
configmap polling test added
wind57 Jun 15, 2022
2a5da4c
informers + tests
wind57 Jun 16, 2022
940efcc
k8s native changes + tests
wind57 Jun 16, 2022
eba2c9f
fix tests
wind57 Jun 16, 2022
af8dc00
fix tests take 2
wind57 Jun 16, 2022
d7164dc
fix tests take 3
wind57 Jun 16, 2022
66b1e6a
fix tests take 4
wind57 Jun 16, 2022
4f08bed
merge previous
wind57 Jun 16, 2022
db6043c
fix test
wind57 Jun 16, 2022
623bac6
merge main
wind57 Jun 17, 2022
587a2cc
Merge branch 'reload-conditionals-nitpick-2' into reload-simplify-con…
wind57 Jun 17, 2022
f5487ae
merge previous
wind57 Jun 17, 2022
c648adf
dirty
wind57 Jul 4, 2022
63d34d9
started merging
wind57 Jul 4, 2022
69af4b1
fabric8 polish
wind57 Jul 4, 2022
83a60be
license
wind57 Jul 4, 2022
8410306
log added
wind57 Jul 4, 2022
8f919c9
inform
wind57 Jul 4, 2022
61507aa
informers commented
wind57 Jul 4, 2022
4a47895
k8s native
wind57 Jul 4, 2022
7886338
more
wind57 Jul 4, 2022
eaf5938
2 minutes
wind57 Jul 4, 2022
0ffbb3d
give more time
wind57 Jul 6, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration;
Expand All @@ -42,113 +41,106 @@
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy;
import org.springframework.cloud.kubernetes.commons.config.reload.PollingConfigMapChangeDetector;
import org.springframework.cloud.kubernetes.commons.config.reload.PollingSecretsChangeDetector;
import org.springframework.cloud.kubernetes.commons.config.reload.condition.ConditionalOnKubernetesReloadEnabled;
import org.springframework.cloud.kubernetes.commons.config.reload.condition.EventReloadDetectionMode;
import org.springframework.cloud.kubernetes.commons.config.reload.condition.PollingReloadDetectionMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.scheduling.TaskScheduler;

/**
* @author Ryan Baxter
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnKubernetesAndConfigEnabled
@ConditionalOnClass(EndpointAutoConfiguration.class)
@ConditionalOnKubernetesReloadEnabled
@ConditionalOnClass({ EndpointAutoConfiguration.class, RestartEndpoint.class, ContextRefresher.class })
@AutoConfigureAfter({ InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class,
RefreshAutoConfiguration.class })
@Import(ConfigReloadAutoConfiguration.class)
@EnableConfigurationProperties(ConfigReloadProperties.class)
@Import(ConfigReloadAutoConfiguration.class)
public class KubernetesClientConfigReloadAutoConfiguration {

/**
* Configuration reload must be enabled explicitly.
* Polling configMap ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param configMapPropertySourceLocator configMap property source locator
* @return a bean that listen to configuration changes and fire a reload.
*/
@ConditionalOnProperty("spring.cloud.kubernetes.reload.enabled")
@ConditionalOnClass({ RestartEndpoint.class, ContextRefresher.class })
protected static class ConfigReloadAutoConfigurationBeans {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same simplification has been done in fabric8 - drop the inner class, move annotations


/**
* Polling configMap ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param configMapPropertySourceLocator configMap property source locator
* @return a bean that listen to configuration changes and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientConfigMapPropertySourceLocator.class)
@Conditional(PollingReloadDetectionMode.class)
public ConfigurationChangeDetector configMapPropertyChangePollingWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientConfigMapPropertySourceLocator configMapPropertySourceLocator,
AbstractEnvironment environment, TaskSchedulerWrapper taskScheduler) {

return new PollingConfigMapChangeDetector(environment, properties, strategy,
KubernetesClientConfigMapPropertySource.class, configMapPropertySourceLocator,
taskScheduler.getTaskScheduler());
}
@Bean
@ConditionalOnBean(KubernetesClientConfigMapPropertySourceLocator.class)
@Conditional(PollingReloadDetectionMode.class)
public ConfigurationChangeDetector configMapPropertyChangePollingWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientConfigMapPropertySourceLocator configMapPropertySourceLocator,
AbstractEnvironment environment, TaskSchedulerWrapper<TaskScheduler> taskScheduler) {

/**
* Polling secrets ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param secretsPropertySourceLocator secrets property source locator
* @return a bean that listen to configuration changes and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientSecretsPropertySourceLocator.class)
@Conditional(PollingReloadDetectionMode.class)
public ConfigurationChangeDetector secretsPropertyChangePollingWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientSecretsPropertySourceLocator secretsPropertySourceLocator,
AbstractEnvironment environment, TaskSchedulerWrapper taskScheduler) {
return new PollingConfigMapChangeDetector(environment, properties, strategy,
KubernetesClientConfigMapPropertySource.class, configMapPropertySourceLocator,
taskScheduler.getTaskScheduler());
}

return new PollingSecretsChangeDetector(environment, properties, strategy,
KubernetesClientSecretsPropertySource.class, secretsPropertySourceLocator,
taskScheduler.getTaskScheduler());
}
/**
* Polling secrets ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param secretsPropertySourceLocator secrets property source locator
* @return a bean that listen to configuration changes and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientSecretsPropertySourceLocator.class)
@Conditional(PollingReloadDetectionMode.class)
public ConfigurationChangeDetector secretsPropertyChangePollingWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientSecretsPropertySourceLocator secretsPropertySourceLocator, AbstractEnvironment environment,
TaskSchedulerWrapper<TaskScheduler> taskScheduler) {

/**
* Event Based configMap ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param configMapPropertySourceLocator configMap property source locator
* @return a bean that listen to configMap change events and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientConfigMapPropertySourceLocator.class)
@Conditional(EventReloadDetectionMode.class)
public ConfigurationChangeDetector configMapPropertyChangeEventWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientConfigMapPropertySourceLocator configMapPropertySourceLocator,
AbstractEnvironment environment, CoreV1Api coreV1Api,
KubernetesNamespaceProvider kubernetesNamespaceProvider) {
return new PollingSecretsChangeDetector(environment, properties, strategy,
KubernetesClientSecretsPropertySource.class, secretsPropertySourceLocator,
taskScheduler.getTaskScheduler());
}

return new KubernetesClientEventBasedConfigMapChangeDetector(coreV1Api, environment, properties, strategy,
configMapPropertySourceLocator, kubernetesNamespaceProvider);
}
/**
* Event Based configMap ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param configMapPropertySourceLocator configMap property source locator
* @return a bean that listen to configMap change events and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientConfigMapPropertySourceLocator.class)
@Conditional(EventReloadDetectionMode.class)
public ConfigurationChangeDetector configMapPropertyChangeEventWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientConfigMapPropertySourceLocator configMapPropertySourceLocator,
AbstractEnvironment environment, CoreV1Api coreV1Api,
KubernetesNamespaceProvider kubernetesNamespaceProvider) {

/**
* Event Based secrets ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param secretsPropertySourceLocator secrets property source locator
* @return a bean that listen to secrets change events and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientSecretsPropertySourceLocator.class)
@Conditional(EventReloadDetectionMode.class)
public ConfigurationChangeDetector secretsPropertyChangeEventWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientSecretsPropertySourceLocator secretsPropertySourceLocator,
AbstractEnvironment environment, CoreV1Api coreV1Api,
KubernetesNamespaceProvider kubernetesNamespaceProvider) {
return new KubernetesClientEventBasedConfigMapChangeDetector(coreV1Api, environment, properties, strategy,
configMapPropertySourceLocator, kubernetesNamespaceProvider);
}

return new KubernetesClientEventBasedSecretsChangeDetector(coreV1Api, environment, properties, strategy,
secretsPropertySourceLocator, kubernetesNamespaceProvider);
}
/**
* Event Based secrets ConfigurationChangeDetector.
* @param properties config reload properties
* @param strategy configuration update strategy
* @param secretsPropertySourceLocator secrets property source locator
* @return a bean that listen to secrets change events and fire a reload.
*/
@Bean
@ConditionalOnBean(KubernetesClientSecretsPropertySourceLocator.class)
@Conditional(EventReloadDetectionMode.class)
public ConfigurationChangeDetector secretsPropertyChangeEventWatcher(ConfigReloadProperties properties,
ConfigurationUpdateStrategy strategy,
KubernetesClientSecretsPropertySourceLocator secretsPropertySourceLocator, AbstractEnvironment environment,
CoreV1Api coreV1Api, KubernetesNamespaceProvider kubernetesNamespaceProvider) {

return new KubernetesClientEventBasedSecretsChangeDetector(coreV1Api, environment, properties, strategy,
secretsPropertySourceLocator, kubernetesNamespaceProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@
import io.kubernetes.client.util.CallGeneratorParams;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySource;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySourceLocator;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationChangeDetector;
Expand All @@ -44,17 +41,15 @@
*/
public class KubernetesClientEventBasedConfigMapChangeDetector extends ConfigurationChangeDetector {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some minor cleanups in this class


private static final Log LOG = LogFactory.getLog(KubernetesClientEventBasedConfigMapChangeDetector.class);

private CoreV1Api coreV1Api = null;
private final CoreV1Api coreV1Api;

private final KubernetesClientConfigMapPropertySourceLocator propertySourceLocator;

private final SharedInformerFactory factory;

private KubernetesClientProperties kubernetesClientProperties;
private final String namespace;

private KubernetesNamespaceProvider kubernetesNamespaceProvider;
private final boolean monitorConfigMaps;

public KubernetesClientEventBasedConfigMapChangeDetector(CoreV1Api coreV1Api, ConfigurableEnvironment environment,
ConfigReloadProperties properties, ConfigurationUpdateStrategy strategy,
Expand All @@ -71,38 +66,33 @@ public KubernetesClientEventBasedConfigMapChangeDetector(CoreV1Api coreV1Api, Co
// certificate authorities for the cluster. This results in SSL errors.
// See https://github.com/spring-cloud/spring-cloud-kubernetes/issues/885
this.factory = new SharedInformerFactory(createApiClientForInformerClient());
this.kubernetesNamespaceProvider = kubernetesNamespaceProvider;
}

private String getNamespace() {
return kubernetesNamespaceProvider != null ? kubernetesNamespaceProvider.getNamespace()
: kubernetesClientProperties.getNamespace();
this.namespace = kubernetesNamespaceProvider.getNamespace();
this.monitorConfigMaps = properties.isMonitoringConfigMaps();
}

@PostConstruct
public void watch() {
if (coreV1Api != null && this.properties.isMonitoringConfigMaps()) {
if (coreV1Api != null && monitorConfigMaps) {
SharedIndexInformer<V1ConfigMap> configMapInformer = factory.sharedIndexInformerFor(
(CallGeneratorParams params) -> coreV1Api.listNamespacedConfigMapCall(getNamespace(), null, null,
null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch,
null),
(CallGeneratorParams params) -> coreV1Api.listNamespacedConfigMapCall(namespace, null, null, null,
null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null),
V1ConfigMap.class, V1ConfigMapList.class);
configMapInformer.addEventHandler(new ResourceEventHandler<V1ConfigMap>() {
configMapInformer.addEventHandler(new ResourceEventHandler<>() {
@Override
public void onAdd(V1ConfigMap obj) {
LOG.info("CongifMap " + obj.getMetadata().getName() + " was added.");
log.info("ConfigMap " + obj.getMetadata().getName() + " was added.");
onEvent(obj);
}

@Override
public void onUpdate(V1ConfigMap oldObj, V1ConfigMap newObj) {
LOG.info("ConfigMap " + newObj.getMetadata().getName() + " was added.");
log.info("ConfigMap " + newObj.getMetadata().getName() + " was added.");
onEvent(newObj);
}

@Override
public void onDelete(V1ConfigMap obj, boolean deletedFinalStateUnknown) {
LOG.info("ConfigMap " + obj.getMetadata() + " was deleted.");
log.info("ConfigMap " + obj.getMetadata() + " was deleted.");
onEvent(obj);
}
});
Expand All @@ -116,15 +106,15 @@ public void unwatch() {
}

private void onEvent(V1ConfigMap configMap) {
this.log.debug(String.format("onEvent configMap: %s", configMap.toString()));
log.debug("onEvent configMap: " + configMap.toString());
boolean changed = changed(locateMapPropertySources(this.propertySourceLocator, this.environment),
findPropertySources(KubernetesClientConfigMapPropertySource.class));
if (changed) {
LOG.info("Configuration change detected, reloading properties.");
log.info("Configuration change detected, reloading properties.");
reloadProperties();
}
else {
LOG.warn("Configuration change was not detected.");
log.warn("Configuration change was not detected.");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
import io.kubernetes.client.openapi.models.V1SecretList;
import io.kubernetes.client.util.CallGeneratorParams;
import jakarta.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySource;
import org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySourceLocator;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationChangeDetector;
Expand All @@ -43,17 +40,15 @@
*/
public class KubernetesClientEventBasedSecretsChangeDetector extends ConfigurationChangeDetector {

private static final Log LOG = LogFactory.getLog(KubernetesClientEventBasedSecretsChangeDetector.class);

private CoreV1Api coreV1Api;
private final CoreV1Api coreV1Api;

private final KubernetesClientSecretsPropertySourceLocator propertySourceLocator;

private final SharedInformerFactory factory;

private KubernetesClientProperties kubernetesClientProperties;
private final String namespace;

private KubernetesNamespaceProvider kubernetesNamespaceProvider;
private final boolean monitorSecrets;

public KubernetesClientEventBasedSecretsChangeDetector(CoreV1Api coreV1Api, ConfigurableEnvironment environment,
ConfigReloadProperties properties, ConfigurationUpdateStrategy strategy,
Expand All @@ -70,37 +65,33 @@ public KubernetesClientEventBasedSecretsChangeDetector(CoreV1Api coreV1Api, Conf
// See https://github.com/spring-cloud/spring-cloud-kubernetes/issues/885
this.factory = new SharedInformerFactory(createApiClientForInformerClient());
this.coreV1Api = coreV1Api;
this.kubernetesNamespaceProvider = kubernetesNamespaceProvider;
}

private String getNamespace() {
return kubernetesNamespaceProvider != null ? kubernetesNamespaceProvider.getNamespace()
: kubernetesClientProperties.getNamespace();
this.namespace = kubernetesNamespaceProvider.getNamespace();
this.monitorSecrets = properties.isMonitoringSecrets();
}

@PostConstruct
public void watch() {
if (coreV1Api != null && this.properties.isMonitoringSecrets()) {
if (coreV1Api != null && monitorSecrets) {
SharedIndexInformer<V1Secret> configMapInformer = factory.sharedIndexInformerFor(
(CallGeneratorParams params) -> coreV1Api.listNamespacedSecretCall(getNamespace(), null, null, null,
(CallGeneratorParams params) -> coreV1Api.listNamespacedSecretCall(namespace, null, null, null,
null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null),
V1Secret.class, V1SecretList.class);
configMapInformer.addEventHandler(new ResourceEventHandler<V1Secret>() {
configMapInformer.addEventHandler(new ResourceEventHandler<>() {
@Override
public void onAdd(V1Secret obj) {
LOG.info("Secret " + obj.getMetadata().getName() + " was added.");
log.info("Secret " + obj.getMetadata().getName() + " was added.");
onEvent(obj);
}

@Override
public void onUpdate(V1Secret oldObj, V1Secret newObj) {
LOG.info("Secret " + newObj.getMetadata().getName() + " was added.");
log.info("Secret " + newObj.getMetadata().getName() + " was added.");
onEvent(newObj);
}

@Override
public void onDelete(V1Secret obj, boolean deletedFinalStateUnknown) {
LOG.info("Secret " + obj.getMetadata() + " was deleted.");
log.info("Secret " + obj.getMetadata() + " was deleted.");
onEvent(obj);
}
});
Expand All @@ -109,11 +100,11 @@ public void onDelete(V1Secret obj, boolean deletedFinalStateUnknown) {
}

private void onEvent(V1Secret secret) {
this.log.debug(String.format("onEvent configMap: %s", secret.toString()));
log.debug("onEvent configMap: " + secret.toString());
boolean changed = changed(locateMapPropertySources(this.propertySourceLocator, this.environment),
findPropertySources(KubernetesClientSecretsPropertySource.class));
if (changed) {
this.log.info("Detected change in secrets");
log.info("Detected change in secrets");
reloadProperties();
}
}
Expand Down
Loading