Skip to content

Commit a8d71f4

Browse files
committed
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 da84cd3 commit a8d71f4

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
@@ -16,50 +16,25 @@ public class WorkflowBuilder<P extends HasMetadata> {
1616

1717
private final Map<String, DependentResourceNode<?, P>> dependentResourceNodes = new HashMap<>();
1818
private boolean throwExceptionAutomatically = THROW_EXCEPTION_AUTOMATICALLY_DEFAULT;
19-
private DependentResourceNode currentNode;
2019
private boolean isCleaner = false;
2120

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

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

60-
public WorkflowBuilder<P> withActivationCondition(Condition activationCondition) {
61-
currentNode.setActivationCondition(activationCondition);
62-
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;
6338
}
6439

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

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)