12
12
import io .fabric8 .kubernetes .client .dsl .Resource ;
13
13
import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
14
14
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
15
+ import io .javaoperatorsdk .operator .processing .CustomResourceCache ;
15
16
import io .javaoperatorsdk .operator .processing .KubernetesResourceUtils ;
16
17
import io .javaoperatorsdk .operator .processing .event .AbstractEventSource ;
17
18
import java .util .ArrayList ;
@@ -35,6 +36,7 @@ public class CustomResourceEventSource<T extends CustomResource<?, ?>> extends A
35
36
private final Map <String , Long > lastGenerationProcessedSuccessfully = new ConcurrentHashMap <>();
36
37
private final List <Watch > watches ;
37
38
private final String resClass ;
39
+ private final CustomResourceCache customResourceCache ;
38
40
39
41
public CustomResourceEventSource (
40
42
MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
@@ -44,7 +46,8 @@ public CustomResourceEventSource(
44
46
configuration .getEffectiveNamespaces (),
45
47
configuration .isGenerationAware (),
46
48
configuration .getFinalizer (),
47
- configuration .getCustomResourceClass ());
49
+ configuration .getCustomResourceClass (),
50
+ new CustomResourceCache (configuration .getConfigurationService ().getObjectMapper ()));
48
51
}
49
52
50
53
CustomResourceEventSource (
@@ -53,12 +56,29 @@ public CustomResourceEventSource(
53
56
boolean generationAware ,
54
57
String resourceFinalizer ,
55
58
Class <T > resClass ) {
59
+ this (
60
+ client ,
61
+ targetNamespaces ,
62
+ generationAware ,
63
+ resourceFinalizer ,
64
+ resClass ,
65
+ new CustomResourceCache ());
66
+ }
67
+
68
+ CustomResourceEventSource (
69
+ MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
70
+ Set <String > targetNamespaces ,
71
+ boolean generationAware ,
72
+ String resourceFinalizer ,
73
+ Class <T > resClass ,
74
+ CustomResourceCache customResourceCache ) {
56
75
this .client = (CustomResourceOperationsImpl <T , KubernetesResourceList <T >>) client ;
57
76
this .targetNamespaces = targetNamespaces ;
58
77
this .generationAware = generationAware ;
59
78
this .resourceFinalizer = resourceFinalizer ;
60
79
this .watches = new ArrayList <>();
61
80
this .resClass = resClass .getName ();
81
+ this .customResourceCache = customResourceCache ;
62
82
}
63
83
64
84
@ Override
@@ -97,6 +117,9 @@ public void eventReceived(Watcher.Action action, T customResource) {
97
117
action .name (),
98
118
customResource .getMetadata ().getName ());
99
119
120
+ // cache the latest version of the CR
121
+ customResourceCache .cacheResource (customResource );
122
+
100
123
if (action == Action .ERROR ) {
101
124
log .debug (
102
125
"Skipping {} event for custom resource uid: {}, version: {}" ,
0 commit comments