Skip to content

Commit f4be7ef

Browse files
committed
refactor: remove DependentResource access from managed context
1 parent a400028 commit f4be7ef

File tree

4 files changed

+8
-44
lines changed

4 files changed

+8
-44
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public DefaultContext(RetryInfo retryInfo, Controller<P> controller, P primaryRe
2020
this.controller = controller;
2121
this.primaryResource = primaryResource;
2222
this.controllerConfiguration = controller.getConfiguration();
23-
this.managedDependentResourceContext = new ManagedDependentResourceContext(
24-
controller.getDependents());
23+
this.managedDependentResourceContext = new ManagedDependentResourceContext();
2524
}
2625

2726
@Override

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

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
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;
86

9-
import io.javaoperatorsdk.operator.OperatorException;
107
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
118
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
129

@@ -17,7 +14,7 @@
1714
@SuppressWarnings("rawtypes")
1815
public class ManagedDependentResourceContext {
1916

20-
private final Map<String, DependentResourceInfo> dependentResources;
17+
private final Map<String, ReconcileResult> reconcileResults = new ConcurrentHashMap<>();
2118
private final ConcurrentHashMap attributes = new ConcurrentHashMap();
2219

2320
/**
@@ -65,26 +62,13 @@ public Optional put(Object key, Object value) {
6562
* @return the contextual object value associated with the specified key
6663
* @see #get(Object, Class)
6764
*/
65+
@SuppressWarnings("unused")
6866
public <T> T getMandatory(Object key, Class<T> expectedType) {
6967
return get(key, expectedType).orElseThrow(() -> new IllegalStateException(
7068
"Mandatory attribute (key: " + key + ", type: " + expectedType.getName()
7169
+ ") is missing or not of the expected type"));
7270
}
7371

74-
public ManagedDependentResourceContext(Map<String, DependentResource> 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;
86-
}
87-
8872
/**
8973
* Retrieve the {@link ReconcileResult}, if it exists, associated with the
9074
* {@link DependentResource} associated with the specified name
@@ -94,9 +78,9 @@ private DependentResourceInfo getDependentResource(String name) {
9478
* @return an Optional containing the reconcile result or {@link Optional#empty()} if no such
9579
* result is available
9680
*/
97-
@SuppressWarnings("rawtypes")
81+
@SuppressWarnings({"rawtypes", "unused"})
9882
public Optional<ReconcileResult> getReconcileResult(String name) {
99-
return Optional.of(getDependentResource(name)).map(DependentResourceInfo::result);
83+
return Optional.ofNullable(reconcileResults.get(name));
10084
}
10185

10286
/**
@@ -108,26 +92,6 @@ public Optional<ReconcileResult> getReconcileResult(String name) {
10892
* {@link DependentResource}
10993
*/
11094
public void setReconcileResult(String name, ReconcileResult reconcileResult) {
111-
getDependentResource(name).result = reconcileResult;
112-
}
113-
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;
123-
}
124-
125-
private String name() {
126-
return name;
127-
}
128-
129-
public ReconcileResult result() {
130-
return result;
131-
}
95+
reconcileResults.put(name, reconcileResult);
13296
}
13397
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import io.fabric8.kubernetes.api.model.HasMetadata;
77
import io.javaoperatorsdk.operator.api.reconciler.Context;
8+
import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter;
89
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
910
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceProvider;
1011
import io.javaoperatorsdk.operator.api.reconciler.dependent.RecentOperationCacheFiller;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void replaceNamedDependentResourceConfigShouldWork() {
4444
final var overridden = ControllerConfigurationOverrider.override(configuration)
4545
.replacingNamedDependentResourceConfig(
4646
DependentResource.defaultNameFor(ReadOnlyDependent.class),
47-
new KubernetesDependentResourceConfig(true, new String[] {overriddenNS}, labelSelector))
47+
new KubernetesDependentResourceConfig(new String[] {overriddenNS}, labelSelector))
4848
.build();
4949
dependents = overridden.getDependentResources();
5050
dependentSpec = dependents.get(dependentResourceName);

0 commit comments

Comments
 (0)