Skip to content

Commit b569ffd

Browse files
committed
fix: restore previous caching behavior
1 parent 732807d commit b569ffd

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/DefaultEventHandler.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager;
1313
import io.javaoperatorsdk.operator.processing.event.Event;
1414
import io.javaoperatorsdk.operator.processing.event.EventHandler;
15-
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent;
1615
import io.javaoperatorsdk.operator.processing.retry.GenericRetry;
1716
import io.javaoperatorsdk.operator.processing.retry.Retry;
1817
import io.javaoperatorsdk.operator.processing.retry.RetryExecution;
@@ -90,12 +89,6 @@ public void setEventSourceManager(DefaultEventSourceManager eventSourceManager)
9089

9190
@Override
9291
public void handleEvent(Event event) {
93-
// cache the latest version of the CR
94-
if (event instanceof CustomResourceEvent) {
95-
CustomResourceEvent crEvent = (CustomResourceEvent) event;
96-
customResourceCache.cacheResource(crEvent.getCustomResource());
97-
}
98-
9992
try {
10093
lock.lock();
10194
log.debug("Received event: {}", event);

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/internal/CustomResourceEventSource.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.fabric8.kubernetes.client.dsl.Resource;
1313
import io.fabric8.kubernetes.client.dsl.internal.CustomResourceOperationsImpl;
1414
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
15+
import io.javaoperatorsdk.operator.processing.CustomResourceCache;
1516
import io.javaoperatorsdk.operator.processing.KubernetesResourceUtils;
1617
import io.javaoperatorsdk.operator.processing.event.AbstractEventSource;
1718
import java.util.ArrayList;
@@ -35,6 +36,7 @@ public class CustomResourceEventSource<T extends CustomResource<?, ?>> extends A
3536
private final Map<String, Long> lastGenerationProcessedSuccessfully = new ConcurrentHashMap<>();
3637
private final List<Watch> watches;
3738
private final String resClass;
39+
private final CustomResourceCache customResourceCache;
3840

3941
public CustomResourceEventSource(
4042
MixedOperation<T, KubernetesResourceList<T>, Resource<T>> client,
@@ -44,7 +46,8 @@ public CustomResourceEventSource(
4446
configuration.getEffectiveNamespaces(),
4547
configuration.isGenerationAware(),
4648
configuration.getFinalizer(),
47-
configuration.getCustomResourceClass());
49+
configuration.getCustomResourceClass(),
50+
new CustomResourceCache(configuration.getConfigurationService().getObjectMapper()));
4851
}
4952

5053
CustomResourceEventSource(
@@ -53,12 +56,29 @@ public CustomResourceEventSource(
5356
boolean generationAware,
5457
String resourceFinalizer,
5558
Class<T> resClass) {
59+
this(
60+
client,
61+
targetNamespaces,
62+
generationAware,
63+
resourceFinalizer,
64+
resClass,
65+
new CustomResourceCache());
66+
}
67+
68+
CustomResourceEventSource(
69+
MixedOperation<T, KubernetesResourceList<T>, Resource<T>> client,
70+
Set<String> targetNamespaces,
71+
boolean generationAware,
72+
String resourceFinalizer,
73+
Class<T> resClass,
74+
CustomResourceCache customResourceCache) {
5675
this.client = (CustomResourceOperationsImpl<T, KubernetesResourceList<T>>) client;
5776
this.targetNamespaces = targetNamespaces;
5877
this.generationAware = generationAware;
5978
this.resourceFinalizer = resourceFinalizer;
6079
this.watches = new ArrayList<>();
6180
this.resClass = resClass.getName();
81+
this.customResourceCache = customResourceCache;
6282
}
6383

6484
@Override
@@ -97,6 +117,9 @@ public void eventReceived(Watcher.Action action, T customResource) {
97117
action.name(),
98118
customResource.getMetadata().getName());
99119

120+
// cache the latest version of the CR
121+
customResourceCache.cacheResource(customResource);
122+
100123
if (action == Action.ERROR) {
101124
log.debug(
102125
"Skipping {} event for custom resource uid: {}, version: {}",

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/DefaultEventHandlerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ public void dispatchesEventsIfNoExecutionInProgress() {
7373
verify(eventDispatcherMock, timeout(50).times(1)).handleExecution(any());
7474
}
7575

76-
/*@Test
76+
@Test
7777
public void skipProcessingIfLatestCustomResourceNotInCache() {
7878
Event event = prepareCREvent();
7979
customResourceCache.cleanup(event.getRelatedCustomResourceUid());
8080

8181
defaultEventHandler.handleEvent(event);
8282

8383
verify(eventDispatcherMock, timeout(50).times(0)).handleExecution(any());
84-
}*/
84+
}
8585

8686
@Test
8787
public void ifExecutionInProgressWaitsUntilItsFinished() throws InterruptedException {

0 commit comments

Comments
 (0)