Skip to content

Commit 6c30945

Browse files
metacosmcsviri
authored andcommitted
fix: explicitly mark as visited
Signed-off-by: Chris Laprun <[email protected]>
1 parent 2d50c96 commit 6c30945

File tree

8 files changed

+62
-31
lines changed

8 files changed

+62
-31
lines changed

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/AbstractWorkflowExecutor.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ protected boolean noMoreExecutionsScheduled() {
7272
}
7373

7474
protected boolean alreadyVisited(DependentResourceNode<?, P> dependentResourceNode) {
75-
return results.containsKey(dependentResourceNode);
76-
}
77-
78-
protected void markAsVisited(DependentResourceNode<?, P> dependentResourceNode) {
79-
createOrGetResultFor(dependentResourceNode);
75+
final WorkflowResult.DetailBuilder<?> builder = results.get(dependentResourceNode);
76+
return builder != null && builder.isVisited();
8077
}
8178

8279
protected boolean postDeleteConditionNotMet(DependentResourceNode<?, P> dependentResourceNode) {
@@ -162,7 +159,7 @@ protected <R> void registerOrDeregisterEventSourceBasedOnActivation(
162159
}
163160
}
164161

165-
protected Map<DependentResource, WorkflowResult.Detail> asDetails() {
162+
protected Map<DependentResource, WorkflowResult.Detail<?>> asDetails() {
166163
return results.entrySet().stream()
167164
.collect(
168165
Collectors.toMap(e -> e.getKey().getDependentResource(), e -> e.getValue().build()));

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/ResultCondition.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
public interface ResultCondition<R, P extends HasMetadata, T> extends Condition<R, P> {
88
Result<T> detailedIsMet(DependentResource<R, P> dependentResource, P primary, Context<P> context);
99

10-
Object NULL = new Object();
11-
1210
@Override
1311
default boolean isMet(DependentResource<R, P> dependentResource, P primary, Context<P> context) {
1412
return detailedIsMet(dependentResource, primary, context).isSuccess();
@@ -18,31 +16,45 @@ interface Result<T> {
1816
Result metWithoutResult = new Result() {
1917
@Override
2018
public Object getResult() {
21-
return NULL;
19+
return null;
2220
}
2321

2422
@Override
2523
public boolean isSuccess() {
2624
return true;
2725
}
26+
27+
@Override
28+
public String toString() {
29+
return asString();
30+
}
2831
};
2932

3033
Result unmetWithoutResult = new Result() {
3134
@Override
3235
public Object getResult() {
33-
return NULL;
36+
return null;
3437
}
3538

3639
@Override
3740
public boolean isSuccess() {
3841
return false;
3942
}
43+
44+
@Override
45+
public String toString() {
46+
return asString();
47+
}
4048
};
4149

4250
static Result withoutResult(boolean success) {
4351
return success ? metWithoutResult : unmetWithoutResult;
4452
}
4553

54+
default String asString() {
55+
return "Result: " + getResult() + " met: " + isSuccess();
56+
}
57+
4658
T getResult();
4759

4860
boolean isSuccess();

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowCleanupExecutor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void doRun(DependentResourceNode<R, P> dependentResourceNode) {
7474
}
7575

7676
if (deletePostConditionMet) {
77-
markAsVisited(dependentResourceNode);
77+
createOrGetResultFor(dependentResourceNode).markAsVisited();
7878
handleDependentCleaned(dependentResourceNode);
7979
}
8080
}

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowCleanupResult.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@SuppressWarnings("rawtypes")
99
public class WorkflowCleanupResult extends WorkflowResult {
10-
WorkflowCleanupResult(Map<DependentResource, Detail> results) {
10+
WorkflowCleanupResult(Map<DependentResource, Detail<?>> results) {
1111
super(results);
1212
}
1313

@@ -16,7 +16,7 @@ public List<DependentResource> getDeleteCalledOnDependents() {
1616
}
1717

1818
public List<DependentResource> getPostConditionNotMetDependents() {
19-
return listFilteredBy(detail -> !Detail.isConditionMet(detail.deletePostconditionResult()));
19+
return listFilteredBy(detail -> !detail.isConditionWithTypeMet(Condition.Type.DELETE));
2020
}
2121

2222
public boolean allPostConditionsMet() {

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowReconcileExecutor.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

3+
import java.util.ArrayList;
34
import java.util.HashSet;
45
import java.util.Set;
56
import java.util.concurrent.ConcurrentHashMap;
@@ -140,12 +141,13 @@ protected void doRun(DependentResourceNode<R, P> dependentResourceNode) {
140141
log.debug(
141142
"Reconciling for primary: {} node: {} ", primaryID, dependentResourceNode);
142143
ReconcileResult reconcileResult = dependentResource.reconcile(primary, context);
143-
createOrGetResultFor(dependentResourceNode).withReconcileResult(reconcileResult);
144+
final var detailBuilder = createOrGetResultFor(dependentResourceNode);
145+
detailBuilder.withReconcileResult(reconcileResult);
144146

145147
if (isConditionMet(dependentResourceNode.getReconcilePrecondition(), dependentResourceNode)) {
146148
log.debug("Setting already reconciled for: {} primaryID: {}",
147149
dependentResourceNode, primaryID);
148-
markAsVisited(dependentResourceNode);
150+
detailBuilder.markAsVisited();
149151
handleDependentsReconcile(dependentResourceNode);
150152
} else {
151153
log.debug("Setting already reconciled but not ready for: {}", dependentResourceNode);
@@ -162,8 +164,6 @@ private NodeDeleteExecutor(DependentResourceNode<R, P> dependentResourceNode) {
162164
@Override
163165
@SuppressWarnings("unchecked")
164166
protected void doRun(DependentResourceNode<R, P> dependentResourceNode) {
165-
var deletePostCondition = dependentResourceNode.getDeletePostcondition();
166-
167167
final var dependentResource = dependentResourceNode.getDependentResource();
168168
boolean deletePostConditionMet = true;
169169
if (isConditionMet(dependentResourceNode.getActivationCondition(), dependentResourceNode)) {
@@ -173,11 +173,12 @@ protected void doRun(DependentResourceNode<R, P> dependentResourceNode) {
173173
if (dependentResource instanceof Deleter) {
174174
((Deleter<P>) dependentResource).delete(primary, context);
175175
}
176-
deletePostConditionMet = isConditionMet(deletePostCondition, dependentResourceNode);
176+
deletePostConditionMet =
177+
isConditionMet(dependentResourceNode.getDeletePostcondition(), dependentResourceNode);
177178
}
178179

179180
if (deletePostConditionMet) {
180-
markAsVisited(dependentResourceNode);
181+
createOrGetResultFor(dependentResourceNode).markAsVisited();
181182
handleDependentDeleted(dependentResourceNode);
182183
}
183184
}
@@ -224,7 +225,7 @@ private void markDependentsForDelete(DependentResourceNode<?, P> dependentResour
224225
}
225226
} else {
226227
// this is for an edge case when there is only one resource but that is not active
227-
markAsVisited(dependentResourceNode);
228+
createOrGetResultFor(dependentResourceNode).markAsVisited();
228229
if (dependents.isEmpty()) {
229230
handleNodeExecutionFinish(dependentResourceNode);
230231
} else {

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowReconcileResult.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@SuppressWarnings("rawtypes")
1010
public class WorkflowReconcileResult extends WorkflowResult {
1111

12-
public WorkflowReconcileResult(Map<DependentResource, Detail> results) {
12+
public WorkflowReconcileResult(Map<DependentResource, Detail<?>> results) {
1313
super(results);
1414
}
1515

@@ -18,7 +18,7 @@ public List<DependentResource> getReconciledDependents() {
1818
}
1919

2020
public List<DependentResource> getNotReadyDependents() {
21-
return listFilteredBy(detail -> !Detail.isConditionMet(detail.reconcilePostconditionResult()));
21+
return listFilteredBy(detail -> !detail.isConditionWithTypeMet(Condition.Type.READY));
2222
}
2323

2424
public <T> T getNotReadyDependentResult(DependentResource dependentResource,

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowResult.java

+28-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.Map;
55
import java.util.Map.Entry;
6+
import java.util.Optional;
67
import java.util.function.Function;
78
import java.util.stream.Collectors;
89
import java.util.stream.Stream;
@@ -13,10 +14,10 @@
1314

1415
@SuppressWarnings("rawtypes")
1516
class WorkflowResult {
16-
private final Map<DependentResource, Detail> results;
17+
private final Map<DependentResource, Detail<?>> results;
1718
private Boolean hasErroredDependents;
1819

19-
WorkflowResult(Map<DependentResource, Detail> results) {
20+
WorkflowResult(Map<DependentResource, Detail<?>> results) {
2021
this.results = results;
2122
}
2223

@@ -25,11 +26,11 @@ public Map<DependentResource, Exception> getErroredDependents() {
2526
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().error));
2627
}
2728

28-
private Stream<Entry<DependentResource, Detail>> getErroredDependentsStream() {
29+
private Stream<Entry<DependentResource, Detail<?>>> getErroredDependentsStream() {
2930
return results.entrySet().stream().filter(entry -> entry.getValue().error != null);
3031
}
3132

32-
protected Map<DependentResource, Detail> results() {
33+
protected Map<DependentResource, Detail<?>> results() {
3334
return results;
3435
}
3536

@@ -64,10 +65,11 @@ static class DetailBuilder<R> {
6465
private ResultCondition.Result readyPostconditionResult;
6566
private ResultCondition.Result reconcilePostconditionResult;
6667
private boolean deleted;
68+
private boolean visited;
6769

6870
Detail<R> build() {
6971
return new Detail<>(error, reconcileResult, activationConditionResult,
70-
deletePostconditionResult, readyPostconditionResult, reconcilePostconditionResult, deleted);
72+
deletePostconditionResult, readyPostconditionResult, reconcilePostconditionResult, deleted, visited);
7173
}
7274

7375
DetailBuilder<R> withResultForCondition(DependentResourceNode.ConditionWithType conditionWithType, ResultCondition.Result conditionResult) {
@@ -107,6 +109,15 @@ public boolean hasPostDeleteConditionNotMet() {
107109
public boolean isNotReady() {
108110
return readyPostconditionResult != null && !readyPostconditionResult.isSuccess();
109111
}
112+
113+
DetailBuilder<R> markAsVisited() {
114+
visited = true;
115+
return this;
116+
}
117+
118+
public boolean isVisited() {
119+
return visited;
120+
}
110121
}
111122

112123

@@ -115,10 +126,19 @@ record Detail<R>(Exception error, ReconcileResult<R> reconcileResult,
115126
ResultCondition.Result deletePostconditionResult,
116127
ResultCondition.Result readyPostconditionResult,
117128
ResultCondition.Result reconcilePostconditionResult,
118-
boolean deleted) {
129+
boolean deleted, boolean visited) {
130+
131+
boolean isConditionWithTypeMet(Condition.Type conditionType) {
132+
return getResultForConditionWithType(conditionType).map(ResultCondition.Result::isSuccess).orElse(true);
133+
}
119134

120-
static boolean isConditionMet(ResultCondition.Result conditionResult) {
121-
return conditionResult != null && conditionResult.isSuccess();
135+
Optional<ResultCondition.Result<?>> getResultForConditionWithType(Condition.Type conditionType) {
136+
return switch (conditionType) {
137+
case ACTIVATION -> Optional.ofNullable(activationConditionResult);
138+
case DELETE -> Optional.ofNullable(deletePostconditionResult);
139+
case READY -> Optional.ofNullable(readyPostconditionResult);
140+
case RECONCILE -> Optional.ofNullable(reconcilePostconditionResult);
141+
};
122142
}
123143
}
124144
}

Diff for: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowResultTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
class WorkflowResultTest {
1616
private final static WorkflowResult.Detail detail =
17-
new WorkflowResult.Detail<>(new RuntimeException(), null, null, null, null, null, false);
17+
new WorkflowResult.Detail<>(new RuntimeException(), null, null, null, null, null, false,
18+
false);
1819

1920
@Test
2021
void throwsExceptionWithoutNumberingIfAllDifferentClass() {

0 commit comments

Comments
 (0)