Skip to content

Commit 706aef2

Browse files
authored
feat: explicit node configuration in WorkflowBuilder (#2511)
* feat: explicit node configuration in WorkflowBuilder Fixes #2284 Signed-off-by: Chris Laprun <[email protected]> * refactor: rename more appropriately Signed-off-by: Chris Laprun <[email protected]> --------- Signed-off-by: Chris Laprun <[email protected]>
1 parent 1ed539b commit 706aef2

File tree

6 files changed

+181
-140
lines changed

6 files changed

+181
-140
lines changed

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

+78-37
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,25 @@ public class WorkflowBuilder<P extends HasMetadata> {
1717
private final Map<String, DependentResourceNode<?, P>> dependentResourceNodes =
1818
new HashMap<>();
1919
private boolean throwExceptionAutomatically = THROW_EXCEPTION_AUTOMATICALLY_DEFAULT;
20-
private DependentResourceNode currentNode;
2120
private boolean isCleaner = false;
2221

23-
public WorkflowBuilder<P> addDependentResource(DependentResource dependentResource) {
24-
currentNode = new DependentResourceNode<>(dependentResource);
25-
isCleaner = isCleaner || dependentResource.isDeletable();
26-
final var actualName = dependentResource.name();
27-
dependentResourceNodes.put(actualName, currentNode);
28-
return this;
29-
}
30-
31-
public WorkflowBuilder<P> dependsOn(Set<DependentResource> dependentResources) {
32-
for (var dependentResource : dependentResources) {
33-
var dependsOn = getNodeByDependentResource(dependentResource);
34-
currentNode.addDependsOnRelation(dependsOn);
35-
}
36-
return this;
37-
}
38-
39-
public WorkflowBuilder<P> dependsOn(DependentResource... dependentResources) {
40-
if (dependentResources != null) {
41-
return dependsOn(new HashSet<>(Arrays.asList(dependentResources)));
42-
}
43-
return this;
22+
public WorkflowNodeConfigurationBuilder addDependentResourceAndConfigure(
23+
DependentResource dependentResource) {
24+
final var currentNode = doAddDependentResource(dependentResource);
25+
return new WorkflowNodeConfigurationBuilder(currentNode);
4426
}
4527

46-
public WorkflowBuilder<P> withReconcilePrecondition(Condition reconcilePrecondition) {
47-
currentNode.setReconcilePrecondition(reconcilePrecondition);
48-
return this;
49-
}
50-
51-
public WorkflowBuilder<P> withReadyPostcondition(Condition readyPostcondition) {
52-
currentNode.setReadyPostcondition(readyPostcondition);
53-
return this;
54-
}
55-
56-
public WorkflowBuilder<P> withDeletePostcondition(Condition deletePostcondition) {
57-
currentNode.setDeletePostcondition(deletePostcondition);
28+
public WorkflowBuilder<P> addDependentResource(DependentResource dependentResource) {
29+
doAddDependentResource(dependentResource);
5830
return this;
5931
}
6032

61-
public WorkflowBuilder<P> withActivationCondition(Condition activationCondition) {
62-
currentNode.setActivationCondition(activationCondition);
63-
return this;
33+
private DependentResourceNode doAddDependentResource(DependentResource dependentResource) {
34+
final var currentNode = new DependentResourceNode<>(dependentResource);
35+
isCleaner = isCleaner || dependentResource.isDeletable();
36+
final var actualName = dependentResource.name();
37+
dependentResourceNodes.put(actualName, currentNode);
38+
return currentNode;
6439
}
6540

6641
DependentResourceNode getNodeByDependentResource(DependentResource<?, ?> dependentResource) {
@@ -89,4 +64,70 @@ DefaultWorkflow<P> buildAsDefaultWorkflow() {
8964
return new DefaultWorkflow(new HashSet<>(dependentResourceNodes.values()),
9065
throwExceptionAutomatically, isCleaner);
9166
}
67+
68+
public class WorkflowNodeConfigurationBuilder {
69+
private final DependentResourceNode currentNode;
70+
71+
private WorkflowNodeConfigurationBuilder(DependentResourceNode currentNode) {
72+
this.currentNode = currentNode;
73+
}
74+
75+
public WorkflowBuilder<P> addDependentResource(DependentResource<?, ?> dependentResource) {
76+
return WorkflowBuilder.this.addDependentResource(dependentResource);
77+
}
78+
79+
public WorkflowNodeConfigurationBuilder addDependentResourceAndConfigure(
80+
DependentResource<?, ?> dependentResource) {
81+
final var currentNode = WorkflowBuilder.this.doAddDependentResource(dependentResource);
82+
return new WorkflowNodeConfigurationBuilder(currentNode);
83+
}
84+
85+
public Workflow<P> build() {
86+
return WorkflowBuilder.this.build();
87+
}
88+
89+
DefaultWorkflow<P> buildAsDefaultWorkflow() {
90+
return WorkflowBuilder.this.buildAsDefaultWorkflow();
91+
}
92+
93+
public WorkflowBuilder<P> withThrowExceptionFurther(boolean throwExceptionFurther) {
94+
return WorkflowBuilder.this.withThrowExceptionFurther(throwExceptionFurther);
95+
}
96+
97+
public WorkflowNodeConfigurationBuilder toDependOn(Set<DependentResource> dependentResources) {
98+
for (var dependentResource : dependentResources) {
99+
var dependsOn = getNodeByDependentResource(dependentResource);
100+
currentNode.addDependsOnRelation(dependsOn);
101+
}
102+
return this;
103+
}
104+
105+
public WorkflowNodeConfigurationBuilder toDependOn(DependentResource... dependentResources) {
106+
if (dependentResources != null) {
107+
return toDependOn(new HashSet<>(Arrays.asList(dependentResources)));
108+
}
109+
return this;
110+
}
111+
112+
public WorkflowNodeConfigurationBuilder withReconcilePrecondition(
113+
Condition reconcilePrecondition) {
114+
currentNode.setReconcilePrecondition(reconcilePrecondition);
115+
return this;
116+
}
117+
118+
public WorkflowNodeConfigurationBuilder withReadyPostcondition(Condition readyPostcondition) {
119+
currentNode.setReadyPostcondition(readyPostcondition);
120+
return this;
121+
}
122+
123+
public WorkflowNodeConfigurationBuilder withDeletePostcondition(Condition deletePostcondition) {
124+
currentNode.setDeletePostcondition(deletePostcondition);
125+
return this;
126+
}
127+
128+
public WorkflowNodeConfigurationBuilder withActivationCondition(Condition activationCondition) {
129+
currentNode.setActivationCondition(activationCondition);
130+
return this;
131+
}
132+
}
92133
}

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

+26-24
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ void setup() {
5454
void cleanUpDiamondWorkflow() {
5555
var workflow = new WorkflowBuilder<TestCustomResource>()
5656
.addDependentResource(dd1)
57-
.addDependentResource(dr1).dependsOn(dd1)
58-
.addDependentResource(dd2).dependsOn(dd1)
59-
.addDependentResource(dd3).dependsOn(dr1, dd2)
57+
.addDependentResourceAndConfigure(dr1).toDependOn(dd1)
58+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
59+
.addDependentResourceAndConfigure(dd3).toDependOn(dr1, dd2)
6060
.build();
6161

6262
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -73,9 +73,9 @@ void cleanUpDiamondWorkflow() {
7373
void dontDeleteIfDependentErrored() {
7474
var workflow = new WorkflowBuilder<TestCustomResource>()
7575
.addDependentResource(dd1)
76-
.addDependentResource(dd2).dependsOn(dd1)
77-
.addDependentResource(dd3).dependsOn(dd2)
78-
.addDependentResource(errorDD).dependsOn(dd2)
76+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
77+
.addDependentResourceAndConfigure(dd3).toDependOn(dd2)
78+
.addDependentResourceAndConfigure(errorDD).toDependOn(dd2)
7979
.withThrowExceptionFurther(false)
8080
.build();
8181

@@ -95,7 +95,8 @@ void dontDeleteIfDependentErrored() {
9595
void cleanupConditionTrivialCase() {
9696
var workflow = new WorkflowBuilder<TestCustomResource>()
9797
.addDependentResource(dd1)
98-
.addDependentResource(dd2).dependsOn(dd1).withDeletePostcondition(notMetCondition)
98+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
99+
.withDeletePostcondition(notMetCondition)
99100
.build();
100101

101102
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -110,7 +111,7 @@ void cleanupConditionTrivialCase() {
110111
void cleanupConditionMet() {
111112
var workflow = new WorkflowBuilder<TestCustomResource>()
112113
.addDependentResource(dd1)
113-
.addDependentResource(dd2).dependsOn(dd1).withDeletePostcondition(metCondition)
114+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1).withDeletePostcondition(metCondition)
114115
.build();
115116

116117
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -126,9 +127,10 @@ void cleanupConditionMet() {
126127
void cleanupConditionDiamondWorkflow() {
127128
var workflow = new WorkflowBuilder<TestCustomResource>()
128129
.addDependentResource(dd1)
129-
.addDependentResource(dd2).dependsOn(dd1)
130-
.addDependentResource(dd3).dependsOn(dd1).withDeletePostcondition(notMetCondition)
131-
.addDependentResource(dd4).dependsOn(dd2, dd3)
130+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
131+
.addDependentResourceAndConfigure(dd3).toDependOn(dd1)
132+
.withDeletePostcondition(notMetCondition)
133+
.addDependentResourceAndConfigure(dd4).toDependOn(dd2, dd3)
132134
.build();
133135

134136
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -162,10 +164,10 @@ void dontDeleteIfGarbageCollected() {
162164
void ifDependentActiveDependentNormallyDeleted() {
163165
var workflow = new WorkflowBuilder<TestCustomResource>()
164166
.addDependentResource(dd1)
165-
.addDependentResource(dd2).dependsOn(dd1)
166-
.addDependentResource(dd3).dependsOn(dd1)
167+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
168+
.addDependentResourceAndConfigure(dd3).toDependOn(dd1)
167169
.withActivationCondition(metCondition)
168-
.addDependentResource(dd4).dependsOn(dd2, dd3)
170+
.addDependentResourceAndConfigure(dd4).toDependOn(dd2, dd3)
169171
.build();
170172

171173
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -182,11 +184,11 @@ void ifDependentActiveDependentNormallyDeleted() {
182184
void ifDependentActiveDeletePostConditionIsChecked() {
183185
var workflow = new WorkflowBuilder<TestCustomResource>()
184186
.addDependentResource(dd1)
185-
.addDependentResource(dd2).dependsOn(dd1)
186-
.addDependentResource(dd3).dependsOn(dd1)
187+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
188+
.addDependentResourceAndConfigure(dd3).toDependOn(dd1)
187189
.withDeletePostcondition(notMetCondition)
188190
.withActivationCondition(metCondition)
189-
.addDependentResource(dd4).dependsOn(dd2, dd3)
191+
.addDependentResourceAndConfigure(dd4).toDependOn(dd2, dd3)
190192
.build();
191193

192194
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -206,10 +208,10 @@ void ifDependentActiveDeletePostConditionIsChecked() {
206208
void ifDependentInactiveDeleteIsNotCalled() {
207209
var workflow = new WorkflowBuilder<TestCustomResource>()
208210
.addDependentResource(dd1)
209-
.addDependentResource(dd2).dependsOn(dd1)
210-
.addDependentResource(dd3).dependsOn(dd1)
211+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
212+
.addDependentResourceAndConfigure(dd3).toDependOn(dd1)
211213
.withActivationCondition(notMetCondition)
212-
.addDependentResource(dd4).dependsOn(dd2, dd3)
214+
.addDependentResourceAndConfigure(dd4).toDependOn(dd2, dd3)
213215
.build();
214216

215217
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -225,11 +227,11 @@ void ifDependentInactiveDeleteIsNotCalled() {
225227
void ifDependentInactiveDeletePostConditionNotChecked() {
226228
var workflow = new WorkflowBuilder<TestCustomResource>()
227229
.addDependentResource(dd1)
228-
.addDependentResource(dd2).dependsOn(dd1)
229-
.addDependentResource(dd3).dependsOn(dd1)
230+
.addDependentResourceAndConfigure(dd2).toDependOn(dd1)
231+
.addDependentResourceAndConfigure(dd3).toDependOn(dd1)
230232
.withDeletePostcondition(notMetCondition)
231233
.withActivationCondition(notMetCondition)
232-
.addDependentResource(dd4).dependsOn(dd2, dd3)
234+
.addDependentResourceAndConfigure(dd4).toDependOn(dd2, dd3)
233235
.build();
234236

235237
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -243,7 +245,7 @@ void ifDependentInactiveDeletePostConditionNotChecked() {
243245
@Test
244246
void singleInactiveDependent() {
245247
var workflow = new WorkflowBuilder<TestCustomResource>()
246-
.addDependentResource(dd1)
248+
.addDependentResourceAndConfigure(dd1)
247249
.withActivationCondition(notMetCondition)
248250
.build();
249251

0 commit comments

Comments
 (0)