Skip to content

Commit 445c8d7

Browse files
committed
Merge branch '3.3.x' into fix-2148
2 parents c8cb5b8 + 36e204f commit 445c8d7

File tree

7 files changed

+152
-114
lines changed

7 files changed

+152
-114
lines changed

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetector.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class KubernetesClientEventBasedConfigMapChangeDetector extends Configura
6969

7070
private final boolean enableReloadFiltering;
7171

72+
private final boolean monitoringConfigMaps;
73+
7274
private final ResourceEventHandler<V1ConfigMap> handler = new ResourceEventHandler<>() {
7375

7476
@Override
@@ -107,33 +109,40 @@ public KubernetesClientEventBasedConfigMapChangeDetector(CoreV1Api coreV1Api, Co
107109
this.coreV1Api = coreV1Api;
108110
this.apiClient = createApiClientForInformerClient();
109111
this.enableReloadFiltering = properties.enableReloadFiltering();
112+
this.monitoringConfigMaps = properties.monitoringConfigMaps();
110113
namespaces = namespaces(kubernetesNamespaceProvider, properties, "configmap");
111114
}
112115

113116
@PostConstruct
114117
void inform() {
115-
LOG.info(() -> "Kubernetes event-based configMap change detector activated");
116-
117-
namespaces.forEach(namespace -> {
118-
SharedIndexInformer<V1ConfigMap> informer;
119-
String[] filter = new String[1];
120118

121-
if (enableReloadFiltering) {
122-
filter[0] = ConfigReloadProperties.RELOAD_LABEL_FILTER + "=true";
123-
}
124-
SharedInformerFactory factory = new SharedInformerFactory(apiClient);
125-
factories.add(factory);
126-
informer = factory
127-
.sharedIndexInformerFor((CallGeneratorParams params) -> coreV1Api.listNamespacedConfigMapCall(namespace,
128-
null, null, null, null, filter[0], null, params.resourceVersion, null, null,
129-
params.timeoutSeconds, params.watch, null), V1ConfigMap.class, V1ConfigMapList.class);
130-
131-
LOG.debug(() -> "added configmap informer for namespace : " + namespace + " with filter : " + filter[0]);
132-
133-
informer.addEventHandler(handler);
134-
informers.add(informer);
135-
factory.startAllRegisteredInformers();
136-
});
119+
if (monitoringConfigMaps) {
120+
LOG.info(() -> "Kubernetes event-based configMap change detector activated");
121+
122+
namespaces.forEach(namespace -> {
123+
SharedIndexInformer<V1ConfigMap> informer;
124+
String[] filter = new String[1];
125+
126+
if (enableReloadFiltering) {
127+
filter[0] = ConfigReloadProperties.RELOAD_LABEL_FILTER + "=true";
128+
}
129+
SharedInformerFactory factory = new SharedInformerFactory(apiClient);
130+
factories.add(factory);
131+
informer = factory
132+
.sharedIndexInformerFor(
133+
(CallGeneratorParams params) -> coreV1Api.listNamespacedConfigMapCall(namespace, null, null,
134+
null, null, filter[0], null, params.resourceVersion, null, null,
135+
params.timeoutSeconds, params.watch, null),
136+
V1ConfigMap.class, V1ConfigMapList.class);
137+
138+
LOG.debug(
139+
() -> "added configmap informer for namespace : " + namespace + " with filter : " + filter[0]);
140+
141+
informer.addEventHandler(handler);
142+
informers.add(informer);
143+
factory.startAllRegisteredInformers();
144+
});
145+
}
137146

138147
}
139148

spring-cloud-kubernetes-client-config/src/main/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetector.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public class KubernetesClientEventBasedSecretsChangeDetector extends Configurati
7171

7272
private final boolean enableReloadFiltering;
7373

74+
private final boolean monitoringSecrets;
75+
7476
private final ResourceEventHandler<V1Secret> handler = new ResourceEventHandler<>() {
7577

7678
@Override
@@ -110,34 +112,37 @@ public KubernetesClientEventBasedSecretsChangeDetector(CoreV1Api coreV1Api, Conf
110112
this.coreV1Api = coreV1Api;
111113
this.apiClient = createApiClientForInformerClient();
112114
this.enableReloadFiltering = properties.enableReloadFiltering();
115+
this.monitoringSecrets = properties.monitoringSecrets();
113116
namespaces = namespaces(kubernetesNamespaceProvider, properties, "secret");
114117
}
115118

116119
@PostConstruct
117120
void inform() {
118121
LOG.info(() -> "Kubernetes event-based secrets change detector activated");
119122

120-
namespaces.forEach(namespace -> {
121-
SharedIndexInformer<V1Secret> informer;
122-
String[] filter = new String[1];
123-
124-
if (enableReloadFiltering) {
125-
filter[0] = ConfigReloadProperties.RELOAD_LABEL_FILTER + "=true";
126-
}
127-
SharedInformerFactory factory = new SharedInformerFactory(apiClient);
128-
factories.add(factory);
129-
informer = factory
130-
.sharedIndexInformerFor((CallGeneratorParams params) -> coreV1Api.listNamespacedSecretCall(namespace,
131-
null, null, null, null, filter[0], null, params.resourceVersion, null, null,
132-
params.timeoutSeconds, params.watch, null), V1Secret.class, V1SecretList.class);
133-
134-
LOG.debug(() -> "added secret informer for namespace : " + namespace + " with filter : " + filter[0]);
135-
136-
informer.addEventHandler(handler);
137-
informers.add(informer);
138-
factory.startAllRegisteredInformers();
139-
});
140-
123+
if (monitoringSecrets) {
124+
namespaces.forEach(namespace -> {
125+
SharedIndexInformer<V1Secret> informer;
126+
String[] filter = new String[1];
127+
128+
if (enableReloadFiltering) {
129+
filter[0] = ConfigReloadProperties.RELOAD_LABEL_FILTER + "=true";
130+
}
131+
SharedInformerFactory factory = new SharedInformerFactory(apiClient);
132+
factories.add(factory);
133+
informer = factory.sharedIndexInformerFor(
134+
(CallGeneratorParams params) -> coreV1Api.listNamespacedSecretCall(namespace, null, null, null,
135+
null, filter[0], null, params.resourceVersion, null, null, params.timeoutSeconds,
136+
params.watch, null),
137+
V1Secret.class, V1SecretList.class);
138+
139+
LOG.debug(() -> "added secret informer for namespace : " + namespace + " with filter : " + filter[0]);
140+
141+
informer.addEventHandler(handler);
142+
informers.add(informer);
143+
factory.startAllRegisteredInformers();
144+
});
145+
}
141146
}
142147

143148
@PreDestroy

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import java.time.Duration;
2020

2121
import jakarta.annotation.PostConstruct;
22-
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
2422

2523
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
2624
import org.springframework.core.env.AbstractEnvironment;
2725
import org.springframework.core.env.MapPropertySource;
26+
import org.springframework.core.log.LogAccessor;
2827
import org.springframework.scheduling.TaskScheduler;
2928
import org.springframework.scheduling.support.PeriodicTrigger;
3029

@@ -38,7 +37,7 @@
3837
*/
3938
public class PollingConfigMapChangeDetector extends ConfigurationChangeDetector {
4039

41-
protected Log log = LogFactory.getLog(getClass());
40+
private static final LogAccessor LOG = new LogAccessor(PollingConfigMapChangeDetector.class);
4241

4342
private final PropertySourceLocator propertySourceLocator;
4443

@@ -48,6 +47,8 @@ public class PollingConfigMapChangeDetector extends ConfigurationChangeDetector
4847

4948
private final long period;
5049

50+
// this beans has already a Conditional on this property, we only have it here
51+
// in case it is created manually.
5152
private final boolean monitorConfigMaps;
5253

5354
public PollingConfigMapChangeDetector(AbstractEnvironment environment, ConfigReloadProperties properties,
@@ -63,19 +64,22 @@ public PollingConfigMapChangeDetector(AbstractEnvironment environment, ConfigRel
6364

6465
@PostConstruct
6566
private void init() {
66-
log.info("Kubernetes polling configMap change detector activated");
67-
PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofMillis(period));
68-
trigger.setInitialDelay(Duration.ofMillis(period));
69-
taskExecutor.schedule(this::executeCycle, trigger);
67+
if (monitorConfigMaps) {
68+
LOG.info(() -> "Kubernetes polling configMap change detector activated");
69+
PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofMillis(period));
70+
trigger.setInitialDelay(Duration.ofMillis(period));
71+
taskExecutor.schedule(this::executeCycle, trigger);
72+
}
73+
else {
74+
LOG.debug(() -> "Kubernetes polling configMap change detector disabled");
75+
}
7076
}
7177

7278
private void executeCycle() {
73-
if (monitorConfigMaps) {
74-
boolean changedConfigMap = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass);
75-
if (changedConfigMap) {
76-
log.info("Detected change in config maps");
77-
reloadProperties();
78-
}
79+
boolean changedConfigMap = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass);
80+
if (changedConfigMap) {
81+
LOG.info(() -> "Detected change in config maps");
82+
reloadProperties();
7983
}
8084
}
8185

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import java.time.Duration;
2020

2121
import jakarta.annotation.PostConstruct;
22-
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
2422

2523
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
2624
import org.springframework.core.env.AbstractEnvironment;
2725
import org.springframework.core.env.MapPropertySource;
26+
import org.springframework.core.log.LogAccessor;
2827
import org.springframework.scheduling.TaskScheduler;
2928
import org.springframework.scheduling.support.PeriodicTrigger;
3029

@@ -38,7 +37,7 @@
3837
*/
3938
public class PollingSecretsChangeDetector extends ConfigurationChangeDetector {
4039

41-
protected Log log = LogFactory.getLog(getClass());
40+
private static final LogAccessor LOG = new LogAccessor(PollingSecretsChangeDetector.class);
4241

4342
private final PropertySourceLocator propertySourceLocator;
4443

@@ -48,6 +47,8 @@ public class PollingSecretsChangeDetector extends ConfigurationChangeDetector {
4847

4948
private final long period;
5049

50+
// this beans has already a Conditional on this property, we only have it here
51+
// in case it is created manually.
5152
private final boolean monitorSecrets;
5253

5354
public PollingSecretsChangeDetector(AbstractEnvironment environment, ConfigReloadProperties properties,
@@ -63,19 +64,22 @@ public PollingSecretsChangeDetector(AbstractEnvironment environment, ConfigReloa
6364

6465
@PostConstruct
6566
private void init() {
66-
log.info("Kubernetes polling secrets change detector activated");
67-
PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofMillis(period));
68-
trigger.setInitialDelay(Duration.ofMillis(period));
69-
taskExecutor.schedule(this::executeCycle, trigger);
67+
if (monitorSecrets) {
68+
LOG.info(() -> "Kubernetes polling secrets change detector activated");
69+
PeriodicTrigger trigger = new PeriodicTrigger(Duration.ofMillis(period));
70+
trigger.setInitialDelay(Duration.ofMillis(period));
71+
taskExecutor.schedule(this::executeCycle, trigger);
72+
}
73+
else {
74+
LOG.info(() -> "Kubernetes polling secrets change detector disabled");
75+
}
7076
}
7177

7278
private void executeCycle() {
73-
if (monitorSecrets) {
74-
boolean changedSecrets = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass);
75-
if (changedSecrets) {
76-
log.info("Detected change in secrets");
77-
reloadProperties();
78-
}
79+
boolean changedSecrets = ConfigReloadUtil.reload(propertySourceLocator, environment, propertySourceClass);
80+
if (changedSecrets) {
81+
LOG.info(() -> "Detected change in secrets");
82+
reloadProperties();
7983
}
8084
}
8185

spring-cloud-kubernetes-fabric8-config/src/main/java/org/springframework/cloud/kubernetes/fabric8/config/reload/Fabric8EventBasedConfigMapChangeDetector.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class Fabric8EventBasedConfigMapChangeDetector extends ConfigurationChang
6565

6666
private final boolean enableReloadFiltering;
6767

68+
private final boolean monitorConfigMaps;
69+
6870
public Fabric8EventBasedConfigMapChangeDetector(AbstractEnvironment environment, ConfigReloadProperties properties,
6971
KubernetesClient kubernetesClient, ConfigurationUpdateStrategy strategy,
7072
Fabric8ConfigMapPropertySourceLocator fabric8ConfigMapPropertySourceLocator,
@@ -73,30 +75,38 @@ public Fabric8EventBasedConfigMapChangeDetector(AbstractEnvironment environment,
7375
this.kubernetesClient = kubernetesClient;
7476
this.fabric8ConfigMapPropertySourceLocator = fabric8ConfigMapPropertySourceLocator;
7577
this.enableReloadFiltering = properties.enableReloadFiltering();
78+
this.monitorConfigMaps = properties.monitoringConfigMaps();
7679
namespaces = namespaces(kubernetesClient, namespaceProvider, properties, "configmap");
7780
}
7881

7982
@PostConstruct
8083
private void inform() {
81-
LOG.info("Kubernetes event-based configMap change detector activated");
82-
83-
namespaces.forEach(namespace -> {
84-
SharedIndexInformer<ConfigMap> informer;
85-
if (enableReloadFiltering) {
86-
informer = kubernetesClient.configMaps()
87-
.inNamespace(namespace)
88-
.withLabels(Map.of(ConfigReloadProperties.RELOAD_LABEL_FILTER, "true"))
89-
.inform();
90-
LOG.debug("added configmap informer for namespace : " + namespace + " with enabled filter");
91-
}
92-
else {
93-
informer = kubernetesClient.configMaps().inNamespace(namespace).inform();
94-
LOG.debug("added configmap informer for namespace : " + namespace);
95-
}
84+
if (monitorConfigMaps) {
85+
86+
LOG.info("Kubernetes event-based configMap change detector activated");
87+
88+
namespaces.forEach(namespace -> {
89+
SharedIndexInformer<ConfigMap> informer;
90+
if (enableReloadFiltering) {
91+
informer = kubernetesClient.configMaps()
92+
.inNamespace(namespace)
93+
.withLabels(Map.of(ConfigReloadProperties.RELOAD_LABEL_FILTER, "true"))
94+
.inform();
95+
LOG.debug("added configmap informer for namespace : " + namespace + " with enabled filter");
96+
}
97+
else {
98+
informer = kubernetesClient.configMaps().inNamespace(namespace).inform();
99+
LOG.debug("added configmap informer for namespace : " + namespace);
100+
}
101+
102+
informer.addEventHandler(new ConfigMapInformerAwareEventHandler(informer));
103+
informers.add(informer);
104+
});
105+
}
106+
else {
107+
LOG.info("Kubernetes event-based configMap change detector disabled");
108+
}
96109

97-
informer.addEventHandler(new ConfigMapInformerAwareEventHandler(informer));
98-
informers.add(informer);
99-
});
100110
}
101111

102112
@PreDestroy
@@ -107,7 +117,7 @@ private void shutdown() {
107117
kubernetesClient.close();
108118
}
109119

110-
protected void onEvent(ConfigMap configMap) {
120+
private void onEvent(ConfigMap configMap) {
111121
boolean reload = ConfigReloadUtil.reload("config-map", configMap.toString(),
112122
fabric8ConfigMapPropertySourceLocator, environment, Fabric8ConfigMapPropertySource.class);
113123
if (reload) {

0 commit comments

Comments
 (0)