1
1
package io .javaoperatorsdk .operator .api .reconciler ;
2
2
3
+ import java .util .Map ;
3
4
import java .util .Optional ;
4
5
5
6
import io .fabric8 .kubernetes .api .model .HasMetadata ;
6
7
import io .javaoperatorsdk .operator .processing .Controller ;
8
+ import java .util .concurrent .ConcurrentHashMap ;
7
9
8
10
public class DefaultContext <P extends HasMetadata > implements Context {
9
11
10
12
private final RetryInfo retryInfo ;
11
13
private final Controller <P > controller ;
12
14
private final P primaryResource ;
15
+ private final ConcurrentHashMap attributes = new ConcurrentHashMap ();
13
16
14
17
public DefaultContext (RetryInfo retryInfo , Controller <P > controller , P primaryResource ) {
15
18
this .retryInfo = retryInfo ;
@@ -28,4 +31,19 @@ public <T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventS
28
31
.getResourceEventSourceFor (expectedType , eventSourceName )
29
32
.flatMap (es -> es .getAssociated (primaryResource ));
30
33
}
34
+
35
+ @ Override
36
+ public <T > Optional <T > get (Object key , Class <T > expectedType ) {
37
+ return Optional .ofNullable (attributes .get (key ))
38
+ .filter (expectedType ::isInstance )
39
+ .map (expectedType ::cast );
40
+ }
41
+
42
+ @ Override
43
+ public Optional put (Object key , Object value ) {
44
+ if (value == null ) {
45
+ return Optional .ofNullable (attributes .remove (key ));
46
+ }
47
+ return Optional .ofNullable (attributes .put (key , value ));
48
+ }
31
49
}
0 commit comments