Skip to content

Commit 5089ebf

Browse files
csvirimetacosm
authored andcommitted
feature: simplified cached access
1 parent a681a1b commit 5089ebf

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/ControllerResourceEventSource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package io.javaoperatorsdk.operator.processing.event.source;
22

33
import java.util.Collections;
4+
import java.util.ArrayList;
5+
import java.util.List;
46
import java.util.Map;
57
import java.util.Objects;
68
import java.util.Optional;
79
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.function.Predicate;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
813

914
import io.fabric8.kubernetes.api.model.HasMetadata;
1015
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
@@ -178,6 +183,20 @@ public Optional<T> getCustomResource(ResourceID resourceID) {
178183
}
179184
}
180185

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+
181200
/**
182201
* @return shared informers by namespace. If custom resource is not namespace scoped use
183202
* CustomResourceEventSource.ANY_NAMESPACE_MAP_KEY

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/WebappReconciler.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@ public void prepareEventSources(EventSourceRegistry<Webapp> eventSourceRegistry)
4444
// we need to find which WebApp this Tomcat custom resource is related to.
4545
// To find the related customResourceId of the WebApp resource we traverse the cache to
4646
// 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());
5852
});
5953
eventSourceRegistry.registerEventSource(tomcatEventSource);
6054
}

0 commit comments

Comments
 (0)