File tree Expand file tree Collapse file tree 2 files changed +24
-11
lines changed
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source
sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample Expand file tree Collapse file tree 2 files changed +24
-11
lines changed Original file line number Diff line number Diff line change 1
1
package io .javaoperatorsdk .operator .processing .event .source ;
2
2
3
3
import java .util .Collections ;
4
+ import java .util .ArrayList ;
5
+ import java .util .List ;
4
6
import java .util .Map ;
5
7
import java .util .Objects ;
6
8
import java .util .Optional ;
7
9
import java .util .concurrent .ConcurrentHashMap ;
10
+ import java .util .function .Predicate ;
11
+ import java .util .stream .Collectors ;
12
+ import java .util .stream .Stream ;
8
13
9
14
import io .fabric8 .kubernetes .api .model .HasMetadata ;
10
15
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
@@ -178,6 +183,20 @@ public Optional<T> getCustomResource(ResourceID resourceID) {
178
183
}
179
184
}
180
185
186
+ public Stream <T > getCachedCustomResources () {
187
+ return getCachedCustomResources (a -> true );
188
+ }
189
+
190
+ public Stream <T > getCachedCustomResources (Predicate <T > predicate ) {
191
+ var streams = sharedIndexInformers .values ().stream ()
192
+ .map (i -> i .getStore ().list ().stream ().filter (predicate ));
193
+ var lists = streams .map (s -> s .collect (Collectors .toList ())).collect (Collectors .toList ());
194
+ var size = lists .stream ().mapToInt (List ::size ).sum ();
195
+ List <T > list = new ArrayList <>(size );
196
+ lists .forEach (list ::addAll );
197
+ return list .stream ();
198
+ }
199
+
181
200
/**
182
201
* @return shared informers by namespace. If custom resource is not namespace scoped use
183
202
* CustomResourceEventSource.ANY_NAMESPACE_MAP_KEY
Original file line number Diff line number Diff line change @@ -44,17 +44,11 @@ public void prepareEventSources(EventSourceRegistry<Webapp> eventSourceRegistry)
44
44
// we need to find which WebApp this Tomcat custom resource is related to.
45
45
// To find the related customResourceId of the WebApp resource we traverse the cache to
46
46
// and identify it based on naming convention.
47
- var webAppInformer =
48
- eventSourceRegistry .getControllerResourceEventSource ()
49
- .getInformer (ControllerResourceEventSource .ANY_NAMESPACE_MAP_KEY );
50
-
51
- var ids = webAppInformer .getStore ().list ().stream ()
52
- .filter (
53
- (Webapp webApp ) -> webApp .getSpec ().getTomcat ().equals (t .getMetadata ().getName ()))
54
- .map (webapp -> new ResourceID (webapp .getMetadata ().getName (),
55
- webapp .getMetadata ().getNamespace ()))
56
- .collect (Collectors .toSet ());
57
- return ids ;
47
+ return eventSourceRegistry .getControllerResourceEventSource ()
48
+ .getCachedCustomResources (
49
+ (Webapp webApp ) -> webApp .getSpec ().getTomcat ()
50
+ .equals (t .getMetadata ().getName ()))
51
+ .map (ResourceID ::fromResource ).collect (Collectors .toSet ());
58
52
});
59
53
eventSourceRegistry .registerEventSource (tomcatEventSource );
60
54
}
You can’t perform that action at this time.
0 commit comments