Skip to content

Commit dc3c566

Browse files
committed
fix: minor improvements
Signed-off-by: Chris Laprun <[email protected]>
1 parent 620972b commit dc3c566

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

docs/content/en/docs/documentation/working-with-es-caches.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ allowing you to cache external resources and trigger reconciliation when those r
1919
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).
2020
[InformerEventSource](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java)
2121
in Java Operator SDK wraps the Fabric8 client informers.
22-
This wrapper adds additional capabilities specifically required for controllers
23-
(note that informers have broader applications beyond just implementing controllers).
22+
While this wrapper adds additional capabilities specifically required for controllers, this is the event
23+
source that most likely will be used to deal with Kubernetes resources.
2424

2525
These additional capabilities include:
2626
- Maintaining an index that maps secondary resources in the informer cache to their related primary resources
@@ -31,8 +31,8 @@ These additional capabilities include:
3131
### Associating Secondary Resources to Primary Resource
3232

3333
Event sources need to trigger the appropriate reconciler, providing the correct primary resource, whenever one of their
34-
handled secondary resources changes. It is thus core to an event source's role to identify which primary resource (
35-
usually, your custom resource) is potentially impacted by that change.
34+
handled secondary resources changes. It is thus core to an event source's role to identify which primary resource
35+
(usually, your custom resource) is potentially impacted by that change.
3636
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)
3737
for this purpose. For `InformerEventSources`, which target Kubernetes resources, this mapping is typically done using
3838
either the owner reference or an annotation on the secondary resource. For external resources, other mechanisms need to
@@ -53,8 +53,8 @@ secondary resources using the `Set<R> getSecondaryResources(Class<R> expectedTyp
5353
provided as part of the `reconcile` method.
5454

5555
For `InformerEventSource`, this will leverage the associated `PrimaryToSecondaryIndex`. Resources are then retrieved
56-
from the informer's cache. Note that since all those steps work
57-
on top of indexes, those operations are very fast, usually O(1).
56+
from the informer's cache. Note that since all those steps work on top of indexes, those operations are very fast,
57+
usually O(1).
5858

5959
While we've focused mostly on `InformerEventSource`, this concept can be extended to all `EventSources`, since
6060
[`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)
@@ -96,7 +96,7 @@ public class WebPageReconciler implements Reconciler<WebPage> {
9696
## The Use Case for PrimaryToSecondaryMapper
9797

9898
**TL;DR**: `PrimaryToSecondaryMapper` allows `InformerEventSource` to access secondary resources directly
99-
instead of using the PrimaryToSecondaryIndex. When this mapper is configured, `InformerEventSource.getSecondaryResources(..)`
99+
instead of using the `PrimaryToSecondaryIndex`. When this mapper is configured, `InformerEventSource.getSecondaryResources(..)`
100100
will call the mapper to retrieve the target secondary resources. This is typically required when the `SecondaryToPrimaryMapper`
101101
uses informer caches to list the target resources.
102102

@@ -113,7 +113,7 @@ Consider this example: a `Job` primary resource can be assigned to run on a clus
113113
`Cluster` resource.
114114
Multiple jobs can run on the same cluster, so multiple `Job` resources can reference the same `Cluster` resource. However,
115115
a `Cluster` resource shouldn't know about `Job` resources, as this information isn't part of what defines a cluster.
116-
When a cluster changes, we might want to redirect associated jobs to other clusters. Our reconciler
116+
When a cluster changes, though, we might want to redirect associated jobs to other clusters. Our reconciler
117117
therefore needs to determine which `Job` (primary) resources are associated with the changed `Cluster` (secondary)
118118
resource.
119119
See full
@@ -122,10 +122,12 @@ sample [here](https://github.com/operator-framework/java-operator-sdk/blob/main/
122122
```java
123123
InformerEventSourceConfiguration
124124
.from(Cluster.class, Job.class)
125-
.withSecondaryToPrimaryMapper(cluster -> context.getPrimaryCache()
126-
.list().filter(job -> job.getSpec().getClusterName().equals(cluster.getMetadata().getName()))
127-
.map(ResourceID::fromResource)
128-
.collect(Collectors.toSet()))
125+
.withSecondaryToPrimaryMapper(cluster ->
126+
context.getPrimaryCache()
127+
.list()
128+
.filter(job -> job.getSpec().getClusterName().equals(cluster.getMetadata().getName()))
129+
.map(ResourceID::fromResource)
130+
.collect(Collectors.toSet()))
129131
```
130132

131133
This configuration will trigger all related `Jobs` when the associated cluster changes and maintains the `PrimaryToSecondaryIndex`,
@@ -166,11 +168,10 @@ In fact, when this mapper is configured, the `PrimaryToSecondaryIndex` isn't eve
166168
In the `SecondaryToPrimaryMapper` example above, we iterate through all resources in the cache:
167169

168170
```java
169-
context.getPrimaryCache()
170-
.list().filter(job -> job.getSpec().getClusterName().equals(cluster.getMetadata().getName()))
171+
context.getPrimaryCache().list().filter(job -> job.getSpec().getClusterName().equals(cluster.getMetadata().getName()))
171172
```
172173

173-
This approach can be inefficient when dealing with a large number of primary (Job) resources. To improve performance,
174+
This approach can be inefficient when dealing with a large number of primary (`Job`) resources. To improve performance,
174175
you can create an index in the underlying Informer that indexes the target jobs for each cluster:
175176

176177
```java
@@ -191,7 +192,7 @@ where `indexKey` is a String that uniquely identifies a Cluster:
191192
```java
192193
private String indexKey(String clusterName, String namespace) {
193194
return clusterName + "#" + namespace;
194-
}
195+
}
195196
```
196197

197198
With this index in place, you can retrieve the target resources very efficiently:
@@ -214,4 +215,4 @@ With this index in place, you can retrieve the target resources very efficiently
214215
.map(ResourceID::fromResource)
215216
.collect(Collectors.toSet()))
216217
.withNamespacesInheritedFromController().build(), context);
217-
```
218+
```

0 commit comments

Comments
 (0)