Skip to content

Make primary resource accessible from Context #2782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ default <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
/** ExecutorService initialized by framework for workflows. Used for workflow standalone mode. */
ExecutorService getWorkflowExecutorService();

/**
* Retrieves the primary resource.
*
* @return the primary resource associated with the current reconciliation
*/
P getPrimaryResource();

/**
* Retrieves the primary resource cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ public <T> Set<T> getSecondaryResources(Class<T> expectedType) {
return getSecondaryResourcesAsStream(expectedType).collect(Collectors.toSet());
}

@Override
public IndexedResourceCache<P> getPrimaryCache() {
return controller.getEventSourceManager().getControllerEventSource();
}

@Override
public boolean isNextReconciliationImminent() {
return controller
.getEventProcessor()
.isNextReconciliationImminent(ResourceID.fromResource(primaryResource));
}

@Override
public <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
return controller.getEventSourceManager().getEventSourcesFor(expectedType).stream()
Expand Down Expand Up @@ -114,12 +102,25 @@ public ExecutorService getWorkflowExecutorService() {
return controller.getExecutorServiceManager().workflowExecutorService();
}

@Override
public P getPrimaryResource() {
return primaryResource;
}

@Override
public IndexedResourceCache<P> getPrimaryCache() {
return controller.getEventSourceManager().getControllerEventSource();
}

@Override
public boolean isNextReconciliationImminent() {
return controller
.getEventProcessor()
.isNextReconciliationImminent(ResourceID.fromResource(primaryResource));
}

public DefaultContext<P> setRetryInfo(RetryInfo retryInfo) {
this.retryInfo = retryInfo;
return this;
}

public P getPrimaryResource() {
return primaryResource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ private PostExecutionControl<P> reconcileExecution(
return createPostExecutionControl(updatedCustomResource, updateControl);
}

@SuppressWarnings("unchecked")
private PostExecutionControl<P> handleErrorStatusHandler(
P resource, P originalResource, Context<P> context, Exception e) throws Exception {

RetryInfo retryInfo =
context
.getRetryInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
import io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class DefaultContextTest {

Secret primary = new Secret();
Controller<Secret> mockController = mock(Controller.class);
private final Secret primary = new Secret();
private final Controller<Secret> mockController = mock();

DefaultContext<?> context = new DefaultContext<>(null, mockController, primary);
private final DefaultContext<?> context = new DefaultContext<>(null, mockController, primary);

@Test
@SuppressWarnings("unchecked")
void getSecondaryResourceReturnsEmptyOptionalOnNonActivatedDRType() {
var mockManager = mock(EventSourceManager.class);
when(mockController.getEventSourceManager()).thenReturn(mockManager);
Expand All @@ -30,7 +30,14 @@ void getSecondaryResourceReturnsEmptyOptionalOnNonActivatedDRType() {
.thenThrow(new NoEventSourceForClassException(ConfigMap.class));

var res = context.getSecondaryResource(ConfigMap.class);

assertThat(res).isEmpty();
}

@Test
void setRetryInfo() {
RetryInfo retryInfo = mock();
var newContext = context.setRetryInfo(retryInfo);
assertThat(newContext).isSameAs(context);
assertThat(newContext.getRetryInfo()).hasValue(retryInfo);
}
}