Skip to content

Commit 673a531

Browse files
committed
feat: add getDependentConditionResult method and document it
Signed-off-by: Chris Laprun <[email protected]>
1 parent bf51047 commit 673a531

File tree

5 files changed

+58
-25
lines changed

5 files changed

+58
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected boolean isNotReady(DependentResourceNode<?, P> dependentResourceNode)
120120
}
121121

122122
protected boolean isInError(DependentResourceNode<?, P> dependentResourceNode) {
123-
return getResultFlagFor(dependentResourceNode, WorkflowResult.DetailBuilder::hasError);
123+
return getResultFlagFor(dependentResourceNode, WorkflowResult.DetailBuilder::hasError);
124124
}
125125

126126
protected synchronized void handleNodeExecutionFinish(

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
@SuppressWarnings("rawtypes")
99
public class WorkflowCleanupResult extends WorkflowResult {
10+
private Boolean allPostConditionsMet;
11+
1012
WorkflowCleanupResult(Map<DependentResource, Detail<?>> results) {
1113
super(results);
1214
}
@@ -20,6 +22,9 @@ public List<DependentResource> getPostConditionNotMetDependents() {
2022
}
2123

2224
public boolean allPostConditionsMet() {
23-
return getPostConditionNotMetDependents().isEmpty();
25+
if (allPostConditionsMet == null) {
26+
allPostConditionsMet = getPostConditionNotMetDependents().isEmpty();
27+
}
28+
return allPostConditionsMet;
2429
}
2530
}

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

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

99
@SuppressWarnings("rawtypes")
1010
public class WorkflowReconcileResult extends WorkflowResult {
11+
private Boolean allDependentsReady;
1112

1213
WorkflowReconcileResult(Map<DependentResource, Detail<?>> results) {
1314
super(results);
@@ -21,23 +22,15 @@ public List<DependentResource> getNotReadyDependents() {
2122
return listFilteredBy(detail -> !detail.isConditionWithTypeMet(Condition.Type.READY));
2223
}
2324

24-
public <T> T getNotReadyDependentResult(DependentResource dependentResource,
25+
public <T> Optional<T> getNotReadyDependentResult(DependentResource dependentResource,
2526
Class<T> expectedResultType) {
26-
final var result = new Object[1];
27-
try {
28-
return Optional.ofNullable(results().get(dependentResource))
29-
.flatMap(detail -> detail.getResultForConditionWithType(Condition.Type.READY))
30-
.map(r -> result[0] = r.getResult())
31-
.map(expectedResultType::cast)
32-
.orElse(null);
33-
} catch (Exception e) {
34-
throw new IllegalArgumentException("Condition result " + result[0] +
35-
" for Dependent " + dependentResource.name() + " doesn't match expected type "
36-
+ expectedResultType.getSimpleName(), e);
37-
}
27+
return getDependentConditionResult(dependentResource, Condition.Type.READY, expectedResultType);
3828
}
3929

4030
public boolean allDependentResourcesReady() {
41-
return getNotReadyDependents().isEmpty();
31+
if (allDependentsReady == null) {
32+
allDependentsReady = getNotReadyDependents().isEmpty();
33+
}
34+
return allDependentsReady;
4235
}
4336
}

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ protected Map<DependentResource, Detail<?>> results() {
3434
return results;
3535
}
3636

37+
/**
38+
* Retrieves the optional result of the condition with the specified type for the specified
39+
* dependent resource.
40+
*
41+
* @param <T> the expected result type of the condition
42+
* @param dependentResource the dependent resource for which we want to retrieve a condition
43+
* result
44+
* @param conditionType the condition type which result we're interested in
45+
* @param expectedResultType the expected result type of the condition
46+
* @return the dependent condition result if it exists or {@link Optional#empty()} otherwise
47+
* @throws IllegalArgumentException if a result exists but is not of the expected type
48+
*/
49+
public <T> Optional<T> getDependentConditionResult(DependentResource dependentResource,
50+
Condition.Type conditionType, Class<T> expectedResultType) {
51+
final var result = new Object[1];
52+
try {
53+
return Optional.ofNullable(results().get(dependentResource))
54+
.flatMap(detail -> detail.getResultForConditionWithType(conditionType))
55+
.map(r -> result[0] = r.getResult())
56+
.map(expectedResultType::cast);
57+
} catch (Exception e) {
58+
throw new IllegalArgumentException("Condition " +
59+
"result " + result[0] +
60+
" for Dependent " + dependentResource.name() + " doesn't match expected type "
61+
+ expectedResultType.getSimpleName(), e);
62+
}
63+
}
64+
3765
protected List<DependentResource> listFilteredBy(
3866
Function<Detail, Boolean> filter) {
3967
return results.entrySet().stream()
@@ -71,16 +99,20 @@ static class DetailBuilder<R> {
7199

72100
Detail<R> build() {
73101
return new Detail<>(error, reconcileResult, activationConditionResult,
74-
deletePostconditionResult, readyPostconditionResult, reconcilePostconditionResult, deleted, visited, markedForDelete);
102+
deletePostconditionResult, readyPostconditionResult, reconcilePostconditionResult,
103+
deleted, visited, markedForDelete);
75104
}
76105

77-
DetailBuilder<R> withResultForCondition(DependentResourceNode.ConditionWithType conditionWithType, ResultCondition.Result conditionResult) {
106+
DetailBuilder<R> withResultForCondition(
107+
DependentResourceNode.ConditionWithType conditionWithType,
108+
ResultCondition.Result conditionResult) {
78109
switch (conditionWithType.type()) {
79110
case ACTIVATION -> activationConditionResult = conditionResult;
80111
case DELETE -> deletePostconditionResult = conditionResult;
81112
case READY -> readyPostconditionResult = conditionResult;
82113
case RECONCILE -> reconcilePostconditionResult = conditionResult;
83-
default -> throw new IllegalStateException("Unexpected condition type: " + conditionWithType);
114+
default ->
115+
throw new IllegalStateException("Unexpected condition type: " + conditionWithType);
84116
}
85117
return this;
86118
}
@@ -137,13 +169,15 @@ record Detail<R>(Exception error, ReconcileResult<R> reconcileResult,
137169
ResultCondition.Result deletePostconditionResult,
138170
ResultCondition.Result readyPostconditionResult,
139171
ResultCondition.Result reconcilePostconditionResult,
140-
boolean deleted, boolean visited, boolean markedForDelete) {
172+
boolean deleted, boolean visited, boolean markedForDelete) {
141173

142174
boolean isConditionWithTypeMet(Condition.Type conditionType) {
143-
return getResultForConditionWithType(conditionType).map(ResultCondition.Result::isSuccess).orElse(true);
175+
return getResultForConditionWithType(conditionType).map(ResultCondition.Result::isSuccess)
176+
.orElse(true);
144177
}
145178

146-
Optional<ResultCondition.Result<?>> getResultForConditionWithType(Condition.Type conditionType) {
179+
Optional<ResultCondition.Result<?>> getResultForConditionWithType(
180+
Condition.Type conditionType) {
147181
return switch (conditionType) {
148182
case ACTIVATION -> Optional.ofNullable(activationConditionResult);
149183
case DELETE -> Optional.ofNullable(deletePostconditionResult);

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowReconcileExecutorTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,8 @@ public boolean isSuccess() {
604604
.build();
605605

606606
final var reconcileResult = workflow.reconcile(new TestCustomResource(), mockContext);
607-
assertEquals(result, reconcileResult.getNotReadyDependentResult(dr1, Integer.class));
607+
assertEquals(result,
608+
reconcileResult.getNotReadyDependentResult(dr1, Integer.class).orElseThrow());
608609
}
609610

610611
@Test
@@ -643,13 +644,13 @@ public boolean isSuccess() {
643644
}
644645

645646
@Test
646-
void shouldReturnNullIfNoConditionResultExists() {
647+
void shouldReturnEmptyIfNoConditionResultExists() {
647648
var workflow = new WorkflowBuilder<TestCustomResource>()
648649
.addDependentResource(dr1)
649650
.withReadyPostcondition(notMetCondition)
650651
.build();
651652

652653
final var reconcileResult = workflow.reconcile(new TestCustomResource(), mockContext);
653-
assertNull(reconcileResult.getNotReadyDependentResult(dr1, Integer.class));
654+
assertTrue(reconcileResult.getNotReadyDependentResult(dr1, Integer.class).isEmpty());
654655
}
655656
}

0 commit comments

Comments
 (0)