Skip to content

Commit 7aa9bf3

Browse files
laxmikantbpandharemetacosm
authored andcommitted
feature: modified for addition of config in defaulteventhandler
1 parent 5183c84 commit 7aa9bf3

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class DefaultEventHandler implements EventHandler {
4848
private final int terminationTimeout;
4949
private final ReentrantLock lock = new ReentrantLock();
5050
private DefaultEventSourceManager eventSourceManager;
51-
private final Metrics metrics = new Metrics(new Metrics.NoopMeterRegistry(Clock.SYSTEM));
51+
private ControllerConfiguration configuration;
5252

5353
public DefaultEventHandler(
5454
ResourceController controller, ControllerConfiguration configuration, MixedOperation client) {
@@ -57,28 +57,28 @@ public DefaultEventHandler(
5757
configuration.getName(),
5858
GenericRetry.fromConfiguration(configuration.getRetryConfiguration()),
5959
configuration.getConfigurationService().concurrentReconciliationThreads(),
60-
configuration.getConfigurationService().getTerminationTimeoutSeconds());
60+
configuration.getConfigurationService().getTerminationTimeoutSeconds(), configuration);
6161
}
6262

6363
DefaultEventHandler(
6464
EventDispatcher eventDispatcher,
6565
String relatedControllerName,
6666
Retry retry,
67-
int concurrentReconciliationThreads) {
67+
int concurrentReconciliationThreads, ControllerConfiguration configuration) {
6868
this(
6969
eventDispatcher,
7070
relatedControllerName,
7171
retry,
7272
concurrentReconciliationThreads,
73-
ConfigurationService.DEFAULT_TERMINATION_TIMEOUT_SECONDS);
73+
ConfigurationService.DEFAULT_TERMINATION_TIMEOUT_SECONDS, configuration);
7474
}
7575

7676
private DefaultEventHandler(
7777
EventDispatcher eventDispatcher,
7878
String relatedControllerName,
7979
Retry retry,
8080
int concurrentReconciliationThreads,
81-
int terminationTimeout) {
81+
int terminationTimeout, ControllerConfiguration configuration) {
8282
this.eventDispatcher = eventDispatcher;
8383
this.retry = retry;
8484
this.controllerName = relatedControllerName;
@@ -88,6 +88,7 @@ private DefaultEventHandler(
8888
new ScheduledThreadPoolExecutor(
8989
concurrentReconciliationThreads,
9090
runnable -> new Thread(runnable, "EventHandler-" + relatedControllerName));
91+
this.configuration = configuration;
9192
}
9293

9394
@Override
@@ -116,7 +117,10 @@ public void handleEvent(Event event) {
116117
final Predicate<CustomResource> selector = event.getCustomResourcesSelector();
117118
for (String uid : eventSourceManager.getLatestResourceUids(selector)) {
118119
eventBuffer.addEvent(uid, event);
119-
metrics.timeControllerEvents();
120+
configuration
121+
.getConfigurationService()
122+
.getMetrics()
123+
.timeControllerEvents();
120124
executeBufferedEvents(uid);
121125
}
122126
} finally {
@@ -166,7 +170,10 @@ void eventProcessingFinished(
166170

167171
if (retry != null && postExecutionControl.exceptionDuringExecution()) {
168172
handleRetryOnException(executionScope);
169-
metrics.timeControllerRetry();
173+
configuration
174+
.getConfigurationService()
175+
.getMetrics()
176+
.timeControllerRetry();
170177
return;
171178
}
172179

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
import static org.mockito.Mockito.when;
1212

1313
import io.fabric8.kubernetes.client.Watcher;
14+
import io.javaoperatorsdk.operator.Metrics;
1415
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
16+
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1517
import io.javaoperatorsdk.operator.processing.event.DefaultEvent;
1618
import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager;
1719
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent;
20+
import io.javaoperatorsdk.operator.processing.event.internal.TimerEventSource;
1821
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
1922
import java.util.Objects;
2023
import java.util.UUID;
@@ -33,12 +36,17 @@ class CustomResourceSelectorTest {
3336
private final DefaultEventSourceManager defaultEventSourceManagerMock =
3437
mock(DefaultEventSourceManager.class);
3538

39+
private TimerEventSource retryTimerEventSourceMock = mock(TimerEventSource.class);
40+
private ControllerConfiguration configuration =
41+
mock(ControllerConfiguration.class);
42+
private final ConfigurationService configService = mock(ConfigurationService.class);
43+
3644
private final DefaultEventHandler defaultEventHandler =
3745
new DefaultEventHandler(
3846
eventDispatcherMock,
3947
"Test",
4048
null,
41-
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER);
49+
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER, configuration);
4250

4351
@BeforeEach
4452
public void setup() {
@@ -58,6 +66,10 @@ public void setup() {
5866
})
5967
.when(defaultEventSourceManagerMock)
6068
.cleanup(any());
69+
70+
when(configuration.getName()).thenReturn("DefaultEventHandlerTest");
71+
when(configService.getMetrics()).thenReturn(Metrics.NOOP);
72+
when(configuration.getConfigurationService()).thenReturn(configService);
6173
}
6274

6375
@Test

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
import static org.mockito.Mockito.verify;
1414
import static org.mockito.Mockito.when;
1515

16+
import io.fabric8.kubernetes.client.CustomResource;
1617
import io.fabric8.kubernetes.client.Watcher;
18+
import io.javaoperatorsdk.operator.Metrics;
1719
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
20+
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1821
import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager;
1922
import io.javaoperatorsdk.operator.processing.event.Event;
2023
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent;
@@ -44,20 +47,25 @@ class DefaultEventHandlerTest {
4447
mock(DefaultEventSourceManager.class);
4548

4649
private TimerEventSource retryTimerEventSourceMock = mock(TimerEventSource.class);
50+
private ControllerConfiguration configuration =
51+
mock(ControllerConfiguration.class);
52+
private final ConfigurationService configService = mock(ConfigurationService.class);
4753

4854
private DefaultEventHandler defaultEventHandler =
4955
new DefaultEventHandler(
5056
eventDispatcherMock,
5157
"Test",
5258
null,
53-
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER);
59+
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER,
60+
configuration);
5461

5562
private DefaultEventHandler defaultEventHandlerWithRetry =
5663
new DefaultEventHandler(
5764
eventDispatcherMock,
5865
"Test",
5966
GenericRetry.defaultLimitedExponentialRetry(),
60-
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER);
67+
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER,
68+
configuration);
6169

6270
@BeforeEach
6371
public void setup() {
@@ -66,6 +74,10 @@ public void setup() {
6674
defaultEventHandler.setEventSourceManager(defaultEventSourceManagerMock);
6775
defaultEventHandlerWithRetry.setEventSourceManager(defaultEventSourceManagerMock);
6876

77+
when(configuration.getName()).thenReturn("DefaultEventHandlerTest");
78+
when(configService.getMetrics()).thenReturn(Metrics.NOOP);
79+
when(configuration.getConfigurationService()).thenReturn(configService);
80+
6981
// todo: remove
7082
when(defaultEventSourceManagerMock.getCache()).thenReturn(customResourceCache);
7183
doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResource(any());

0 commit comments

Comments
 (0)