Skip to content

Commit ac1a026

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 7808fb6 commit ac1a026

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public class Controller<P extends HasMetadata>
6161
private static final String RESOURCE = "resource";
6262
private static final String STATUS = "status";
6363
private static final String BOTH = "both";
64+
public static final String CLEANER_NOT_SUPPORTED_ON_ALL_EVENT_ERROR_MESSAGE =
65+
"Cleaner is not supported when triggerReconcilerOnAllEvent enabled.";
66+
public static final String
67+
MANAGED_WORKFLOWS_NOT_SUPPORTED_TRIGGER_RECONCILER_ON_ALL_EVENT_ERROR_MESSAGE =
68+
"Managed workflows are not supported when triggerReconcilerOnAllEvent enabled.";
6469

6570
private final Reconciler<P> reconciler;
6671
private final ControllerConfiguration<P> configuration;
@@ -97,6 +102,16 @@ public Controller(
97102
explicitWorkflowInvocation =
98103
configuration.getWorkflowSpec().map(WorkflowSpec::isExplicitInvocation).orElse(false);
99104

105+
if (configuration.triggerReconcilerOnAllEvent()) {
106+
if (isCleaner) {
107+
throw new OperatorException(CLEANER_NOT_SUPPORTED_ON_ALL_EVENT_ERROR_MESSAGE);
108+
}
109+
if (managedWorkflow != null && !managedWorkflow.isEmpty()) {
110+
throw new OperatorException(
111+
MANAGED_WORKFLOWS_NOT_SUPPORTED_TRIGGER_RECONCILER_ON_ALL_EVENT_ERROR_MESSAGE);
112+
}
113+
}
114+
100115
eventSourceManager = new EventSourceManager<>(this);
101116
eventProcessor = new EventProcessor<>(eventSourceManager, configurationService);
102117
eventSourceManager.postProcessDefaultEventSourcesAfterProcessorInitializer();

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import io.fabric8.kubernetes.api.model.Secret;
1010
import io.javaoperatorsdk.operator.MockKubernetesClient;
11+
import io.javaoperatorsdk.operator.OperatorException;
1112
import io.javaoperatorsdk.operator.api.config.BaseConfigurationService;
1213
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1314
import io.javaoperatorsdk.operator.api.config.MockControllerConfiguration;
@@ -23,7 +24,10 @@
2324
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
2425

2526
import static io.javaoperatorsdk.operator.api.monitoring.Metrics.NOOP;
27+
import static io.javaoperatorsdk.operator.processing.Controller.CLEANER_NOT_SUPPORTED_ON_ALL_EVENT_ERROR_MESSAGE;
28+
import static io.javaoperatorsdk.operator.processing.Controller.MANAGED_WORKFLOWS_NOT_SUPPORTED_TRIGGER_RECONCILER_ON_ALL_EVENT_ERROR_MESSAGE;
2629
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
2731
import static org.mockito.ArgumentMatchers.any;
2832
import static org.mockito.Mockito.mock;
2933
import static org.mockito.Mockito.never;
@@ -76,6 +80,54 @@ void usesFinalizerIfThereIfReconcilerImplementsCleaner() {
7680
assertThat(controller.useFinalizer()).isTrue();
7781
}
7882

83+
@Test
84+
void cleanerNotAllowedWithTriggerOnAllEventEnabled() {
85+
Reconciler reconciler = mock(Reconciler.class, withSettings().extraInterfaces(Cleaner.class));
86+
final var configuration = MockControllerConfiguration.forResource(Secret.class);
87+
when(configuration.getConfigurationService()).thenReturn(new BaseConfigurationService());
88+
when(configuration.triggerReconcilerOnAllEvent()).thenReturn(true);
89+
90+
var exception =
91+
assertThrows(
92+
OperatorException.class,
93+
() ->
94+
new Controller<Secret>(
95+
reconciler, configuration, MockKubernetesClient.client(Secret.class)));
96+
97+
assertThat(exception.getMessage()).isEqualTo(CLEANER_NOT_SUPPORTED_ON_ALL_EVENT_ERROR_MESSAGE);
98+
}
99+
100+
@Test
101+
void managedWorkflowNotAllowedWithOnAllEventEnabled() {
102+
Reconciler reconciler = mock(Reconciler.class);
103+
final var configuration = MockControllerConfiguration.forResource(Secret.class);
104+
105+
var configurationService = mock(ConfigurationService.class);
106+
var mockWorkflowFactory = mock(ManagedWorkflowFactory.class);
107+
var mockManagedWorkflow = mock(ManagedWorkflow.class);
108+
109+
when(configuration.getConfigurationService()).thenReturn(configurationService);
110+
var workflowSpec = mock(WorkflowSpec.class);
111+
when(configuration.getWorkflowSpec()).thenReturn(Optional.of(workflowSpec));
112+
when(configurationService.getMetrics()).thenReturn(NOOP);
113+
when(configurationService.getWorkflowFactory()).thenReturn(mockWorkflowFactory);
114+
when(mockWorkflowFactory.workflowFor(any())).thenReturn(mockManagedWorkflow);
115+
var managedWorkflowMock = workflow(true);
116+
when(mockManagedWorkflow.resolve(any(), any())).thenReturn(managedWorkflowMock);
117+
118+
when(configuration.triggerReconcilerOnAllEvent()).thenReturn(true);
119+
120+
var exception =
121+
assertThrows(
122+
OperatorException.class,
123+
() ->
124+
new Controller<Secret>(
125+
reconciler, configuration, MockKubernetesClient.client(Secret.class)));
126+
127+
assertThat(exception.getMessage())
128+
.isEqualTo(MANAGED_WORKFLOWS_NOT_SUPPORTED_TRIGGER_RECONCILER_ON_ALL_EVENT_ERROR_MESSAGE);
129+
}
130+
79131
@ParameterizedTest
80132
@CsvSource({
81133
"true, true, true, false",
@@ -132,6 +184,7 @@ private Workflow workflow(boolean hasCleaner) {
132184
var workflow = mock(Workflow.class);
133185
when(workflow.cleanup(any(), any())).thenReturn(mock(WorkflowCleanupResult.class));
134186
when(workflow.hasCleaner()).thenReturn(hasCleaner);
187+
when(workflow.isEmpty()).thenReturn(false);
135188
return workflow;
136189
}
137190
}

0 commit comments

Comments
 (0)