|
4 | 4 | import java.util.Set;
|
5 | 5 | import java.util.function.Function;
|
6 | 6 |
|
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
7 | 10 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
8 | 11 | import io.fabric8.kubernetes.client.KubernetesClient;
|
9 | 12 | import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
|
|
16 | 19 |
|
17 | 20 | public class InformerEventSource<T extends HasMetadata> extends AbstractEventSource {
|
18 | 21 |
|
| 22 | + private static final Logger log = LoggerFactory.getLogger(InformerEventSource.class); |
| 23 | + |
19 | 24 | private final SharedInformer<T> sharedInformer;
|
20 |
| - private final Function<T, Set<CustomResourceID>> resourceToUIDs; |
| 25 | + private final Function<T, Set<CustomResourceID>> resourceToCustomResourceIDSet; |
21 | 26 | private final Function<HasMetadata, T> associatedWith;
|
22 | 27 | private final boolean skipUpdateEventPropagationIfNoChange;
|
23 | 28 |
|
24 | 29 | public InformerEventSource(SharedInformer<T> sharedInformer,
|
25 |
| - Function<T, Set<CustomResourceID>> resourceToUIDs) { |
26 |
| - this(sharedInformer, resourceToUIDs, null, true); |
| 30 | + Function<T, Set<CustomResourceID>> resourceToCustomResourceIDSet) { |
| 31 | + this(sharedInformer, resourceToCustomResourceIDSet, null, true); |
27 | 32 | }
|
28 | 33 |
|
29 | 34 | public InformerEventSource(KubernetesClient client, Class<T> type,
|
30 |
| - Function<T, Set<CustomResourceID>> resourceToUIDs) { |
31 |
| - this(client, type, resourceToUIDs, false); |
| 35 | + Function<T, Set<CustomResourceID>> resourceToCustomResourceIDSet) { |
| 36 | + this(client, type, resourceToCustomResourceIDSet, false); |
32 | 37 | }
|
33 | 38 |
|
34 | 39 | InformerEventSource(KubernetesClient client, Class<T> type,
|
35 |
| - Function<T, Set<CustomResourceID>> resourceToUIDs, |
| 40 | + Function<T, Set<CustomResourceID>> resourceToCustomResourceIDSet, |
36 | 41 | boolean skipUpdateEventPropagationIfNoChange) {
|
37 |
| - this(client.informers().sharedIndexInformerFor(type, 0), resourceToUIDs, null, |
| 42 | + this(client.informers().sharedIndexInformerFor(type, 0), resourceToCustomResourceIDSet, null, |
38 | 43 | skipUpdateEventPropagationIfNoChange);
|
39 | 44 | }
|
40 | 45 |
|
41 | 46 | public InformerEventSource(SharedInformer<T> sharedInformer,
|
42 |
| - Function<T, Set<CustomResourceID>> resourceToUIDs, |
| 47 | + Function<T, Set<CustomResourceID>> resourceToCustomResourceIDSet, |
43 | 48 | Function<HasMetadata, T> associatedWith,
|
44 | 49 | boolean skipUpdateEventPropagationIfNoChange) {
|
45 | 50 | this.sharedInformer = sharedInformer;
|
46 |
| - this.resourceToUIDs = resourceToUIDs; |
| 51 | + this.resourceToCustomResourceIDSet = resourceToCustomResourceIDSet; |
47 | 52 | this.skipUpdateEventPropagationIfNoChange = skipUpdateEventPropagationIfNoChange;
|
| 53 | + if (sharedInformer.isRunning()) { |
| 54 | + log.warn( |
| 55 | + "Informer is already running on event source creation, this is not desirable and may " + |
| 56 | + "lead to non deterministic behavior."); |
| 57 | + } |
48 | 58 |
|
49 | 59 | this.associatedWith = Objects.requireNonNullElseGet(associatedWith, () -> cr -> {
|
50 | 60 | final var metadata = cr.getMetadata();
|
@@ -76,13 +86,20 @@ public void onDelete(T t, boolean b) {
|
76 | 86 | }
|
77 | 87 |
|
78 | 88 | private void propagateEvent(T object) {
|
79 |
| - var uids = resourceToUIDs.apply(object); |
| 89 | + var uids = resourceToCustomResourceIDSet.apply(object); |
80 | 90 | if (uids.isEmpty()) {
|
81 | 91 | return;
|
82 | 92 | }
|
83 | 93 | uids.forEach(uid -> {
|
84 | 94 | Event event = new Event(CustomResourceID.fromResource(object));
|
85 |
| - this.eventHandler.handleEvent(event); |
| 95 | + /* |
| 96 | + * In fabric8 client for certain cases informers can be created on in a way that they are |
| 97 | + * automatically started, what would cause a NullPointerException here, since an event might |
| 98 | + * be received between creation and registration. |
| 99 | + */ |
| 100 | + if (eventHandler != null) { |
| 101 | + this.eventHandler.handleEvent(event); |
| 102 | + } |
86 | 103 | });
|
87 | 104 | }
|
88 | 105 |
|
|
0 commit comments