Skip to content

Commit 3f22f3e

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent dd0e326 commit 3f22f3e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ private boolean isWatchingAllNamespaces() {
230230
return sources.containsKey(WATCH_ALL_NAMESPACES);
231231
}
232232

233+
public boolean isWatchingNamespace(String namespace) {
234+
// for cluster scoped resources we can assume
235+
// that we watch the whole cluster
236+
if (namespace == null) {
237+
return true;
238+
}
239+
if (isWatchingAllNamespaces()) {
240+
return true;
241+
}
242+
return sources.containsKey(namespace);
243+
}
244+
233245
private Optional<InformerWrapper<R>> getSource(String namespace) {
234246
namespace = isWatchingAllNamespaces() || namespace == null ? WATCH_ALL_NAMESPACES : namespace;
235247
return Optional.ofNullable(sources.get(namespace));

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ public synchronized void putResource(T newResource) {
188188
return;
189189
}
190190

191+
// todo unit test
192+
// this can happen when we dynamically change the NS
193+
if (!managedInformerEventSource
194+
.manager()
195+
.isWatchingNamespace(newResource.getMetadata().getNamespace())) {
196+
return;
197+
}
198+
191199
// check against the latestResourceVersion processed by the TemporaryResourceCache
192200
// If the resource is older, then we can safely ignore.
193201
//
@@ -223,6 +231,7 @@ private String getLastSyncResourceVersion(String namespace) {
223231
return managedInformerEventSource.manager().lastSyncResourceVersion(namespace);
224232
}
225233

234+
// todo tests with combination of event processing
226235
/**
227236
* There are (probably extremely rare) circumstances, when we can miss a delete event related to a
228237
* resources: when we create a resource that is deleted right after by third party and the related
@@ -235,9 +244,21 @@ private void checkGhostResources() {
235244
var iterator = cache.entrySet().iterator();
236245
while (iterator.hasNext()) {
237246
var e = iterator.next();
247+
248+
var ns = e.getValue().getMetadata().getNamespace();
249+
// todo unit tests
250+
// this can happen if followed namespaces are changed dynamically
251+
if (!managedInformerEventSource.manager().isWatchingNamespace(ns)) {
252+
log.debug(
253+
"Removing resource: {} from cache as part of ghost cleanup. Namespace is not followed"
254+
+ " anymore: {}",
255+
e.getKey(),
256+
ns);
257+
iterator.remove();
258+
continue;
259+
}
238260
if ((ReconcilerUtilsInternal.compareResourceVersions(
239-
e.getValue().getMetadata().getResourceVersion(),
240-
getLastSyncResourceVersion(e.getValue().getMetadata().getNamespace()))
261+
e.getValue().getMetadata().getResourceVersion(), getLastSyncResourceVersion(ns))
241262
< 0)
242263
// making sure we have the situation where resource is missing from the cache
243264
&& managedInformerEventSource

0 commit comments

Comments
 (0)