|
3 | 3 | import java.util.Map;
|
4 | 4 | import java.util.Optional;
|
5 | 5 | import java.util.concurrent.ConcurrentHashMap;
|
| 6 | +import java.util.function.Function; |
| 7 | +import java.util.stream.Collectors; |
6 | 8 |
|
7 | 9 | import io.javaoperatorsdk.operator.OperatorException;
|
8 | 10 | import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
|
| 11 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult; |
9 | 12 |
|
10 | 13 | /**
|
11 | 14 | * Contextual information related to {@link DependentResource} either to retrieve the actual
|
|
14 | 17 | @SuppressWarnings("rawtypes")
|
15 | 18 | public class ManagedDependentResourceContext {
|
16 | 19 |
|
17 |
| - private final Map<String, DependentResource> dependentResources; |
| 20 | + private final Map<String, DependentResourceInfo> dependentResources; |
18 | 21 | private final ConcurrentHashMap attributes = new ConcurrentHashMap();
|
19 | 22 |
|
20 | 23 | /**
|
@@ -69,58 +72,62 @@ public <T> T getMandatory(Object key, Class<T> expectedType) {
|
69 | 72 | }
|
70 | 73 |
|
71 | 74 | 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; |
73 | 86 | }
|
74 | 87 |
|
75 | 88 | /**
|
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 |
77 | 91 | *
|
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 |
79 | 96 | */
|
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); |
82 | 100 | }
|
83 | 101 |
|
84 | 102 | /**
|
85 |
| - * Retrieve the dependent resource implementation associated with the specified resource type. |
| 103 | + * Set the {@link ReconcileResult} for the specified {@link DependentResource} implementation. |
86 | 104 | *
|
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} |
94 | 109 | */
|
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; |
105 | 112 | }
|
106 | 113 |
|
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; |
111 | 123 | }
|
112 |
| - return dependentResource; |
113 |
| - } |
114 | 124 |
|
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; |
124 | 131 | }
|
125 | 132 | }
|
126 | 133 | }
|
0 commit comments