You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As described in [Event sources and related topics](eventing.md) event sources are the backbone
7
-
for caching resources and triggering reconciliation of primary resources related
8
-
to these secondary resources.
7
+
for caching resources and triggering the reconciliation for primary resources thar are related
8
+
to cached resources.
9
9
10
-
In Kubernetes parlance, `Informers` handle that responsibility. Without going into
11
-
the details (there are plenty of good documents online regarding this topics), informers
12
-
watch resources, cache them, and emit an event whenever watched resources change.
10
+
In Kubernetes world, the component that does this is called Informer. Without going into
11
+
the details (there are plenty of good documents online regarding informers), its responsibility
12
+
is to watch resources, cache them, and emit an event if the resource changed.
13
13
14
-
`EventSource` generalizes this concept to also cover non-Kubernetes resources. Thus,
15
-
allowing caching of external resources, and triggering reconciliation when those change.
14
+
EventSource is a generalized concept of Informer to non-Kubernetes resources. Thus,
15
+
to cache external resources, and trigger reconciliation if those change.
16
16
17
17
## The InformerEventSource
18
18
19
-
The underlying informer implementation comes from the Fabric8 client,
20
-
called [DefaultSharedIndexInformer](https://github.com/fabric8io/kubernetes-client/blob/main/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/impl/DefaultSharedIndexInformer.java).
in Java Operator SDK wraps informers from Fabric8 client, thus presenting a unified front to deal with Kubernetes and
23
-
non-Kubernetes resources with the `EventSource` architecture.
19
+
The underlying informer implementation comes from the fabric8 client, called [DefaultSharedIndexInformer](https://github.com/fabric8io/kubernetes-client/blob/main/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/impl/DefaultSharedIndexInformer.java).
in Java Operator SDK wraps informers from fabric8 client.
22
+
The purpose of such wrapping is to add additional capabilities required for controllers.
23
+
(In general, Informers are not used only for implementing controllers).
24
24
25
-
However, `InformerEventSource` also provide additional capabilities such as:
26
-
27
-
- recording the relations between primary and secondary resources so that the event source knows which primary resource
28
-
to trigger a reconciler with whenever one of the cached secondary resources cached by the informer changes,
29
-
- setting up multiple informers for the same type if needed, for example to transparently watch multiple namespaces,
30
-
without you having to worry about it,
31
-
- dynamically adding/removing watched namespaces, if needed
32
-
- and more, outside of the scope of this document.
25
+
Such capabilities are:
26
+
- maintaining and index to which primary are the secondary resources in informer cache are related to.
27
+
- setting up multiple informers for the same type if needed. You need informer per namespace if the informer
28
+
is not watching the whole cluster.
29
+
- Dynamically adding/removing watched namespaces.
30
+
- Some others, what is out of the scope of this document.
33
31
34
32
### Associating Secondary Resources to Primary Resource
35
33
36
-
Event sources need to trigger the appropriate reconciler, providing the correct primary resource, whenever one of their
37
-
handled secondary resources changes. It is thus core to an event source's role to identify which primary resource (
38
-
usually, your custom resource) is potentially impacted by that change.
39
-
The framework uses [`SecondaryToPrimaryMapper`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/SecondaryToPrimaryMapper.java)
40
-
for this purpose. For `InformerEventSources`, which target Kubernetes resources, this mapping is typically done using
41
-
either the owner reference or an annotation on the secondary resource. For external resources, other mechanisms need to
42
-
be used and there are also cases where the default mechanisms provided by the SDK do not work, even for Kubernetes
43
-
resources.
44
-
45
-
However, once the event source has triggered a primary resource reconciliation, the associated reconciler needs to
46
-
access the secondary resources which changes caused the reconciliation. Indeed, the information from the secondary
47
-
resources might be needed during the reconciliation. For that purpose,
48
-
`InformerEventSource` maintains a reverse
49
-
index [PrimaryToSecondaryIndex](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/DefaultPrimaryToSecondaryIndex.java),
50
-
based on the result of the `SecondaryToPrimaryMapper`result.
34
+
The question is, how to trigger reconciliation of a primary resources (your custom resource),
35
+
when Informer receives a new resource.
36
+
For this purpose the framework uses [`SecondaryToPrimaryMapper`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/SecondaryToPrimaryMapper.java)
37
+
that tells (usually) based on the resource which primary resource reconciliation to trigger.
38
+
The mapping is usually done based on the owner reference or annotation on the secondary resource.
39
+
(But not always, as we will see)
40
+
41
+
It is important to realize that if a resource triggers the reconciliation of a primary resource, that
42
+
resource naturally will be used during reconciliation. So the reconciler will need to access them.
43
+
Therefore, InformerEventSource maintains a revers index [PrimaryToSecondaryIndex](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/DefaultPrimaryToSecondaryIndex.java),
44
+
based on the result of the `SecondaryToPrimaryMapper`result.
51
45
52
46
## Unified API for Related Resources
53
47
54
-
To access all related resources for a primary resource, the framework provides an API to access the related
55
-
secondary resources using the `Set<R> getSecondaryResources(Class<R> expectedType)` method of the `Context` object
56
-
provided as part of the `reconcile` method.
48
+
To access all related resources for a primary resource, the framework provides an API to access the related
actually implements the `Set<R> getSecondaryResources(P primary)` method that can be called from the `Context`.
59
+
We mostly talk about InformerEventSource, but this works in similar ways for generalized EventSources concept, since
60
+
the [`EventSource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/EventSource.java#L93)
61
+
actually implements the `Set<R> getSecondaryResources(P primary);` method. That is just called from the context.
65
62
66
-
As there can be multiple event sources for the same resource types, things are a little more complex: the union of each
67
-
event source results is returned.
63
+
It is a bit more complex than that, since there can be multiple event sources for the same type, in that case
64
+
the union of the results is returned.
68
65
69
66
## Getting Resources Directly from Event Sources
70
67
71
-
Note that nothing prevents you from directly accessing resources in the cache without going through
72
-
`getSecondaryResources(...)`:
68
+
Note that nothing stops you to directly access the resources in the cache (so not just through `getSecondaryResources(...)`):
0 commit comments