Skip to content

Commit cd87a31

Browse files
committed
refactor: make it easier to create Conditions
1 parent a4f8730 commit cd87a31

File tree

12 files changed

+64
-76
lines changed

12 files changed

+64
-76
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AnnotationControllerConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
2323
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
2424
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
25-
import io.javaoperatorsdk.operator.api.reconciler.dependent.VoidCondition;
2625
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
2726
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
2827
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
@@ -276,7 +275,7 @@ public List<DependentResourceSpec> getDependentResources() {
276275
}
277276

278277
private Condition<?, ?> instantiateConditionIfNotVoid(Class<? extends Condition> condition) {
279-
if (condition != VoidCondition.class) {
278+
if (condition != Condition.class) {
280279
try {
281280
return condition.getDeclaredConstructor().newInstance();
282281
} catch (InstantiationException

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/Dependent.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111

1212
String name() default NO_VALUE_SET;
1313

14-
@SuppressWarnings("rawtypes")
15-
Class<? extends Condition> readyPostcondition() default VoidCondition.class;
14+
Class<? extends Condition> readyPostcondition() default Condition.class;
1615

17-
@SuppressWarnings("rawtypes")
18-
Class<? extends Condition> reconcilePrecondition() default VoidCondition.class;
16+
Class<? extends Condition> reconcilePrecondition() default Condition.class;
1917

20-
@SuppressWarnings("rawtypes")
21-
Class<? extends Condition> deletePostcondition() default VoidCondition.class;
18+
Class<? extends Condition> deletePostcondition() default Condition.class;
2219

2320
String[] dependsOn() default {};
2421
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/VoidCondition.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

3+
import java.util.Optional;
4+
35
import io.fabric8.kubernetes.api.model.HasMetadata;
46
import io.javaoperatorsdk.operator.api.reconciler.Context;
5-
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
67

78
public interface Condition<R, P extends HasMetadata> {
89

9-
boolean isMet(DependentResource<R, P> dependentResource, P primary, Context<P> context);
10+
boolean isMet(P primary, Optional<R> secondary, Context<P> context);
1011
}

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

3+
import java.util.List;
34
import java.util.Map;
5+
import java.util.Optional;
46
import java.util.Set;
57
import java.util.concurrent.ConcurrentHashMap;
68
import java.util.concurrent.Future;
@@ -75,17 +77,16 @@ private synchronized void handleCleanup(DependentResourceNode dependentResourceN
7577
}
7678

7779
Future<?> nodeFuture =
78-
workflow.getExecutorService().submit(
79-
new NodeExecutor(dependentResourceNode));
80+
workflow.getExecutorService().submit(new NodeExecutor(dependentResourceNode));
8081
actualExecutions.put(dependentResourceNode, nodeFuture);
8182
log.debug("Submitted for cleanup: {}", dependentResourceNode);
8283
}
8384

8485
private class NodeExecutor implements Runnable {
8586

86-
private final DependentResourceNode<?, P> dependentResourceNode;
87+
private final DependentResourceNode dependentResourceNode;
8788

88-
private NodeExecutor(DependentResourceNode<?, P> dependentResourceNode) {
89+
private NodeExecutor(DependentResourceNode dependentResourceNode) {
8990
this.dependentResourceNode = dependentResourceNode;
9091
}
9192

@@ -94,7 +95,7 @@ private NodeExecutor(DependentResourceNode<?, P> dependentResourceNode) {
9495
public void run() {
9596
try {
9697
var dependentResource = dependentResourceNode.getDependentResource();
97-
var deletePostCondition = dependentResourceNode.getDeletePostcondition();
98+
Optional<Condition> deletePostCondition = dependentResourceNode.getDeletePostcondition();
9899

99100
if (dependentResource instanceof Deleter
100101
&& !(dependentResource instanceof GarbageCollected)) {
@@ -103,7 +104,9 @@ public void run() {
103104
}
104105
alreadyVisited.add(dependentResourceNode);
105106
boolean deletePostConditionMet =
106-
deletePostCondition.map(c -> c.isMet(dependentResource, primary, context)).orElse(true);
107+
deletePostCondition.map(c -> c.isMet(primary,
108+
dependentResourceNode.getDependentResource().getSecondaryResource(primary),
109+
context)).orElse(true);
107110
if (deletePostConditionMet) {
108111
handleDependentCleaned(dependentResourceNode);
109112
} else {
@@ -147,22 +150,21 @@ private boolean isCleaningNow(DependentResourceNode<?, ?> dependentResourceNode)
147150
return actualExecutions.containsKey(dependentResourceNode);
148151
}
149152

150-
private boolean alreadyVisited(
151-
DependentResourceNode<?, ?> dependentResourceNode) {
153+
private boolean alreadyVisited(DependentResourceNode dependentResourceNode) {
152154
return alreadyVisited.contains(dependentResourceNode);
153155
}
154156

155-
private boolean allDependentsCleaned(
156-
DependentResourceNode<?, P> dependentResourceNode) {
157-
var parents = dependentResourceNode.getParents();
157+
@SuppressWarnings("unchecked")
158+
private boolean allDependentsCleaned(DependentResourceNode dependentResourceNode) {
159+
List<DependentResourceNode> parents = dependentResourceNode.getParents();
158160
return parents.isEmpty()
159161
|| parents.stream()
160162
.allMatch(d -> alreadyVisited(d) && !postDeleteConditionNotMet.contains(d));
161163
}
162164

163-
private boolean hasErroredDependent(
164-
DependentResourceNode<?, P> dependentResourceNode) {
165-
var parents = dependentResourceNode.getParents();
165+
@SuppressWarnings("unchecked")
166+
private boolean hasErroredDependent(DependentResourceNode dependentResourceNode) {
167+
List<DependentResourceNode> parents = dependentResourceNode.getParents();
166168
return !parents.isEmpty()
167169
&& parents.stream().anyMatch(exceptionsDuringExecution::containsKey);
168170
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.HashMap;
44
import java.util.HashSet;
55
import java.util.Map;
6+
import java.util.Optional;
67
import java.util.Set;
78
import java.util.concurrent.ConcurrentHashMap;
89
import java.util.concurrent.Future;
@@ -71,8 +72,7 @@ public synchronized WorkflowReconcileResult reconcile() {
7172
return createReconcileResult();
7273
}
7374

74-
private synchronized void handleReconcile(
75-
DependentResourceNode<?, P> dependentResourceNode) {
75+
private synchronized void handleReconcile(DependentResourceNode<?, P> dependentResourceNode) {
7676
log.debug("Submitting for reconcile: {}", dependentResourceNode);
7777

7878
if (alreadyVisited(dependentResourceNode)
@@ -84,8 +84,9 @@ private synchronized void handleReconcile(
8484
return;
8585
}
8686

87-
boolean reconcileConditionMet = dependentResourceNode.getReconcilePrecondition().map(
88-
rc -> rc.isMet(dependentResourceNode.getDependentResource(), primary, context))
87+
boolean reconcileConditionMet = dependentResourceNode.getReconcilePrecondition()
88+
.map(rc -> rc.isMet(primary,
89+
dependentResourceNode.getDependentResource().getSecondaryResource(primary), context))
8990
.orElse(true);
9091

9192
if (!reconcileConditionMet) {
@@ -145,9 +146,9 @@ private synchronized void setAlreadyReconciledButNotReady(
145146

146147
private class NodeReconcileExecutor implements Runnable {
147148

148-
private final DependentResourceNode<?, P> dependentResourceNode;
149+
private final DependentResourceNode dependentResourceNode;
149150

150-
private NodeReconcileExecutor(DependentResourceNode<?, P> dependentResourceNode) {
151+
private NodeReconcileExecutor(DependentResourceNode dependentResourceNode) {
151152
this.dependentResourceNode = dependentResourceNode;
152153
}
153154

@@ -165,8 +166,10 @@ public void run() {
165166
ReconcileResult reconcileResult = dependentResource.reconcile(primary, context);
166167
reconcileResults.put(dependentResource, reconcileResult);
167168
reconciled.add(dependentResourceNode);
168-
boolean ready = dependentResourceNode.getReadyPostcondition()
169-
.map(rc -> rc.isMet(dependentResource, primary, context))
169+
boolean ready = ((Optional<Condition>) dependentResourceNode.getReadyPostcondition())
170+
.map(rc -> rc.isMet(primary,
171+
dependentResourceNode.getDependentResource().getSecondaryResource(primary),
172+
context))
170173
.orElse(true);
171174

172175
if (ready) {
@@ -205,7 +208,9 @@ public void run() {
205208
}
206209
alreadyVisited.add(dependentResourceNode);
207210
boolean deletePostConditionMet =
208-
deletePostCondition.map(c -> c.isMet(dependentResource, primary, context)).orElse(true);
211+
deletePostCondition.map(c -> c.isMet(primary,
212+
dependentResourceNode.getDependentResource().getSecondaryResource(primary),
213+
context)).orElse(true);
209214
if (deletePostConditionMet) {
210215
handleDependentDeleted(dependentResourceNode);
211216
} else {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ public class AbstractWorkflowExecutorTest {
2121
protected TestErrorDependent drError = new TestErrorDependent("ERROR_1");
2222
protected TestErrorDeleterDependent errorDD = new TestErrorDeleterDependent("ERROR_DELETER");
2323

24-
protected final Condition noMetDeletePostCondition =
25-
(dependentResource, primary, context) -> false;
26-
protected final Condition metDeletePostCondition =
27-
(dependentResource, primary, context) -> true;
24+
@SuppressWarnings("rawtypes")
25+
protected final Condition noMetDeletePostCondition = (primary, secondary, context) -> false;
26+
@SuppressWarnings("rawtypes")
27+
protected final Condition metDeletePostCondition = (primary, secondary, context) -> true;
2828

2929
protected List<ReconcileRecord> executionHistory =
3030
Collections.synchronizedList(new ArrayList<>());
3131

3232
public class TestDependent implements DependentResource<String, TestCustomResource> {
3333

34-
private String name;
34+
private final String name;
3535

3636
public TestDependent(String name) {
3737
this.name = name;
@@ -95,7 +95,7 @@ public void delete(TestCustomResource primary, Context<TestCustomResource> conte
9595
}
9696

9797
public class TestErrorDependent implements DependentResource<String, TestCustomResource> {
98-
private String name;
98+
private final String name;
9999

100100
public TestErrorDependent(String name) {
101101
this.name = name;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
@SuppressWarnings("rawtypes")
1414
class WorkflowReconcileExecutorTest extends AbstractWorkflowExecutorTest {
1515

16-
private final Condition met_reconcile_condition =
17-
(dependentResource, primary, context) -> true;
18-
private final Condition not_met_reconcile_condition =
19-
(dependentResource, primary, context) -> false;
16+
private final Condition met_reconcile_condition = (primary, secondary, context) -> true;
17+
private final Condition not_met_reconcile_condition = (primary, secondary, context) -> false;
2018

2119
private final Condition<String, TestCustomResource> metReadyCondition =
22-
(dependentResource, primary, context) -> true;
20+
(primary, secondary, context) -> true;
2321
private final Condition<String, TestCustomResource> notMetReadyCondition =
24-
(dependentResource, primary, context) -> false;
22+
(primary, secondary, context) -> false;
2523

2624
@Test
2725
void reconcileTopLevelResources() {

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/workflowallfeature/ConfigMapDeletePostCondition.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.javaoperatorsdk.operator.sample.workflowallfeature;
22

3+
import java.util.Optional;
4+
35
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
57

68
import io.fabric8.kubernetes.api.model.ConfigMap;
79
import io.javaoperatorsdk.operator.api.reconciler.Context;
8-
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
910
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
1011

1112
public class ConfigMapDeletePostCondition
@@ -15,9 +16,9 @@ public class ConfigMapDeletePostCondition
1516

1617
@Override
1718
public boolean isMet(
18-
DependentResource<ConfigMap, WorkflowAllFeatureCustomResource> dependentResource,
19-
WorkflowAllFeatureCustomResource primary, Context<WorkflowAllFeatureCustomResource> context) {
20-
var configMapDeleted = dependentResource.getSecondaryResource(primary).isEmpty();
19+
WorkflowAllFeatureCustomResource primary, Optional<ConfigMap> secondary,
20+
Context<WorkflowAllFeatureCustomResource> context) {
21+
var configMapDeleted = secondary.isEmpty();
2122
log.debug("Config Map Deleted: {}", configMapDeleted);
2223
return configMapDeleted;
2324
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package io.javaoperatorsdk.operator.sample.workflowallfeature;
22

3+
import java.util.Optional;
4+
35
import io.fabric8.kubernetes.api.model.ConfigMap;
46
import io.javaoperatorsdk.operator.api.reconciler.Context;
5-
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
67
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
78

89
public class ConfigMapReconcileCondition
910
implements Condition<ConfigMap, WorkflowAllFeatureCustomResource> {
1011

1112
@Override
12-
public boolean isMet(
13-
DependentResource<ConfigMap, WorkflowAllFeatureCustomResource> dependentResource,
14-
WorkflowAllFeatureCustomResource primary, Context<WorkflowAllFeatureCustomResource> context) {
13+
public boolean isMet(WorkflowAllFeatureCustomResource primary, Optional<ConfigMap> secondary,
14+
Context<WorkflowAllFeatureCustomResource> context) {
1515
return primary.getSpec().isCreateConfigMap();
1616
}
1717
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/workflowallfeature/DeploymentReadyCondition.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.javaoperatorsdk.operator.sample.workflowallfeature;
22

3+
import java.util.Optional;
4+
35
import io.fabric8.kubernetes.api.model.apps.Deployment;
46
import io.javaoperatorsdk.operator.api.reconciler.Context;
5-
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
67
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
78

89
public class DeploymentReadyCondition
910
implements Condition<Deployment, WorkflowAllFeatureCustomResource> {
1011
@Override
11-
public boolean isMet(
12-
DependentResource<Deployment, WorkflowAllFeatureCustomResource> dependentResource,
13-
WorkflowAllFeatureCustomResource primary, Context<WorkflowAllFeatureCustomResource> context) {
14-
15-
var deployment = dependentResource.getSecondaryResource(primary).orElseThrow();
12+
public boolean isMet(WorkflowAllFeatureCustomResource primary, Optional<Deployment> secondary,
13+
Context<WorkflowAllFeatureCustomResource> context) {
14+
var deployment = secondary.orElseThrow();
1615
var readyReplicas = deployment.getStatus().getReadyReplicas();
1716

1817
return readyReplicas != null && deployment.getSpec().getReplicas().equals(readyReplicas);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package io.javaoperatorsdk.operator.sample;
22

3+
import java.util.Optional;
4+
35
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
46
import io.javaoperatorsdk.operator.api.reconciler.Context;
5-
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
67
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
78

89
public class ExposedIngressCondition implements Condition<Ingress, WebPage> {
910
@Override
10-
public boolean isMet(DependentResource<Ingress, WebPage> dependentResource, WebPage primary,
11-
Context<WebPage> context) {
11+
public boolean isMet(WebPage primary, Optional<Ingress> secondary, Context<WebPage> context) {
1212
return primary.getSpec().getExposed();
1313
}
1414
}

0 commit comments

Comments
 (0)