@@ -65,6 +65,7 @@ public class InformerEventSource<R extends HasMetadata, P extends HasMetadata>
65
65
private static final Logger log = LoggerFactory .getLogger (InformerEventSource .class );
66
66
// we need direct control for the indexer to propagate the just update resource also to the index
67
67
private final PrimaryToSecondaryMapper <P > primaryToSecondaryMapper ;
68
+ private final ComplementaryPrimaryToSecondaryIndex <R > complementaryPrimaryToSecondaryIndex ;
68
69
private final String id = UUID .randomUUID ().toString ();
69
70
70
71
public InformerEventSource (
@@ -98,13 +99,18 @@ private InformerEventSource(
98
99
// If there is a primary to secondary mapper there is no need for primary to secondary index.
99
100
primaryToSecondaryMapper = configuration .getPrimaryToSecondaryMapper ();
100
101
if (primaryToSecondaryMapper == null ) {
102
+ complementaryPrimaryToSecondaryIndex =
103
+ new DefaultComplementaryPrimaryToSecondaryIndex <>(
104
+ configuration .getSecondaryToPrimaryMapper ());
101
105
addIndexers (
102
106
Map .of (
103
107
PRIMARY_TO_SECONDARY_INDEX_NAME ,
104
108
(R r ) ->
105
109
configuration .getSecondaryToPrimaryMapper ().toPrimaryResourceIDs (r ).stream ()
106
110
.map (InformerEventSource ::resourceIdToString )
107
111
.toList ()));
112
+ } else {
113
+ complementaryPrimaryToSecondaryIndex = new NOOPComplementaryPrimaryToSecondaryIndex ();
108
114
}
109
115
110
116
final var informerConfig = configuration .getInformerConfig ();
@@ -123,7 +129,7 @@ public void onAdd(R newResource) {
123
129
resourceType ().getSimpleName (),
124
130
newResource .getMetadata ().getResourceVersion ());
125
131
}
126
-
132
+ complementaryPrimaryToSecondaryIndex . cleanupForResource ( newResource );
127
133
onAddOrUpdate (
128
134
Operation .ADD , newResource , null , () -> InformerEventSource .super .onAdd (newResource ));
129
135
}
@@ -250,28 +256,33 @@ private void propagateEvent(R object) {
250
256
public Set <R > getSecondaryResources (P primary ) {
251
257
252
258
if (useSecondaryToPrimaryIndex ()) {
253
-
254
- var resources =
255
- byIndex (
256
- PRIMARY_TO_SECONDARY_INDEX_NAME ,
257
- resourceIdToString (ResourceID .fromResource (primary )));
259
+ var primaryID = ResourceID .fromResource (primary );
260
+ var resources = byIndex (PRIMARY_TO_SECONDARY_INDEX_NAME , resourceIdToString (primaryID ));
258
261
259
262
log .debug (
260
263
"Using informer primary to secondary index to find secondary resources for primary name:"
261
264
+ " {} namespace: {}. Found {}" ,
262
265
primary .getMetadata ().getName (),
263
266
primary .getMetadata ().getNamespace (),
264
267
resources .size ());
265
-
266
- return resources .stream ()
267
- .map (
268
- r -> {
269
- Optional <R > resource =
270
- temporaryResourceCache .getResourceFromCache (ResourceID .fromResource (r ));
271
- return resource .orElse (r );
272
- })
273
- .collect (Collectors .toSet ());
274
-
268
+ var complementaryIds =
269
+ complementaryPrimaryToSecondaryIndex .getComplementarySecondaryResources (primaryID );
270
+ var res =
271
+ resources .stream ()
272
+ .map (
273
+ r -> {
274
+ var resourceId = ResourceID .fromResource (r );
275
+ Optional <R > resource = temporaryResourceCache .getResourceFromCache (resourceId );
276
+ complementaryIds .remove (resourceId );
277
+ return resource .orElse (r );
278
+ })
279
+ .collect (Collectors .toSet ());
280
+ complementaryIds .forEach (
281
+ id -> {
282
+ Optional <R > resource = temporaryResourceCache .getResourceFromCache (id );
283
+ resource .ifPresent (res ::add );
284
+ });
285
+ return res ;
275
286
} else {
276
287
Set <ResourceID > secondaryIDs = primaryToSecondaryMapper .toSecondaryResourceIDs (primary );
277
288
log .debug (
@@ -298,8 +309,9 @@ public synchronized void handleRecentResourceCreate(ResourceID resourceID, R res
298
309
}
299
310
300
311
private void handleRecentCreateOrUpdate (Operation operation , R newResource , R oldResource ) {
301
- // todo
302
- // primaryToSecondaryIndex.onAddOrUpdate(newResource);
312
+ if (operation == Operation .ADD ) {
313
+ complementaryPrimaryToSecondaryIndex .explicitAdd (newResource );
314
+ }
303
315
temporaryResourceCache .putResource (
304
316
newResource ,
305
317
Optional .ofNullable (oldResource )
0 commit comments