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
@@ -401,21 +391,38 @@ public class TomcatReconciler implements Reconciler<Tomcat>, EventSourceInitiali
401
391
In the example above an `InformerEventSource` is registered (more on this specific eventsource later).Multiple things
402
392
are going on here:
403
393
404
-
1.An `SharedIndexInformer` (classfrom fabric8 Kubernetes client) is created. This will watch and produce events for
394
+
1.In the background `SharedIndexInformer` (classfrom fabric8 Kubernetes client) is created. This will watch and produce events for
405
395
`Deployments` in every namespace, but will filter them based on label. So `Deployments` which are not managed by
406
396
`tomcat-operator` (the label is not present on them) will not trigger a reconciliation.
407
397
2. In the next step
408
398
an [InformerEventSource](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java)
409
-
is created, which wraps the `SharedIndexInformer`. In addition to that a mapping functions is provided, **this maps
410
-
the event of the watched resource (in this case `Deployment`) to the custom resources to reconcile**. Not that in
411
-
this case this is a simple task, since `Deployment` is already created with an owner reference. Therefore,
412
-
the `ResourceID`
413
-
what identifies the custom resource to reconcile is created from the owner reference.
399
+
is created, which wraps the `SharedIndexInformer`. In addition to that a mapping functions is provided,
400
+
with `withSecondaryToPrimaryMapper`, this maps the event of the watched resource (in this case `Deployment`) to the
401
+
custom resources to reconcile. Note that usually this is covered by a default mapper , when `Deployment`
402
+
is created with an owner reference, the default mapper gets the mapping information from there. Thus,
403
+
the `ResourceID` what identifies the custom resource to reconcile is created from the owner reference.
404
+
For sake of the example a mapper is added that maps secondary to primary resource based on annotations.
414
405
415
406
Note that a set of `ResourceID` is returned, this is usually just a set with one element. The possibility to specify
416
407
multiple values are there to cover some rare corner cases. If an irrelevant resource is observed, an empty set can
417
408
be returned to not reconcile any custom resource.
418
409
410
+
### Managing Relation between Primary and Secondary Resources
411
+
412
+
As already touched in previous section, a `SecondaryToPrimaryMapper` is required to map events to trigger reconciliation
413
+
of the primary resource. By default, this is handled with a mapper that utilizes owner references. If an owner reference
414
+
cannot be used (for example resources are in different namespace), other mapper can be provided, typically an annotation
415
+
based on is provided.
416
+
417
+
Adding a `SecondaryToPrimaryMapper` is typically sufficient when there is a one-to-many relationship between primary and
418
+
secondary resources. The secondary resources can be mapped to its primary owner, and this is enough information to also
419
+
get the resource using the API from the context in reconciler: `context.getSecondaryResources(...)`. There are however
420
+
cases when to map the other way around this mapper is not enough, a `PrimaryToSecondaryMapper` is required.
421
+
This is typically when there is a many-to-one or many-to-many relationship between resources, thus the primary resource
422
+
is referencing a secondary resources. In these cases the mentioned reverse mapper is required to work properly.
423
+
See [PrimaryToSecondaryIT](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework/src/test/java/io/javaoperatorsdk/operator/PrimaryToSecondaryIT.java)
424
+
integration test for a sample.
425
+
419
426
### Built-in EventSources
420
427
421
428
There are multiple event-sources provided out of the box, the following are some more central ones:
Copy file name to clipboardExpand all lines: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerConfiguration.java
Copy file name to clipboardExpand all lines: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java
Copy file name to clipboardExpand all lines: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/DefaultPrimaryToSecondaryIndexTest.java
0 commit comments