Skip to content

Commit 02c16ee

Browse files
committed
feat: explicit node configuration in WorkflowBuilder
Fixes #2284 Signed-off-by: Chris Laprun <[email protected]>
1 parent bfdd8ba commit 02c16ee

File tree

6 files changed

+172
-134
lines changed

6 files changed

+172
-134
lines changed

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

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,24 @@ 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 DependentResourceBuilder configuring(DependentResource dependentResource) {
23+
final var currentNode = doAddDependentResource(dependentResource);
24+
return new DependentResourceBuilder(currentNode);
4425
}
4526

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);
27+
public WorkflowBuilder<P> addDependentResource(DependentResource dependentResource) {
28+
doAddDependentResource(dependentResource);
5829
return this;
5930
}
6031

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

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

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

Lines changed: 24 additions & 24 deletions
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+
.configuring(dr1).dependsOn(dd1)
58+
.configuring(dd2).dependsOn(dd1)
59+
.configuring(dd3).dependsOn(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+
.configuring(dd2).dependsOn(dd1)
77+
.configuring(dd3).dependsOn(dd2)
78+
.configuring(errorDD).dependsOn(dd2)
7979
.withThrowExceptionFurther(false)
8080
.build();
8181

@@ -95,7 +95,7 @@ void dontDeleteIfDependentErrored() {
9595
void cleanupConditionTrivialCase() {
9696
var workflow = new WorkflowBuilder<TestCustomResource>()
9797
.addDependentResource(dd1)
98-
.addDependentResource(dd2).dependsOn(dd1).withDeletePostcondition(notMetCondition)
98+
.configuring(dd2).dependsOn(dd1).withDeletePostcondition(notMetCondition)
9999
.build();
100100

101101
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -110,7 +110,7 @@ void cleanupConditionTrivialCase() {
110110
void cleanupConditionMet() {
111111
var workflow = new WorkflowBuilder<TestCustomResource>()
112112
.addDependentResource(dd1)
113-
.addDependentResource(dd2).dependsOn(dd1).withDeletePostcondition(metCondition)
113+
.configuring(dd2).dependsOn(dd1).withDeletePostcondition(metCondition)
114114
.build();
115115

116116
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -126,9 +126,9 @@ void cleanupConditionMet() {
126126
void cleanupConditionDiamondWorkflow() {
127127
var workflow = new WorkflowBuilder<TestCustomResource>()
128128
.addDependentResource(dd1)
129-
.addDependentResource(dd2).dependsOn(dd1)
130-
.addDependentResource(dd3).dependsOn(dd1).withDeletePostcondition(notMetCondition)
131-
.addDependentResource(dd4).dependsOn(dd2, dd3)
129+
.configuring(dd2).dependsOn(dd1)
130+
.configuring(dd3).dependsOn(dd1).withDeletePostcondition(notMetCondition)
131+
.configuring(dd4).dependsOn(dd2, dd3)
132132
.build();
133133

134134
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -162,10 +162,10 @@ void dontDeleteIfGarbageCollected() {
162162
void ifDependentActiveDependentNormallyDeleted() {
163163
var workflow = new WorkflowBuilder<TestCustomResource>()
164164
.addDependentResource(dd1)
165-
.addDependentResource(dd2).dependsOn(dd1)
166-
.addDependentResource(dd3).dependsOn(dd1)
165+
.configuring(dd2).dependsOn(dd1)
166+
.configuring(dd3).dependsOn(dd1)
167167
.withActivationCondition(metCondition)
168-
.addDependentResource(dd4).dependsOn(dd2, dd3)
168+
.configuring(dd4).dependsOn(dd2, dd3)
169169
.build();
170170

171171
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -182,11 +182,11 @@ void ifDependentActiveDependentNormallyDeleted() {
182182
void ifDependentActiveDeletePostConditionIsChecked() {
183183
var workflow = new WorkflowBuilder<TestCustomResource>()
184184
.addDependentResource(dd1)
185-
.addDependentResource(dd2).dependsOn(dd1)
186-
.addDependentResource(dd3).dependsOn(dd1)
185+
.configuring(dd2).dependsOn(dd1)
186+
.configuring(dd3).dependsOn(dd1)
187187
.withDeletePostcondition(notMetCondition)
188188
.withActivationCondition(metCondition)
189-
.addDependentResource(dd4).dependsOn(dd2, dd3)
189+
.configuring(dd4).dependsOn(dd2, dd3)
190190
.build();
191191

192192
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -206,10 +206,10 @@ void ifDependentActiveDeletePostConditionIsChecked() {
206206
void ifDependentInactiveDeleteIsNotCalled() {
207207
var workflow = new WorkflowBuilder<TestCustomResource>()
208208
.addDependentResource(dd1)
209-
.addDependentResource(dd2).dependsOn(dd1)
210-
.addDependentResource(dd3).dependsOn(dd1)
209+
.configuring(dd2).dependsOn(dd1)
210+
.configuring(dd3).dependsOn(dd1)
211211
.withActivationCondition(notMetCondition)
212-
.addDependentResource(dd4).dependsOn(dd2, dd3)
212+
.configuring(dd4).dependsOn(dd2, dd3)
213213
.build();
214214

215215
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -225,11 +225,11 @@ void ifDependentInactiveDeleteIsNotCalled() {
225225
void ifDependentInactiveDeletePostConditionNotChecked() {
226226
var workflow = new WorkflowBuilder<TestCustomResource>()
227227
.addDependentResource(dd1)
228-
.addDependentResource(dd2).dependsOn(dd1)
229-
.addDependentResource(dd3).dependsOn(dd1)
228+
.configuring(dd2).dependsOn(dd1)
229+
.configuring(dd3).dependsOn(dd1)
230230
.withDeletePostcondition(notMetCondition)
231231
.withActivationCondition(notMetCondition)
232-
.addDependentResource(dd4).dependsOn(dd2, dd3)
232+
.configuring(dd4).dependsOn(dd2, dd3)
233233
.build();
234234

235235
var res = workflow.cleanup(new TestCustomResource(), mockContext);
@@ -243,7 +243,7 @@ void ifDependentInactiveDeletePostConditionNotChecked() {
243243
@Test
244244
void singleInactiveDependent() {
245245
var workflow = new WorkflowBuilder<TestCustomResource>()
246-
.addDependentResource(dd1)
246+
.configuring(dd1)
247247
.withActivationCondition(notMetCondition)
248248
.build();
249249

0 commit comments

Comments
 (0)