1
1
package io .javaoperatorsdk .operator .processing ;
2
2
3
+ import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getName ;
4
+ import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
5
+
6
+ import com .fasterxml .jackson .core .JsonProcessingException ;
7
+ import com .fasterxml .jackson .databind .ObjectMapper ;
8
+ import io .fabric8 .kubernetes .client .CustomResource ;
3
9
import java .util .List ;
4
10
import java .util .Optional ;
5
11
import java .util .Set ;
9
15
import java .util .concurrent .locks .ReentrantLock ;
10
16
import java .util .function .Predicate ;
11
17
import java .util .stream .Collectors ;
12
-
13
18
import org .slf4j .Logger ;
14
19
import org .slf4j .LoggerFactory ;
15
-
16
- import io .fabric8 .kubernetes .client .CustomResource ;
17
20
import io .javaoperatorsdk .operator .Metrics ;
18
21
19
- import com .fasterxml .jackson .core .JsonProcessingException ;
20
- import com .fasterxml .jackson .databind .ObjectMapper ;
21
-
22
- import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getName ;
23
- import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
24
-
25
22
@ SuppressWarnings ("rawtypes" )
26
- public class CustomResourceCache {
23
+ public class CustomResourceCache < T extends CustomResource <?, ?>> {
27
24
28
25
private static final Logger log = LoggerFactory .getLogger (CustomResourceCache .class );
29
26
private static final Predicate passthrough = o -> true ;
30
27
31
28
private final ObjectMapper objectMapper ;
32
- private final ConcurrentMap <String , CustomResource > resources ;
29
+ private final ConcurrentMap <String , T > resources ;
33
30
private final Lock lock = new ReentrantLock ();
34
31
35
32
public CustomResourceCache () {
@@ -44,19 +41,20 @@ public CustomResourceCache(ObjectMapper objectMapper, Metrics metrics) {
44
41
resources = metrics .monitorSizeOf (new ConcurrentHashMap <>(), "cache" );
45
42
}
46
43
47
- public void cacheResource (CustomResource resource ) {
44
+ public void cacheResource (T resource ) {
48
45
cacheResource (resource , passthrough );
49
46
}
50
47
51
- public void cacheResource (CustomResource resource , Predicate <CustomResource > predicate ) {
48
+ public void cacheResource (T resource , Predicate <CustomResource > predicate ) {
52
49
try {
53
50
lock .lock ();
54
51
final var uid = getUID (resource );
55
52
if (predicate .test (resources .get (uid ))) {
56
53
if (passthrough != predicate ) {
57
54
log .trace ("Update cache after condition is true: {}" , getName (resource ));
58
55
}
59
- resources .put (uid , resource );
56
+ // defensive copy
57
+ resources .put (getUID (resource ), clone (resource ));
60
58
}
61
59
} finally {
62
60
lock .unlock ();
@@ -70,11 +68,11 @@ public void cacheResource(CustomResource resource, Predicate<CustomResource> pre
70
68
* @param uuid
71
69
* @return
72
70
*/
73
- public Optional <CustomResource > getLatestResource (String uuid ) {
71
+ public Optional <T > getLatestResource (String uuid ) {
74
72
return Optional .ofNullable (resources .get (uuid )).map (this ::clone );
75
73
}
76
74
77
- public List <CustomResource > getLatestResources (Predicate <CustomResource > selector ) {
75
+ public List <T > getLatestResources (Predicate <CustomResource > selector ) {
78
76
try {
79
77
lock .lock ();
80
78
return resources .values ().stream ()
@@ -98,16 +96,17 @@ public Set<String> getLatestResourcesUids(Predicate<CustomResource> selector) {
98
96
}
99
97
}
100
98
101
- private CustomResource clone (CustomResource customResource ) {
99
+ @ SuppressWarnings ("unchecked" )
100
+ private T clone (CustomResource customResource ) {
102
101
try {
103
- return objectMapper .readValue (
102
+ return ( T ) objectMapper .readValue (
104
103
objectMapper .writeValueAsString (customResource ), customResource .getClass ());
105
104
} catch (JsonProcessingException e ) {
106
105
throw new IllegalStateException (e );
107
106
}
108
107
}
109
108
110
- public CustomResource cleanup (String customResourceUid ) {
109
+ public T cleanup (String customResourceUid ) {
111
110
return resources .remove (customResourceUid );
112
111
}
113
112
}
0 commit comments