Skip to content

Commit 7f82f54

Browse files
committed
feat: record ReconcileResult per DependentResource in context
Fixes #957
1 parent 1e87faf commit 7f82f54

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

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

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import java.util.Map;
44
import java.util.Optional;
55
import java.util.concurrent.ConcurrentHashMap;
6+
import java.util.function.Function;
7+
import java.util.stream.Collectors;
68

79
import io.javaoperatorsdk.operator.OperatorException;
810
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
11+
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
912

1013
/**
1114
* Contextual information related to {@link DependentResource} either to retrieve the actual
@@ -14,7 +17,7 @@
1417
@SuppressWarnings("rawtypes")
1518
public class ManagedDependentResourceContext {
1619

17-
private final Map<String, DependentResource> dependentResources;
20+
private final Map<String, DependentResourceInfo> dependentResources;
1821
private final ConcurrentHashMap attributes = new ConcurrentHashMap();
1922

2023
/**
@@ -69,58 +72,62 @@ public <T> T getMandatory(Object key, Class<T> expectedType) {
6972
}
7073

7174
public ManagedDependentResourceContext(Map<String, DependentResource> dependentResources) {
72-
this.dependentResources = dependentResources;
75+
this.dependentResources = dependentResources.entrySet().stream()
76+
.map(entry -> new DependentResourceInfo(entry.getKey(), entry.getValue()))
77+
.collect(Collectors.toMap(DependentResourceInfo::name, Function.identity()));
78+
}
79+
80+
private DependentResourceInfo getDependentResource(String name) {
81+
var dependentResource = dependentResources.get(name);
82+
if (dependentResource == null) {
83+
throw new OperatorException("No dependent resource found with name: " + name);
84+
}
85+
return dependentResource;
7386
}
7487

7588
/**
76-
* Retrieve all the known {@link DependentResource} implementations
89+
* Retrieve the {@link ReconcileResult}, if it exists, associated with the
90+
* {@link DependentResource} associated with the specified name
7791
*
78-
* @return known {@link DependentResource} implementations keyed by their name
92+
* @param name the name of the {@link DependentResource} for which we want to retrieve a
93+
* {@link ReconcileResult}
94+
* @return an Optional containing the reconcile result or {@link Optional#empty()} if no such
95+
* result is available
7996
*/
80-
public Map<String, DependentResource> getDependentResources() {
81-
return dependentResources;
97+
@SuppressWarnings("rawtypes")
98+
public Optional<ReconcileResult> getReconcileResult(String name) {
99+
return Optional.of(getDependentResource(name)).map(DependentResourceInfo::result);
82100
}
83101

84102
/**
85-
* Retrieve the dependent resource implementation associated with the specified resource type.
103+
* Set the {@link ReconcileResult} for the specified {@link DependentResource} implementation.
86104
*
87-
* @param name the name of the {@link DependentResource} implementation we want to retrieve
88-
* @param resourceClass the resource class for which we want to retrieve the associated dependent
89-
* resource implementation
90-
* @param <T> the type of the resources for which we want to retrieve the associated dependent
91-
* resource implementation
92-
* @return the associated dependent resource implementation if it exists or an exception if it
93-
* doesn't or several implementations are associated with the specified resource type
105+
* @param name the name of the {@link DependentResource} for which we want to set the
106+
* {@link ReconcileResult}
107+
* @param reconcileResult the reconcile result associated with the specified
108+
* {@link DependentResource}
94109
*/
95-
@SuppressWarnings({"unchecked"})
96-
public <T> DependentResource<T, ?> getDependentResource(String name, Class<T> resourceClass) {
97-
DependentResource dependentResource = getDependentResource(name);
98-
final var actual = dependentResource.resourceType();
99-
if (!actual.equals(resourceClass)) {
100-
throw new OperatorException(
101-
"Dependent resource implementation doesn't match expected resource type, was: "
102-
+ actual.getName() + " instead of: " + resourceClass.getName());
103-
}
104-
return dependentResource;
110+
public void setReconcileResult(String name, ReconcileResult reconcileResult) {
111+
getDependentResource(name).result = reconcileResult;
105112
}
106113

107-
private DependentResource getDependentResource(String name) {
108-
var dependentResource = dependentResources.get(name);
109-
if (dependentResource == null) {
110-
throw new OperatorException("No dependent resource found with name: " + name);
114+
private static class DependentResourceInfo {
115+
private final String name;
116+
private final DependentResource dependent;
117+
private ReconcileResult result;
118+
119+
120+
private DependentResourceInfo(String name, DependentResource dependent) {
121+
this.name = name;
122+
this.dependent = dependent;
111123
}
112-
return dependentResource;
113-
}
114124

115-
@SuppressWarnings("unchecked")
116-
public <T extends DependentResource> T getAdaptedDependentResource(String name,
117-
Class<T> dependentResourceClass) {
118-
DependentResource dependentResource = getDependentResource(name);
119-
if (dependentResourceClass.isInstance(dependentResource)) {
120-
return (T) dependentResource;
121-
} else {
122-
throw new IllegalArgumentException("Dependent resource associated with name: " + name
123-
+ " is not adaptable to type: " + dependentResourceClass.getCanonicalName());
125+
private String name() {
126+
return name;
127+
}
128+
129+
public ReconcileResult result() {
130+
return result;
124131
}
125132
}
126133
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/Controller.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,23 @@ public String successTypeName(UpdateControl<P> result) {
187187
@Override
188188
public UpdateControl<P> execute() throws Exception {
189189
initContextIfNeeded(resource, context);
190-
final var result = dependents.values().stream()
191-
.map(dependent -> {
190+
final var result = dependents.entrySet().stream()
191+
.map(entry -> {
192+
final var dependent = entry.getValue();
193+
final var name = entry.getKey();
194+
ReconcileResult reconcileResult;
192195
try {
193-
return dependent.reconcile(resource, context);
196+
reconcileResult = dependent.reconcile(resource, context);
194197
} catch (Exception e) {
195-
return ReconcileResult.error(resource, e);
198+
reconcileResult = ReconcileResult.error(resource, e);
196199
}
200+
context.managedDependentResourceContext().setReconcileResult(name,
201+
reconcileResult);
202+
return reconcileResult;
197203
});
198204

199205
log.info("Dependents reconciliation:\n{}",
200-
result
201-
.map(r -> "\t" + r.toString())
202-
.collect(Collectors.joining("\n")));
206+
result.map(r -> "\t" + r.toString()).collect(Collectors.joining("\n")));
203207
return reconciler.reconcile(resource, context);
204208
}
205209
});

0 commit comments

Comments
 (0)