Skip to content

Commit 36e204f

Browse files
authored
Merge pull request spring-cloud#2161 from wind57/enable_monitoring_filter_for_event_based_reloading
only start event based informers if monitoring is enabled
2 parents 025eef7 + 95a7c19 commit 36e204f

File tree

5 files changed

+118
-88
lines changed

5 files changed

+118
-88
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-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) {

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class Fabric8EventBasedSecretsChangeDetector extends ConfigurationChangeD
6565

6666
private final boolean enableReloadFiltering;
6767

68+
private final boolean monitorSecrets;
69+
6870
public Fabric8EventBasedSecretsChangeDetector(AbstractEnvironment environment, ConfigReloadProperties properties,
6971
KubernetesClient kubernetesClient, ConfigurationUpdateStrategy strategy,
7072
Fabric8SecretsPropertySourceLocator fabric8SecretsPropertySourceLocator,
@@ -73,6 +75,7 @@ public Fabric8EventBasedSecretsChangeDetector(AbstractEnvironment environment, C
7375
this.kubernetesClient = kubernetesClient;
7476
this.fabric8SecretsPropertySourceLocator = fabric8SecretsPropertySourceLocator;
7577
this.enableReloadFiltering = properties.enableReloadFiltering();
78+
this.monitorSecrets = properties.monitoringSecrets();
7679
namespaces = namespaces(kubernetesClient, namespaceProvider, properties, "secrets");
7780
}
7881

@@ -86,35 +89,39 @@ private void shutdown() {
8689

8790
@PostConstruct
8891
private void inform() {
89-
LOG.info("Kubernetes event-based secrets change detector activated");
90-
91-
namespaces.forEach(namespace -> {
92-
SharedIndexInformer<Secret> informer;
93-
if (enableReloadFiltering) {
94-
informer = kubernetesClient.secrets()
95-
.inNamespace(namespace)
96-
.withLabels(Map.of(ConfigReloadProperties.RELOAD_LABEL_FILTER, "true"))
97-
.inform();
98-
LOG.debug("added secret informer for namespace : " + namespace + " with enabled filter");
99-
}
100-
else {
101-
informer = kubernetesClient.secrets().inNamespace(namespace).inform();
102-
LOG.debug("added secret informer for namespace : " + namespace);
103-
}
104-
105-
informer.addEventHandler(new SecretInformerAwareEventHandler(informer));
106-
informers.add(informer);
107-
});
92+
if (monitorSecrets) {
93+
94+
LOG.info("Kubernetes event-based secrets change detector activated");
95+
96+
namespaces.forEach(namespace -> {
97+
SharedIndexInformer<Secret> informer;
98+
if (enableReloadFiltering) {
99+
informer = kubernetesClient.secrets()
100+
.inNamespace(namespace)
101+
.withLabels(Map.of(ConfigReloadProperties.RELOAD_LABEL_FILTER, "true"))
102+
.inform();
103+
LOG.debug("added secret informer for namespace : " + namespace + " with enabled filter");
104+
}
105+
else {
106+
informer = kubernetesClient.secrets().inNamespace(namespace).inform();
107+
LOG.debug("added secret informer for namespace : " + namespace);
108+
}
109+
110+
informer.addEventHandler(new SecretInformerAwareEventHandler(informer));
111+
informers.add(informer);
112+
});
113+
}
114+
else {
115+
LOG.debug("Kubernetes event-based secrets change detector deactivated");
116+
}
108117
}
109118

110-
protected void onEvent(Secret secret) {
111-
119+
private void onEvent(Secret secret) {
112120
boolean reload = ConfigReloadUtil.reload("secrets", secret.toString(), fabric8SecretsPropertySourceLocator,
113121
environment, Fabric8SecretsPropertySource.class);
114122
if (reload) {
115123
reloadProperties();
116124
}
117-
118125
}
119126

120127
private final class SecretInformerAwareEventHandler implements ResourceEventHandler<Secret> {

spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/reload_it/EventReloadSecretTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
classes = { EventReloadSecretTest.TestConfig.class })
6666
@EnableKubernetesMockClient(crud = true)
6767
@ExtendWith(OutputCaptureExtension.class)
68-
6968
class EventReloadSecretTest {
7069

7170
private static final boolean FAIL_FAST = false;
@@ -99,7 +98,7 @@ void test(CapturedOutput output) {
9998
.inNamespace(NAMESPACE);
10099

101100
// makes sure that when 'onEvent' is triggered (because we added a config map)
102-
// the call to /api/v1/namespaces/spring-k8s/secrets will fail with an
101+
// the call to /api/v1/namespaces/spring-k8s/secrets will not fail with an
103102
// Exception
104103
MixedOperation<Secret, SecretList, Resource<Secret>> mixedOperation = Mockito.mock(MixedOperation.class);
105104
NonNamespaceOperation<Secret, SecretList, Resource<Secret>> mockedOperation = Mockito
@@ -183,16 +182,16 @@ AbstractEnvironment environment() {
183182
@Bean
184183
@Primary
185184
ConfigReloadProperties configReloadProperties() {
186-
return new ConfigReloadProperties(true, true, false, ConfigReloadProperties.ReloadStrategy.REFRESH,
185+
return new ConfigReloadProperties(true, true, true, ConfigReloadProperties.ReloadStrategy.REFRESH,
187186
ConfigReloadProperties.ReloadDetectionMode.EVENT, Duration.ofMillis(2000), Set.of(NAMESPACE), false,
188187
Duration.ofSeconds(2));
189188
}
190189

191190
@Bean
192191
@Primary
193192
SecretsConfigProperties secretsConfigProperties() {
194-
return new SecretsConfigProperties(true, Map.of(), List.of(), List.of(), true, SECRET_NAME, NAMESPACE,
195-
false, true, FAIL_FAST, RetryProperties.DEFAULT);
193+
return new SecretsConfigProperties(true, Map.of(), List.of(), List.of(), true, SECRET_NAME, NAMESPACE, true,
194+
true, FAIL_FAST, RetryProperties.DEFAULT);
196195
}
197196

198197
@Bean

0 commit comments

Comments
 (0)