Skip to content

Commit 8247f79

Browse files
committed
tests for ready condition
1 parent 6d23631 commit 8247f79

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.javaoperatorsdk.operator.api.reconciler.Context;
1212
import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter;
1313
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
14+
import io.javaoperatorsdk.operator.processing.dependent.workflow.condition.ReadyCondition;
1415
import io.javaoperatorsdk.operator.processing.dependent.workflow.condition.ReconcileCondition;
1516

1617
public class WorkflowReconcileExecutor<P extends HasMetadata> {
@@ -108,6 +109,10 @@ private synchronized void handleNodeExecutionFinish(DependentResourceNode depend
108109
}
109110
}
110111

112+
private synchronized void updateStatusForNotReady(ReadyCondition<?, P> readyCondition) {
113+
readyCondition.addNotReadyStatusInfo(primary);
114+
}
115+
111116
private boolean ownOrParentsReconcileConditionNotMet(
112117
DependentResourceNode<?, ?> dependentResourceNode) {
113118
return reconcileConditionOrParentsConditionNotMet.contains(dependentResourceNode) ||
@@ -143,6 +148,8 @@ public void run() {
143148
.isMet(dependentResource, primary, context)) {
144149
handleDependents = false;
145150
reconciledButNotReady.add(dependentResourceNode);
151+
// needs to be synced
152+
updateStatusForNotReady(dependentResourceNode.getReadyCondition().get());
146153
}
147154
}
148155
alreadyReconciled.add(dependentResourceNode);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ public interface ReadyCondition<R, P extends HasMetadata> {
88

99
boolean isMet(DependentResource<R, P> dependentResource, P primary, Context<P> context);
1010

11+
/**
12+
* If condition not met, the primary resource status might be updated by overriding this method.
13+
* In case there are multiple conditions in a workflow it is updated multiple times.
14+
*
15+
* @param primary
16+
*/
17+
default void addNotReadyStatusInfo(P primary) {}
1118
}

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Optional;
77

8+
import org.assertj.core.api.Assertions;
89
import org.junit.jupiter.api.Test;
910

1011
import io.javaoperatorsdk.operator.AggregatedOperatorException;
@@ -13,6 +14,7 @@
1314
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
1415
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
1516
import io.javaoperatorsdk.operator.processing.dependent.workflow.builder.WorkflowBuilder;
17+
import io.javaoperatorsdk.operator.processing.dependent.workflow.condition.ReadyCondition;
1618
import io.javaoperatorsdk.operator.processing.dependent.workflow.condition.ReconcileCondition;
1719
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
1820

@@ -21,11 +23,31 @@
2123

2224
class WorkflowTest {
2325

26+
public static final String NOT_READY_YET = "NOT READY YET";
2427
private ReconcileCondition met_reconcile_condition =
2528
(dependentResource, primary, context) -> true;
2629
private ReconcileCondition not_met_reconcile_condition =
2730
(dependentResource, primary, context) -> false;
2831

32+
private ReadyCondition<String, TestCustomResource> metReadyCondition =
33+
(dependentResource, primary, context) -> true;
34+
private ReadyCondition<String, TestCustomResource> notMetReadyCondition =
35+
(dependentResource, primary, context) -> false;
36+
37+
private ReadyCondition<String, TestCustomResource> notMetReadyConditionWithStatusUpdate =
38+
new ReadyCondition<>() {
39+
@Override
40+
public boolean isMet(DependentResource<String, TestCustomResource> dependentResource,
41+
TestCustomResource primary, Context<TestCustomResource> context) {
42+
return false;
43+
}
44+
45+
@Override
46+
public void addNotReadyStatusInfo(TestCustomResource primary) {
47+
primary.getStatus().setConfigMapStatus(NOT_READY_YET);
48+
}
49+
};
50+
2951
public static final String VALUE = "value";
3052
private List<ReconcileRecord> executionHistory =
3153
Collections.synchronizedList(new ArrayList<>());
@@ -233,6 +255,59 @@ void oneDependsOnConditionNotMet() {
233255
assertThat(executionHistory).notReconciled(dr2);
234256
}
235257

258+
@Test
259+
void readyConditionTrivialCase() {
260+
var workflow = new WorkflowBuilder<TestCustomResource>()
261+
.addDependent(dr1).withReadyCondition(metReadyCondition).build()
262+
.addDependent(dr2).dependsOn(dr1).build()
263+
.build();
264+
265+
workflow.reconcile(new TestCustomResource(), null);
266+
267+
assertThat(executionHistory).reconciledInOrder(dr1, dr2);
268+
}
269+
270+
@Test
271+
void readyConditionNotMetTrivialCase() {
272+
var workflow = new WorkflowBuilder<TestCustomResource>()
273+
.addDependent(dr1).withReadyCondition(notMetReadyCondition).build()
274+
.addDependent(dr2).dependsOn(dr1).build()
275+
.build();
276+
277+
workflow.reconcile(new TestCustomResource(), null);
278+
279+
assertThat(executionHistory).reconciled(dr1).notReconciled(dr2);
280+
}
281+
282+
@Test
283+
void readyConditionNotMetStatusUpdates() {
284+
var workflow = new WorkflowBuilder<TestCustomResource>()
285+
.addDependent(dr1).withReadyCondition(notMetReadyConditionWithStatusUpdate).build()
286+
.addDependent(dr2).dependsOn(dr1).build()
287+
.build();
288+
289+
var cr = new TestCustomResource();
290+
workflow.reconcile(cr, null);
291+
292+
assertThat(executionHistory).reconciled(dr1).notReconciled(dr2);
293+
Assertions.assertThat(cr.getStatus().getConfigMapStatus()).isEqualTo(NOT_READY_YET);
294+
}
295+
296+
@Test
297+
void readyConditionNotMetInOneParent() {
298+
TestDependent dr3 = new TestDependent("DR_3");
299+
300+
var workflow = new WorkflowBuilder<TestCustomResource>()
301+
.addDependent(dr1).withReadyCondition(notMetReadyCondition).build()
302+
.addDependent(dr2).build()
303+
.addDependent(dr3).dependsOn(dr1, dr2).build()
304+
.build();
305+
306+
workflow.reconcile(new TestCustomResource(), null);
307+
308+
assertThat(executionHistory).reconciled(dr1, dr2).notReconciled(dr3);
309+
}
310+
236311
private class TestDependent implements DependentResource<String, TestCustomResource> {
237312

238313
private String name;

0 commit comments

Comments
 (0)