|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent.workflow; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Map.Entry; |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import io.javaoperatorsdk.operator.AggregatedOperatorException; |
| 12 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; |
| 13 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult; |
| 14 | + |
| 15 | +@SuppressWarnings("rawtypes") |
| 16 | +class BaseWorkflowResult implements WorkflowResult { |
| 17 | + private final Map<DependentResource, Detail<?>> results; |
| 18 | + private Boolean hasErroredDependents; |
| 19 | + |
| 20 | + BaseWorkflowResult(Map<DependentResource, Detail<?>> results) { |
| 21 | + this.results = results; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public Map<DependentResource, Exception> getErroredDependents() { |
| 26 | + return getErroredDependentsStream() |
| 27 | + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().error)); |
| 28 | + } |
| 29 | + |
| 30 | + private Stream<Entry<DependentResource, Detail<?>>> getErroredDependentsStream() { |
| 31 | + return results.entrySet().stream().filter(entry -> entry.getValue().error != null); |
| 32 | + } |
| 33 | + |
| 34 | + protected Map<DependentResource, Detail<?>> results() { |
| 35 | + return results; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Optional<DependentResource> getDependentResourceByName(String name) { |
| 40 | + if (name == null || name.isEmpty()) { |
| 41 | + return Optional.empty(); |
| 42 | + } |
| 43 | + return results.keySet().stream().filter(dr -> dr.name().equals(name)).findFirst(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public <T> Optional<T> getDependentConditionResult(DependentResource dependentResource, |
| 48 | + Condition.Type conditionType, Class<T> expectedResultType) { |
| 49 | + if (dependentResource == null) { |
| 50 | + return Optional.empty(); |
| 51 | + } |
| 52 | + |
| 53 | + final var result = new Object[1]; |
| 54 | + try { |
| 55 | + return Optional.ofNullable(results().get(dependentResource)) |
| 56 | + .flatMap(detail -> detail.getResultForConditionWithType(conditionType)) |
| 57 | + .map(r -> result[0] = r.getDetail()) |
| 58 | + .map(expectedResultType::cast); |
| 59 | + } catch (Exception e) { |
| 60 | + throw new IllegalArgumentException("Condition " + |
| 61 | + "result " + result[0] + |
| 62 | + " for Dependent " + dependentResource.name() + " doesn't match expected type " |
| 63 | + + expectedResultType.getSimpleName(), e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected List<DependentResource> listFilteredBy( |
| 68 | + Function<Detail, Boolean> filter) { |
| 69 | + return results.entrySet().stream() |
| 70 | + .filter(e -> filter.apply(e.getValue())) |
| 71 | + .map(Map.Entry::getKey) |
| 72 | + .toList(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean erroredDependentsExist() { |
| 77 | + if (hasErroredDependents == null) { |
| 78 | + hasErroredDependents = !getErroredDependents().isEmpty(); |
| 79 | + } |
| 80 | + return hasErroredDependents; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void throwAggregateExceptionIfErrorsPresent() { |
| 85 | + if (erroredDependentsExist()) { |
| 86 | + throw new AggregatedOperatorException("Exception(s) during workflow execution.", |
| 87 | + getErroredDependentsStream() |
| 88 | + .collect(Collectors.toMap(e -> e.getKey().name(), e -> e.getValue().error))); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @SuppressWarnings("UnusedReturnValue") |
| 93 | + static class DetailBuilder<R> { |
| 94 | + private Exception error; |
| 95 | + private ReconcileResult<R> reconcileResult; |
| 96 | + private DetailedCondition.Result activationConditionResult; |
| 97 | + private DetailedCondition.Result deletePostconditionResult; |
| 98 | + private DetailedCondition.Result readyPostconditionResult; |
| 99 | + private DetailedCondition.Result reconcilePostconditionResult; |
| 100 | + private boolean deleted; |
| 101 | + private boolean visited; |
| 102 | + private boolean markedForDelete; |
| 103 | + |
| 104 | + Detail<R> build() { |
| 105 | + return new Detail<>(error, reconcileResult, activationConditionResult, |
| 106 | + deletePostconditionResult, readyPostconditionResult, reconcilePostconditionResult, |
| 107 | + deleted, visited, markedForDelete); |
| 108 | + } |
| 109 | + |
| 110 | + DetailBuilder<R> withResultForCondition( |
| 111 | + ConditionWithType conditionWithType, |
| 112 | + DetailedCondition.Result conditionResult) { |
| 113 | + switch (conditionWithType.type()) { |
| 114 | + case ACTIVATION -> activationConditionResult = conditionResult; |
| 115 | + case DELETE -> deletePostconditionResult = conditionResult; |
| 116 | + case READY -> readyPostconditionResult = conditionResult; |
| 117 | + case RECONCILE -> reconcilePostconditionResult = conditionResult; |
| 118 | + default -> |
| 119 | + throw new IllegalStateException("Unexpected condition type: " + conditionWithType); |
| 120 | + } |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + DetailBuilder<R> withError(Exception error) { |
| 125 | + this.error = error; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + DetailBuilder<R> withReconcileResult(ReconcileResult<R> reconcileResult) { |
| 130 | + this.reconcileResult = reconcileResult; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + DetailBuilder<R> markAsDeleted() { |
| 135 | + this.deleted = true; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + public boolean hasError() { |
| 140 | + return error != null; |
| 141 | + } |
| 142 | + |
| 143 | + public boolean hasPostDeleteConditionNotMet() { |
| 144 | + return deletePostconditionResult != null && !deletePostconditionResult.isSuccess(); |
| 145 | + } |
| 146 | + |
| 147 | + public boolean isReady() { |
| 148 | + return readyPostconditionResult == null || readyPostconditionResult.isSuccess(); |
| 149 | + } |
| 150 | + |
| 151 | + DetailBuilder<R> markAsVisited() { |
| 152 | + visited = true; |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + public boolean isVisited() { |
| 157 | + return visited; |
| 158 | + } |
| 159 | + |
| 160 | + public boolean isMarkedForDelete() { |
| 161 | + return markedForDelete; |
| 162 | + } |
| 163 | + |
| 164 | + DetailBuilder<R> markForDelete() { |
| 165 | + markedForDelete = true; |
| 166 | + return this; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + record Detail<R>(Exception error, ReconcileResult<R> reconcileResult, |
| 172 | + DetailedCondition.Result activationConditionResult, |
| 173 | + DetailedCondition.Result deletePostconditionResult, |
| 174 | + DetailedCondition.Result readyPostconditionResult, |
| 175 | + DetailedCondition.Result reconcilePostconditionResult, |
| 176 | + boolean deleted, boolean visited, boolean markedForDelete) { |
| 177 | + |
| 178 | + boolean isConditionWithTypeMet(Condition.Type conditionType) { |
| 179 | + return getResultForConditionWithType(conditionType).map(DetailedCondition.Result::isSuccess) |
| 180 | + .orElse(true); |
| 181 | + } |
| 182 | + |
| 183 | + Optional<DetailedCondition.Result<?>> getResultForConditionWithType( |
| 184 | + Condition.Type conditionType) { |
| 185 | + return switch (conditionType) { |
| 186 | + case ACTIVATION -> Optional.ofNullable(activationConditionResult); |
| 187 | + case DELETE -> Optional.ofNullable(deletePostconditionResult); |
| 188 | + case READY -> Optional.ofNullable(readyPostconditionResult); |
| 189 | + case RECONCILE -> Optional.ofNullable(reconcilePostconditionResult); |
| 190 | + }; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments