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 ;
8
6
9
- import io .javaoperatorsdk .operator .OperatorException ;
10
7
import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
11
8
import io .javaoperatorsdk .operator .api .reconciler .dependent .ReconcileResult ;
12
9
17
14
@ SuppressWarnings ("rawtypes" )
18
15
public class ManagedDependentResourceContext {
19
16
20
- private final Map <String , DependentResourceInfo > dependentResources ;
17
+ private final Map <String , ReconcileResult > reconcileResults = new ConcurrentHashMap <>() ;
21
18
private final ConcurrentHashMap attributes = new ConcurrentHashMap ();
22
19
23
20
/**
@@ -65,26 +62,13 @@ public Optional put(Object key, Object value) {
65
62
* @return the contextual object value associated with the specified key
66
63
* @see #get(Object, Class)
67
64
*/
65
+ @ SuppressWarnings ("unused" )
68
66
public <T > T getMandatory (Object key , Class <T > expectedType ) {
69
67
return get (key , expectedType ).orElseThrow (() -> new IllegalStateException (
70
68
"Mandatory attribute (key: " + key + ", type: " + expectedType .getName ()
71
69
+ ") is missing or not of the expected type" ));
72
70
}
73
71
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
-
88
72
/**
89
73
* Retrieve the {@link ReconcileResult}, if it exists, associated with the
90
74
* {@link DependentResource} associated with the specified name
@@ -94,9 +78,9 @@ private DependentResourceInfo getDependentResource(String name) {
94
78
* @return an Optional containing the reconcile result or {@link Optional#empty()} if no such
95
79
* result is available
96
80
*/
97
- @ SuppressWarnings ("rawtypes" )
81
+ @ SuppressWarnings ({ "rawtypes" , "unused" } )
98
82
public Optional <ReconcileResult > getReconcileResult (String name ) {
99
- return Optional .of ( getDependentResource (name )). map ( DependentResourceInfo :: result );
83
+ return Optional .ofNullable ( reconcileResults . get (name ));
100
84
}
101
85
102
86
/**
@@ -108,26 +92,6 @@ public Optional<ReconcileResult> getReconcileResult(String name) {
108
92
* {@link DependentResource}
109
93
*/
110
94
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 );
132
96
}
133
97
}
0 commit comments