Skip to content

Commit

Permalink
fix: managed workflow result is available even when exception is thrown
Browse files Browse the repository at this point in the history
Fixes #2551

Signed-off-by: Chris Laprun <[email protected]>
  • Loading branch information
metacosm committed Oct 14, 2024
1 parent 41463e1 commit bb1776e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext;
import io.javaoperatorsdk.operator.health.ControllerHealthInfo;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Workflow;
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowBuilder;
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowCleanupResult;
import io.javaoperatorsdk.operator.processing.event.EventProcessor;
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
Expand Down Expand Up @@ -93,7 +94,11 @@ public Controller(Reconciler<P> reconciler,
isCleaner = reconciler instanceof Cleaner;

final var managed = configurationService.getWorkflowFactory().workflowFor(configuration);
managedWorkflow = managed.resolve(kubernetesClient, configuration);
// wrap original wrapper so that we can properly set the result in case it's throwing an
// exception
final Workflow workflow = managed.resolve(kubernetesClient, configuration);
managedWorkflow = WorkflowBuilder.from(workflow)
.withThrowExceptionFurther(false).build();

eventSourceManager = new EventSourceManager<>(this);
eventProcessor = new EventProcessor<>(eventSourceManager, configurationService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
Expand All @@ -14,11 +15,21 @@
@SuppressWarnings({"rawtypes", "unchecked"})
public class WorkflowBuilder<P extends HasMetadata> {

private final Map<String, DependentResourceNode<?, P>> dependentResourceNodes =
new HashMap<>();
private boolean throwExceptionAutomatically = THROW_EXCEPTION_AUTOMATICALLY_DEFAULT;
private final Map<String, DependentResourceNode<?, P>> dependentResourceNodes;
private boolean throwExceptionAutomatically;
private DependentResourceNode currentNode;
private boolean isCleaner = false;
private boolean isCleaner;

public WorkflowBuilder() {
this(new HashMap<>(), THROW_EXCEPTION_AUTOMATICALLY_DEFAULT, false);
}

private WorkflowBuilder(Map<String, DependentResourceNode<?, P>> dependentResourceNodes,
boolean throwExceptionAutomatically, boolean isCleaner) {
this.dependentResourceNodes = dependentResourceNodes;
this.throwExceptionAutomatically = throwExceptionAutomatically;
this.isCleaner = isCleaner;
}

public WorkflowBuilder<P> addDependentResource(DependentResource dependentResource) {
return addDependentResource(dependentResource, null);
Expand Down Expand Up @@ -91,4 +102,12 @@ public Workflow<P> build() {
return new DefaultWorkflow(new HashSet<>(dependentResourceNodes.values()),
throwExceptionAutomatically, isCleaner);
}

public static <P extends HasMetadata> WorkflowBuilder<P> from(Workflow<P> workflow) {
final Map<String, DependentResourceNode<?, P>> nodes =
workflow.getDependentResourcesByName().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> new DependentResourceNode(e.getKey(), e.getValue())));
return new WorkflowBuilder<>(nodes, false, workflow.hasCleaner());
}
}

0 comments on commit bb1776e

Please sign in to comment.