Skip to content

Commit 824c192

Browse files
metacosmcsviri
authored andcommitted
fix: informers are already started asynchronously
1 parent 09bd45c commit 824c192

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerManager.java

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.javaoperatorsdk.operator.ReconcilerUtils;
2525
import io.javaoperatorsdk.operator.api.config.Cloner;
2626
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
27-
import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
2827
import io.javaoperatorsdk.operator.api.config.ResourceConfiguration;
2928
import io.javaoperatorsdk.operator.processing.LifecycleAware;
3029
import io.javaoperatorsdk.operator.processing.event.ResourceID;
@@ -47,22 +46,7 @@ public class InformerManager<T extends HasMetadata, C extends ResourceConfigurat
4746
@Override
4847
public void start() throws OperatorException {
4948
// make sure informers are all started before proceeding further
50-
ExecutorServiceManager.executeAndWaitForCompletion(
51-
() -> sources.values().parallelStream().forEach(source -> {
52-
// change thread name for easier debugging
53-
final var thread = Thread.currentThread();
54-
final var name = thread.getName();
55-
try {
56-
thread.setName(source.informerInfo() + " " + thread.getId());
57-
source.start();
58-
} catch (Exception e) {
59-
throw new OperatorException("Couldn't start informer: " + source, e);
60-
} finally {
61-
// restore original name
62-
thread.setName(name);
63-
}
64-
}),
65-
"InformerStart");
49+
sources.values().parallelStream().forEach(InformerWrapper::start);
6650
}
6751

6852
void initSources(MixedOperation<T, KubernetesResourceList<T>, Resource<T>> client,
@@ -103,19 +87,18 @@ public void changeNamespaces(Set<String> namespaces) {
10387
log.debug("Stopped informer {} for namespaces: {}", this, sourcesToRemove);
10488
sourcesToRemove.forEach(k -> sources.remove(k).stop());
10589

106-
ExecutorServiceManager.executeAndWaitForCompletion(
107-
() -> namespaces.forEach(ns -> {
108-
if (!sources.containsKey(ns)) {
109-
final var source =
110-
createEventSource(
111-
client.inNamespace(ns).withLabelSelector(configuration.getLabelSelector()),
112-
eventHandler, ns);
113-
source.addIndexers(this.indexers);
114-
source.start();
115-
log.debug("Registered new {} -> {} for namespace: {}", this, source,
116-
ns);
117-
}
118-
}), "InformerStart");
90+
namespaces.forEach(ns -> {
91+
if (!sources.containsKey(ns)) {
92+
final var source =
93+
createEventSource(
94+
client.inNamespace(ns).withLabelSelector(configuration.getLabelSelector()),
95+
eventHandler, ns);
96+
source.addIndexers(this.indexers);
97+
source.start();
98+
log.debug("Registered new {} -> {} for namespace: {}", this, source,
99+
ns);
100+
}
101+
});
119102
}
120103

121104

@@ -131,19 +114,15 @@ private InformerWrapper<T> createEventSource(
131114

132115
@Override
133116
public void stop() {
134-
ExecutorServiceManager.executeAndWaitForCompletion(
135-
() -> {
136-
log.info("Stopping {}", this);
137-
sources.forEach((ns, source) -> {
138-
try {
139-
log.debug("Stopping informer for namespace: {} -> {}", ns, source);
140-
source.stop();
141-
} catch (Exception e) {
142-
log.warn("Error stopping informer for namespace: {} -> {}", ns, source, e);
143-
}
144-
});
145-
},
146-
"StopInformer");
117+
log.info("Stopping {}", this);
118+
sources.forEach((ns, source) -> {
119+
try {
120+
log.debug("Stopping informer for namespace: {} -> {}", ns, source);
121+
source.stop();
122+
} catch (Exception e) {
123+
log.warn("Error stopping informer for namespace: {} -> {}", ns, source, e);
124+
}
125+
});
147126
}
148127

149128
@Override

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerWrapper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public void start() throws OperatorException {
6464
if (!configService.stopOnInformerErrorDuringStartup()) {
6565
informer.exceptionHandler((b, t) -> !ExceptionHandler.isDeserializationException(t));
6666
}
67+
// change thread name for easier debugging
68+
final var thread = Thread.currentThread();
69+
final var name = thread.getName();
6770
try {
71+
thread.setName(informerInfo() + " " + thread.getId());
6872
var start = informer.start();
6973
// note that in case we don't put here timeout and stopOnInformerErrorDuringStartup is
7074
// false, and there is a rbac issue the get never returns; therefore operator never really
@@ -81,6 +85,9 @@ public void start() throws OperatorException {
8185
} catch (InterruptedException e) {
8286
Thread.currentThread().interrupt();
8387
throw new IllegalStateException(e);
88+
} finally {
89+
// restore original name
90+
thread.setName(name);
8491
}
8592

8693
} catch (Exception e) {
@@ -146,7 +153,7 @@ public String toString() {
146153
return informerInfo() + " (" + informer + ')';
147154
}
148155

149-
String informerInfo() {
156+
private String informerInfo() {
150157
return "InformerWrapper [" + versionedFullResourceName() + "]";
151158
}
152159
}

0 commit comments

Comments
 (0)