|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent.workflow; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | +import java.util.concurrent.Executors; |
| 7 | + |
| 8 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 9 | +import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider; |
| 10 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 11 | + |
| 12 | +/** |
| 13 | + * Dependents definition: so if B depends on A, the B is dependent of A. |
| 14 | + * |
| 15 | + * @param <P> primary resource |
| 16 | + */ |
| 17 | +@SuppressWarnings("rawtypes") |
| 18 | +public class Workflow<P extends HasMetadata> { |
| 19 | + |
| 20 | + public static final boolean THROW_EXCEPTION_AUTOMATICALLY_DEFAULT = true; |
| 21 | + |
| 22 | + private final Set<DependentResourceNode> dependentResourceNodes; |
| 23 | + private final Set<DependentResourceNode> topLevelResources = new HashSet<>(); |
| 24 | + private final Set<DependentResourceNode> bottomLevelResource = new HashSet<>(); |
| 25 | + |
| 26 | + private final boolean throwExceptionAutomatically; |
| 27 | + // it's "global" executor service shared between multiple reconciliations running parallel |
| 28 | + private ExecutorService executorService; |
| 29 | + |
| 30 | + public Workflow(Set<DependentResourceNode> dependentResourceNodes) { |
| 31 | + this.executorService = ConfigurationServiceProvider.instance().getExecutorService(); |
| 32 | + this.dependentResourceNodes = dependentResourceNodes; |
| 33 | + this.throwExceptionAutomatically = THROW_EXCEPTION_AUTOMATICALLY_DEFAULT; |
| 34 | + preprocessForReconcile(); |
| 35 | + } |
| 36 | + |
| 37 | + public Workflow(Set<DependentResourceNode> dependentResourceNodes, |
| 38 | + ExecutorService executorService, boolean throwExceptionAutomatically) { |
| 39 | + this.executorService = executorService; |
| 40 | + this.dependentResourceNodes = dependentResourceNodes; |
| 41 | + this.throwExceptionAutomatically = throwExceptionAutomatically; |
| 42 | + preprocessForReconcile(); |
| 43 | + } |
| 44 | + |
| 45 | + public Workflow(Set<DependentResourceNode> dependentResourceNodes, int globalParallelism) { |
| 46 | + this(dependentResourceNodes, Executors.newFixedThreadPool(globalParallelism), true); |
| 47 | + } |
| 48 | + |
| 49 | + public WorkflowExecutionResult reconcile(P primary, Context<P> context) { |
| 50 | + WorkflowReconcileExecutor<P> workflowReconcileExecutor = |
| 51 | + new WorkflowReconcileExecutor<>(this, primary, context); |
| 52 | + var result = workflowReconcileExecutor.reconcile(); |
| 53 | + if (throwExceptionAutomatically) { |
| 54 | + result.throwAggregateExceptionIfErrorsPresent(); |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + public WorkflowCleanupResult cleanup(P primary, Context<P> context) { |
| 60 | + WorkflowCleanupExecutor<P> workflowCleanupExecutor = |
| 61 | + new WorkflowCleanupExecutor<>(this, primary, context); |
| 62 | + var result = workflowCleanupExecutor.cleanup(); |
| 63 | + if (throwExceptionAutomatically) { |
| 64 | + result.throwAggregateExceptionIfErrorsPresent(); |
| 65 | + } |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + // add cycle detection? |
| 70 | + private void preprocessForReconcile() { |
| 71 | + bottomLevelResource.addAll(dependentResourceNodes); |
| 72 | + for (DependentResourceNode<?, P> node : dependentResourceNodes) { |
| 73 | + if (node.getDependsOn().isEmpty()) { |
| 74 | + topLevelResources.add(node); |
| 75 | + } else { |
| 76 | + for (DependentResourceNode dependsOn : node.getDependsOn()) { |
| 77 | + bottomLevelResource.remove(dependsOn); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public boolean isThrowExceptionAutomatically() { |
| 84 | + return throwExceptionAutomatically; |
| 85 | + } |
| 86 | + |
| 87 | + public void setExecutorService(ExecutorService executorService) { |
| 88 | + this.executorService = executorService; |
| 89 | + } |
| 90 | + |
| 91 | + Set<DependentResourceNode> getTopLevelDependentResources() { |
| 92 | + return topLevelResources; |
| 93 | + } |
| 94 | + |
| 95 | + Set<DependentResourceNode> getBottomLevelResource() { |
| 96 | + return bottomLevelResource; |
| 97 | + } |
| 98 | + |
| 99 | + ExecutorService getExecutorService() { |
| 100 | + return executorService; |
| 101 | + } |
| 102 | +} |
0 commit comments