33import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
44import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getVersion ;
55
6+ import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
67import io .fabric8 .kubernetes .client .CustomResource ;
78import io .fabric8 .kubernetes .client .Watch ;
89import io .fabric8 .kubernetes .client .Watcher ;
910import io .fabric8 .kubernetes .client .WatcherException ;
1011import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
12+ import io .fabric8 .kubernetes .client .dsl .Resource ;
1113import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
1214import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
1315import io .javaoperatorsdk .operator .processing .KubernetesResourceUtils ;
2123import org .slf4j .LoggerFactory ;
2224
2325/** This is a special case since is not bound to a single custom resource */
24- public class CustomResourceEventSource extends AbstractEventSource
25- implements Watcher <CustomResource > {
26+ public class CustomResourceEventSource < T extends CustomResource <?, ?>> extends AbstractEventSource
27+ implements Watcher <T > {
2628
2729 private static final Logger log = LoggerFactory .getLogger (CustomResourceEventSource .class );
2830
29- private final MixedOperation client ;
31+ private final CustomResourceOperationsImpl < T , KubernetesResourceList < T >> client ;
3032 private final Set <String > targetNamespaces ;
3133 private final boolean generationAware ;
3234 private final String resourceFinalizer ;
@@ -35,12 +37,23 @@ public class CustomResourceEventSource extends AbstractEventSource
3537 private final String resClass ;
3638
3739 public CustomResourceEventSource (
38- MixedOperation client ,
40+ MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
41+ ControllerConfiguration <T > configuration ) {
42+ this (
43+ client ,
44+ configuration .getEffectiveNamespaces (),
45+ configuration .isGenerationAware (),
46+ configuration .getFinalizer (),
47+ configuration .getCustomResourceClass ());
48+ }
49+
50+ CustomResourceEventSource (
51+ MixedOperation <T , KubernetesResourceList <T >, Resource <T >> client ,
3952 Set <String > targetNamespaces ,
4053 boolean generationAware ,
4154 String resourceFinalizer ,
42- Class <? > resClass ) {
43- this .client = client ;
55+ Class <T > resClass ) {
56+ this .client = ( CustomResourceOperationsImpl < T , KubernetesResourceList < T >>) client ;
4457 this .targetNamespaces = targetNamespaces ;
4558 this .generationAware = generationAware ;
4659 this .resourceFinalizer = resourceFinalizer ;
@@ -50,15 +63,14 @@ public CustomResourceEventSource(
5063
5164 @ Override
5265 public void start () {
53- CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
5466 if (ControllerConfiguration .allNamespacesWatched (targetNamespaces )) {
55- var w = crClient .inAnyNamespace ().watch (this );
67+ var w = client .inAnyNamespace ().watch (this );
5668 watches .add (w );
5769 log .debug ("Registered controller {} -> {} for any namespace" , resClass , w );
5870 } else {
5971 targetNamespaces .forEach (
6072 ns -> {
61- var w = crClient .inNamespace (ns ).watch (this );
73+ var w = client .inNamespace (ns ).watch (this );
6274 watches .add (w );
6375 log .debug ("Registered controller {} -> {} for namespace: {}" , resClass , w , ns );
6476 });
@@ -78,7 +90,7 @@ public void close() {
7890 }
7991
8092 @ Override
81- public void eventReceived (Watcher .Action action , CustomResource customResource ) {
93+ public void eventReceived (Watcher .Action action , T customResource ) {
8294 log .debug (
8395 "Event received for action: {}, resource: {}" ,
8496 action .name (),
@@ -104,14 +116,14 @@ public void eventReceived(Watcher.Action action, CustomResource customResource)
104116 }
105117 }
106118
107- private void markLastGenerationProcessed (CustomResource resource ) {
119+ private void markLastGenerationProcessed (T resource ) {
108120 if (generationAware && resource .hasFinalizer (resourceFinalizer )) {
109121 lastGenerationProcessedSuccessfully .put (
110122 KubernetesResourceUtils .getUID (resource ), resource .getMetadata ().getGeneration ());
111123 }
112124 }
113125
114- private boolean skipBecauseOfGeneration (CustomResource customResource ) {
126+ private boolean skipBecauseOfGeneration (T customResource ) {
115127 if (!generationAware ) {
116128 return false ;
117129 }
@@ -122,7 +134,7 @@ private boolean skipBecauseOfGeneration(CustomResource customResource) {
122134 return !hasGenerationAlreadyBeenProcessed (customResource );
123135 }
124136
125- private boolean hasGenerationAlreadyBeenProcessed (CustomResource resource ) {
137+ private boolean hasGenerationAlreadyBeenProcessed (T resource ) {
126138 Long lastGeneration = lastGenerationProcessedSuccessfully .get (resource .getMetadata ().getUid ());
127139 if (lastGeneration == null ) {
128140 return true ;
0 commit comments