1212import io .fabric8 .kubernetes .client .dsl .Resource ;
1313import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
1414import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
15+ import io .javaoperatorsdk .operator .processing .CustomResourceCache ;
1516import io .javaoperatorsdk .operator .processing .KubernetesResourceUtils ;
1617import io .javaoperatorsdk .operator .processing .event .AbstractEventSource ;
1718import java .util .ArrayList ;
@@ -35,6 +36,7 @@ public class CustomResourceEventSource<T extends CustomResource<?, ?>> extends A
3536 private final Map <String , Long > lastGenerationProcessedSuccessfully = new ConcurrentHashMap <>();
3637 private final List <Watch > watches ;
3738 private final String resClass ;
39+ private final CustomResourceCache customResourceCache ;
3840
3941 public CustomResourceEventSource (
4042 MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
@@ -44,7 +46,8 @@ public CustomResourceEventSource(
4446 configuration .getEffectiveNamespaces (),
4547 configuration .isGenerationAware (),
4648 configuration .getFinalizer (),
47- configuration .getCustomResourceClass ());
49+ configuration .getCustomResourceClass (),
50+ new CustomResourceCache (configuration .getConfigurationService ().getObjectMapper ()));
4851 }
4952
5053 CustomResourceEventSource (
@@ -53,12 +56,29 @@ public CustomResourceEventSource(
5356 boolean generationAware ,
5457 String resourceFinalizer ,
5558 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 ) {
5675 this .client = (CustomResourceOperationsImpl <T , KubernetesResourceList <T >>) client ;
5776 this .targetNamespaces = targetNamespaces ;
5877 this .generationAware = generationAware ;
5978 this .resourceFinalizer = resourceFinalizer ;
6079 this .watches = new ArrayList <>();
6180 this .resClass = resClass .getName ();
81+ this .customResourceCache = customResourceCache ;
6282 }
6383
6484 @ Override
@@ -97,6 +117,9 @@ public void eventReceived(Watcher.Action action, T customResource) {
97117 action .name (),
98118 customResource .getMetadata ().getName ());
99119
120+ // cache the latest version of the CR
121+ customResourceCache .cacheResource (customResource );
122+
100123 if (action == Action .ERROR ) {
101124 log .debug (
102125 "Skipping {} event for custom resource uid: {}, version: {}" ,
0 commit comments