Skip to content

Commit c29c650

Browse files
committed
fix: DefaultEventHandler should get its cache from the CR event source
1 parent b569ffd commit c29c650

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public class DefaultEventHandler implements EventHandler {
3434

3535
private static final Logger log = LoggerFactory.getLogger(DefaultEventHandler.class);
3636

37-
private final CustomResourceCache customResourceCache;
3837
private final EventBuffer eventBuffer;
3938
private final Set<String> underProcessing = new HashSet<>();
4039
private final ScheduledThreadPoolExecutor executor;
@@ -49,20 +48,17 @@ public class DefaultEventHandler implements EventHandler {
4948
public DefaultEventHandler(
5049
ResourceController controller, ControllerConfiguration configuration, MixedOperation client) {
5150
this(
52-
new CustomResourceCache(configuration.getConfigurationService().getObjectMapper()),
5351
new EventDispatcher(controller, configuration.getFinalizer(), client),
5452
configuration.getName(),
5553
GenericRetry.fromConfiguration(configuration.getRetryConfiguration()),
5654
configuration.getConfigurationService().concurrentReconciliationThreads());
5755
}
5856

5957
DefaultEventHandler(
60-
CustomResourceCache customResourceCache,
6158
EventDispatcher eventDispatcher,
6259
String relatedControllerName,
6360
Retry retry,
6461
int concurrentReconciliationThreads) {
65-
this.customResourceCache = customResourceCache;
6662
this.eventDispatcher = eventDispatcher;
6763
this.retry = retry;
6864
this.controllerName = relatedControllerName;
@@ -103,7 +99,7 @@ private void executeBufferedEvents(String customResourceUid) {
10399
boolean newEventForResourceId = eventBuffer.containsEvents(customResourceUid);
104100
boolean controllerUnderExecution = isControllerUnderExecution(customResourceUid);
105101
Optional<CustomResource> latestCustomResource =
106-
customResourceCache.getLatestResource(customResourceUid);
102+
eventSourceManager.getLatestResource(customResourceUid);
107103

108104
if (!controllerUnderExecution && newEventForResourceId && latestCustomResource.isPresent()) {
109105
setUnderExecutionProcessing(customResourceUid);
@@ -235,7 +231,7 @@ private void cacheUpdatedResourceIfChanged(
235231
getUID(originalCustomResource),
236232
getVersion(customResourceAfterExecution),
237233
getVersion(originalCustomResource));
238-
this.customResourceCache.cacheResource(
234+
eventSourceManager.cacheResource(
239235
customResourceAfterExecution,
240236
customResource ->
241237
getVersion(customResource).equals(originalResourceVersion)
@@ -246,7 +242,6 @@ private void cacheUpdatedResourceIfChanged(
246242
private void cleanupAfterDeletedEvent(String customResourceUid) {
247243
eventSourceManager.cleanup(customResourceUid);
248244
eventBuffer.cleanup(customResourceUid);
249-
customResourceCache.cleanup(customResourceUid);
250245
}
251246

252247
private boolean isControllerUnderExecution(String customResourceUid) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.fabric8.kubernetes.client.dsl.Resource;
77
import io.javaoperatorsdk.operator.api.ResourceController;
88
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
9+
import io.javaoperatorsdk.operator.processing.CustomResourceCache;
910
import io.javaoperatorsdk.operator.processing.DefaultEventHandler;
1011
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEventSource;
1112
import io.javaoperatorsdk.operator.processing.event.internal.TimerEventSource;
@@ -15,6 +16,7 @@
1516
import java.util.Optional;
1617
import java.util.concurrent.ConcurrentHashMap;
1718
import java.util.concurrent.locks.ReentrantLock;
19+
import java.util.function.Predicate;
1820
import org.slf4j.Logger;
1921
import org.slf4j.LoggerFactory;
2022

@@ -134,5 +136,24 @@ public void cleanup(String customResourceUid) {
134136
.keySet()
135137
.forEach(k -> deRegisterCustomResourceFromEventSource(k, customResourceUid));
136138
eventSources.remove(customResourceUid);
139+
getCache().cleanup(customResourceUid);
140+
}
141+
142+
// todo: remove
143+
public CustomResourceCache getCache() {
144+
final var source =
145+
(CustomResourceEventSource)
146+
getRegisteredEventSources().get(CUSTOM_RESOURCE_EVENT_SOURCE_NAME);
147+
return source.getCache();
148+
}
149+
150+
// todo: remove
151+
public Optional<CustomResource> getLatestResource(String customResourceUid) {
152+
return getCache().getLatestResource(customResourceUid);
153+
}
154+
155+
// todo: remove
156+
public void cacheResource(CustomResource resource, Predicate<CustomResource> predicate) {
157+
getCache().cacheResource(resource, predicate);
137158
}
138159
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,9 @@ public void onClose(WatcherException e) {
193193
System.exit(1);
194194
}
195195
}
196+
197+
// todo: remove
198+
public CustomResourceCache getCache() {
199+
return customResourceCache;
200+
}
196201
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.assertj.core.api.Assertions.assertThat;
55
import static org.mockito.ArgumentMatchers.eq;
66
import static org.mockito.Mockito.any;
7+
import static org.mockito.Mockito.doAnswer;
8+
import static org.mockito.Mockito.doCallRealMethod;
79
import static org.mockito.Mockito.mock;
810
import static org.mockito.Mockito.never;
911
import static org.mockito.Mockito.timeout;
@@ -40,19 +42,18 @@ class DefaultEventHandlerTest {
4042
private CustomResourceCache customResourceCache = new CustomResourceCache();
4143
private DefaultEventSourceManager defaultEventSourceManagerMock =
4244
mock(DefaultEventSourceManager.class);
45+
4346
private TimerEventSource retryTimerEventSourceMock = mock(TimerEventSource.class);
4447

4548
private DefaultEventHandler defaultEventHandler =
4649
new DefaultEventHandler(
47-
customResourceCache,
4850
eventDispatcherMock,
4951
"Test",
5052
null,
5153
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER);
5254

5355
private DefaultEventHandler defaultEventHandlerWithRetry =
5456
new DefaultEventHandler(
55-
customResourceCache,
5657
eventDispatcherMock,
5758
"Test",
5859
GenericRetry.defaultLimitedExponentialRetry(),
@@ -64,6 +65,19 @@ public void setup() {
6465
.thenReturn(retryTimerEventSourceMock);
6566
defaultEventHandler.setEventSourceManager(defaultEventSourceManagerMock);
6667
defaultEventHandlerWithRetry.setEventSourceManager(defaultEventSourceManagerMock);
68+
69+
// todo: remove
70+
when(defaultEventSourceManagerMock.getCache()).thenReturn(customResourceCache);
71+
doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResource(any());
72+
doCallRealMethod().when(defaultEventSourceManagerMock).cacheResource(any(), any());
73+
doAnswer(
74+
invocation -> {
75+
final var resourceId = (String) invocation.getArgument(0);
76+
customResourceCache.cleanup(resourceId);
77+
return null;
78+
})
79+
.when(defaultEventSourceManagerMock)
80+
.cleanup(any());
6781
}
6882

6983
@Test

0 commit comments

Comments
 (0)