Skip to content

Commit 3610ebd

Browse files
committed
fix: do not use Optional in Condition, fix generics
1 parent a73174b commit 3610ebd

File tree

8 files changed

+45
-40
lines changed

8 files changed

+45
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

3-
import java.util.Optional;
4-
53
import io.fabric8.kubernetes.api.model.HasMetadata;
64
import io.javaoperatorsdk.operator.api.reconciler.Context;
75

86
public interface Condition<R, P extends HasMetadata> {
97

10-
boolean isMet(P primary, Optional<R> secondary, Context<P> context);
8+
/**
9+
* Checks whether a condition holds true for a given
10+
* {@link io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource} based on the
11+
* observed cluster state.
12+
*
13+
* @param primary the primary resource being considered
14+
* @param secondary the secondary resource associated with the specified primary resource or
15+
* {@code null} if no such secondary resource exists (for example because it's been
16+
* deleted)
17+
* @param context the current reconciliation {@link Context}
18+
* @return {@code true} if the condition holds, {@code false} otherwise
19+
*/
20+
boolean isMet(P primary, R secondary, Context<P> context);
1121
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
public class DependentResourceNode<R, P extends HasMetadata> {
1212

1313
private final DependentResource<R, P> dependentResource;
14-
private Condition reconcilePrecondition;
15-
private Condition deletePostcondition;
16-
private Condition readyPostcondition;
14+
private Condition<R, P> reconcilePrecondition;
15+
private Condition<R, P> deletePostcondition;
16+
private Condition<R, P> readyPostcondition;
1717
private final List<DependentResourceNode> dependsOn = new LinkedList<>();
1818
private final List<DependentResourceNode> parents = new LinkedList<>();
1919

@@ -22,12 +22,12 @@ public DependentResourceNode(DependentResource<R, P> dependentResource) {
2222
}
2323

2424
public DependentResourceNode(DependentResource<R, P> dependentResource,
25-
Condition reconcilePrecondition) {
25+
Condition<R, P> reconcilePrecondition) {
2626
this(dependentResource, reconcilePrecondition, null);
2727
}
2828

2929
public DependentResourceNode(DependentResource<R, P> dependentResource,
30-
Condition reconcilePrecondition, Condition deletePostcondition) {
30+
Condition<R, P> reconcilePrecondition, Condition<R, P> deletePostcondition) {
3131
this.dependentResource = dependentResource;
3232
this.reconcilePrecondition = reconcilePrecondition;
3333
this.deletePostcondition = deletePostcondition;
@@ -37,11 +37,11 @@ public DependentResource<R, P> getDependentResource() {
3737
return dependentResource;
3838
}
3939

40-
public Optional<Condition> getReconcilePrecondition() {
40+
public Optional<Condition<R, P>> getReconcilePrecondition() {
4141
return Optional.ofNullable(reconcilePrecondition);
4242
}
4343

44-
public Optional<Condition> getDeletePostcondition() {
44+
public Optional<Condition<R, P>> getDeletePostcondition() {
4545
return Optional.ofNullable(deletePostcondition);
4646
}
4747

@@ -63,12 +63,12 @@ public String toString() {
6363
}
6464

6565
public DependentResourceNode<R, P> setReconcilePrecondition(
66-
Condition reconcilePrecondition) {
66+
Condition<R, P> reconcilePrecondition) {
6767
this.reconcilePrecondition = reconcilePrecondition;
6868
return this;
6969
}
7070

71-
public DependentResourceNode<R, P> setDeletePostcondition(Condition cleanupCondition) {
71+
public DependentResourceNode<R, P> setDeletePostcondition(Condition<R, P> cleanupCondition) {
7272
this.deletePostcondition = cleanupCondition;
7373
return this;
7474
}
@@ -77,7 +77,7 @@ public Optional<Condition<R, P>> getReadyPostcondition() {
7777
return Optional.ofNullable(readyPostcondition);
7878
}
7979

80-
public DependentResourceNode<R, P> setReadyPostcondition(Condition readyPostcondition) {
80+
public DependentResourceNode<R, P> setReadyPostcondition(Condition<R, P> readyPostcondition) {
8181
this.readyPostcondition = readyPostcondition;
8282
return this;
8383
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.HashMap;
44
import java.util.HashSet;
55
import java.util.Map;
6-
import java.util.Optional;
76
import java.util.Set;
87
import java.util.concurrent.ConcurrentHashMap;
98
import java.util.concurrent.Future;
@@ -72,7 +71,7 @@ public synchronized WorkflowReconcileResult reconcile() {
7271
return createReconcileResult();
7372
}
7473

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

7877
if (alreadyVisited(dependentResourceNode)
@@ -86,7 +85,8 @@ private synchronized void handleReconcile(DependentResourceNode<?, P> dependentR
8685

8786
boolean reconcileConditionMet = dependentResourceNode.getReconcilePrecondition()
8887
.map(rc -> rc.isMet(primary,
89-
dependentResourceNode.getDependentResource().getSecondaryResource(primary), context))
88+
dependentResourceNode.getDependentResource().getSecondaryResource(primary).orElse(null),
89+
context))
9090
.orElse(true);
9191

9292
if (!reconcileConditionMet) {
@@ -144,9 +144,9 @@ private synchronized void setAlreadyReconciledButNotReady(
144144
notReady.add(dependentResourceNode);
145145
}
146146

147-
private class NodeReconcileExecutor implements Runnable {
147+
private class NodeReconcileExecutor<R> implements Runnable {
148148

149-
private final DependentResourceNode dependentResourceNode;
149+
private final DependentResourceNode<R, P> dependentResourceNode;
150150

151151
private NodeReconcileExecutor(DependentResourceNode dependentResourceNode) {
152152
this.dependentResourceNode = dependentResourceNode;
@@ -166,9 +166,10 @@ public void run() {
166166
ReconcileResult reconcileResult = dependentResource.reconcile(primary, context);
167167
reconcileResults.put(dependentResource, reconcileResult);
168168
reconciled.add(dependentResourceNode);
169-
boolean ready = ((Optional<Condition>) dependentResourceNode.getReadyPostcondition())
169+
boolean ready = dependentResourceNode.getReadyPostcondition()
170170
.map(rc -> rc.isMet(primary,
171-
dependentResourceNode.getDependentResource().getSecondaryResource(primary),
171+
dependentResourceNode.getDependentResource().getSecondaryResource(primary)
172+
.orElse(null),
172173
context))
173174
.orElse(true);
174175

@@ -187,11 +188,11 @@ public void run() {
187188
}
188189
}
189190

190-
private class NodeDeleteExecutor implements Runnable {
191+
private class NodeDeleteExecutor<R> implements Runnable {
191192

192-
private final DependentResourceNode<?, P> dependentResourceNode;
193+
private final DependentResourceNode<R, P> dependentResourceNode;
193194

194-
private NodeDeleteExecutor(DependentResourceNode<?, P> dependentResourceNode) {
195+
private NodeDeleteExecutor(DependentResourceNode<R, P> dependentResourceNode) {
195196
this.dependentResourceNode = dependentResourceNode;
196197
}
197198

@@ -209,7 +210,8 @@ public void run() {
209210
alreadyVisited.add(dependentResourceNode);
210211
boolean deletePostConditionMet =
211212
deletePostCondition.map(c -> c.isMet(primary,
212-
dependentResourceNode.getDependentResource().getSecondaryResource(primary),
213+
dependentResourceNode.getDependentResource().getSecondaryResource(primary)
214+
.orElse(null),
213215
context)).orElse(true);
214216
if (deletePostConditionMet) {
215217
handleDependentDeleted(dependentResourceNode);

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap>
342342
private static class TestCondition implements Condition<ConfigMap, ConfigMap> {
343343

344344
@Override
345-
public boolean isMet(ConfigMap primary, Optional<ConfigMap> secondary,
346-
Context<ConfigMap> context) {
345+
public boolean isMet(ConfigMap primary, ConfigMap secondary, Context<ConfigMap> context) {
347346
return true;
348347
}
349348
}

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

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

3-
import java.util.Optional;
4-
53
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
75

@@ -16,9 +14,9 @@ public class ConfigMapDeletePostCondition
1614

1715
@Override
1816
public boolean isMet(
19-
WorkflowAllFeatureCustomResource primary, Optional<ConfigMap> secondary,
17+
WorkflowAllFeatureCustomResource primary, ConfigMap secondary,
2018
Context<WorkflowAllFeatureCustomResource> context) {
21-
var configMapDeleted = secondary.isEmpty();
19+
var configMapDeleted = secondary == null;
2220
log.debug("Config Map Deleted: {}", configMapDeleted);
2321
return configMapDeleted;
2422
}

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

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

3-
import java.util.Optional;
4-
53
import io.fabric8.kubernetes.api.model.ConfigMap;
64
import io.javaoperatorsdk.operator.api.reconciler.Context;
75
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
@@ -10,7 +8,7 @@ public class ConfigMapReconcileCondition
108
implements Condition<ConfigMap, WorkflowAllFeatureCustomResource> {
119

1210
@Override
13-
public boolean isMet(WorkflowAllFeatureCustomResource primary, Optional<ConfigMap> secondary,
11+
public boolean isMet(WorkflowAllFeatureCustomResource primary, ConfigMap secondary,
1412
Context<WorkflowAllFeatureCustomResource> context) {
1513
return primary.getSpec().isCreateConfigMap();
1614
}

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

Lines changed: 4 additions & 4 deletions
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-
53
import io.fabric8.kubernetes.api.model.apps.Deployment;
64
import io.javaoperatorsdk.operator.api.reconciler.Context;
75
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
86

97
public class DeploymentReadyCondition
108
implements Condition<Deployment, WorkflowAllFeatureCustomResource> {
119
@Override
12-
public boolean isMet(WorkflowAllFeatureCustomResource primary, Optional<Deployment> secondary,
10+
public boolean isMet(WorkflowAllFeatureCustomResource primary, Deployment deployment,
1311
Context<WorkflowAllFeatureCustomResource> context) {
14-
var deployment = secondary.orElseThrow();
12+
if (deployment == null) {
13+
return false;
14+
}
1515
var readyReplicas = deployment.getStatus().getReadyReplicas();
1616

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

3-
import java.util.Optional;
4-
53
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
64
import io.javaoperatorsdk.operator.api.reconciler.Context;
75
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
86

97
public class ExposedIngressCondition implements Condition<Ingress, WebPage> {
108
@Override
11-
public boolean isMet(WebPage primary, Optional<Ingress> secondary, Context<WebPage> context) {
9+
public boolean isMet(WebPage primary, Ingress secondary, Context<WebPage> context) {
1210
return primary.getSpec().getExposed();
1311
}
1412
}

0 commit comments

Comments
 (0)