Skip to content

Commit 3b29d2f

Browse files
authored
feat: Make primary resource accessible from Context (#2782)
Signed-off-by: David Sondermann <[email protected]>
1 parent c5441fb commit 3b29d2f

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/Context.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ default <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
4646
/** ExecutorService initialized by framework for workflows. Used for workflow standalone mode. */
4747
ExecutorService getWorkflowExecutorService();
4848

49+
/**
50+
* Retrieves the primary resource.
51+
*
52+
* @return the primary resource associated with the current reconciliation
53+
*/
54+
P getPrimaryResource();
55+
4956
/**
5057
* Retrieves the primary resource cache.
5158
*

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/DefaultContext.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ public <T> Set<T> getSecondaryResources(Class<T> expectedType) {
4444
return getSecondaryResourcesAsStream(expectedType).collect(Collectors.toSet());
4545
}
4646

47-
@Override
48-
public IndexedResourceCache<P> getPrimaryCache() {
49-
return controller.getEventSourceManager().getControllerEventSource();
50-
}
51-
52-
@Override
53-
public boolean isNextReconciliationImminent() {
54-
return controller
55-
.getEventProcessor()
56-
.isNextReconciliationImminent(ResourceID.fromResource(primaryResource));
57-
}
58-
5947
@Override
6048
public <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
6149
return controller.getEventSourceManager().getEventSourcesFor(expectedType).stream()
@@ -114,12 +102,25 @@ public ExecutorService getWorkflowExecutorService() {
114102
return controller.getExecutorServiceManager().workflowExecutorService();
115103
}
116104

105+
@Override
106+
public P getPrimaryResource() {
107+
return primaryResource;
108+
}
109+
110+
@Override
111+
public IndexedResourceCache<P> getPrimaryCache() {
112+
return controller.getEventSourceManager().getControllerEventSource();
113+
}
114+
115+
@Override
116+
public boolean isNextReconciliationImminent() {
117+
return controller
118+
.getEventProcessor()
119+
.isNextReconciliationImminent(ResourceID.fromResource(primaryResource));
120+
}
121+
117122
public DefaultContext<P> setRetryInfo(RetryInfo retryInfo) {
118123
this.retryInfo = retryInfo;
119124
return this;
120125
}
121-
122-
public P getPrimaryResource() {
123-
return primaryResource;
124-
}
125126
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ private PostExecutionControl<P> reconcileExecution(
180180
return createPostExecutionControl(updatedCustomResource, updateControl);
181181
}
182182

183-
@SuppressWarnings("unchecked")
184183
private PostExecutionControl<P> handleErrorStatusHandler(
185184
P resource, P originalResource, Context<P> context, Exception e) throws Exception {
186-
187185
RetryInfo retryInfo =
188186
context
189187
.getRetryInfo()

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/DefaultContextTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
import io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException;
1010

1111
import static org.assertj.core.api.Assertions.assertThat;
12-
import static org.junit.jupiter.api.Assertions.*;
1312
import static org.mockito.ArgumentMatchers.any;
1413
import static org.mockito.Mockito.mock;
1514
import static org.mockito.Mockito.when;
1615

1716
class DefaultContextTest {
1817

19-
Secret primary = new Secret();
20-
Controller<Secret> mockController = mock(Controller.class);
18+
private final Secret primary = new Secret();
19+
private final Controller<Secret> mockController = mock();
2120

22-
DefaultContext<?> context = new DefaultContext<>(null, mockController, primary);
21+
private final DefaultContext<?> context = new DefaultContext<>(null, mockController, primary);
2322

2423
@Test
24+
@SuppressWarnings("unchecked")
2525
void getSecondaryResourceReturnsEmptyOptionalOnNonActivatedDRType() {
2626
var mockManager = mock(EventSourceManager.class);
2727
when(mockController.getEventSourceManager()).thenReturn(mockManager);
@@ -30,7 +30,14 @@ void getSecondaryResourceReturnsEmptyOptionalOnNonActivatedDRType() {
3030
.thenThrow(new NoEventSourceForClassException(ConfigMap.class));
3131

3232
var res = context.getSecondaryResource(ConfigMap.class);
33-
3433
assertThat(res).isEmpty();
3534
}
35+
36+
@Test
37+
void setRetryInfo() {
38+
RetryInfo retryInfo = mock();
39+
var newContext = context.setRetryInfo(retryInfo);
40+
assertThat(newContext).isSameAs(context);
41+
assertThat(newContext.getRetryInfo()).hasValue(retryInfo);
42+
}
3643
}

0 commit comments

Comments
 (0)