2121/**
2222 * Dispatches events to the Controller and handles Finalizers for a single type of Custom Resource.
2323 */
24- public class EventDispatcher {
24+ public class EventDispatcher < R extends CustomResource > {
2525
2626 private static final Logger log = LoggerFactory .getLogger (EventDispatcher .class );
2727
28- private final ResourceController controller ;
28+ private final ResourceController < R > controller ;
2929 private final String resourceFinalizer ;
30- private final CustomResourceFacade customResourceFacade ;
30+ private final CustomResourceFacade < R > customResourceFacade ;
3131 private EventSourceManager eventSourceManager ;
3232
33- public EventDispatcher (
34- ResourceController controller , String finalizer , CustomResourceFacade customResourceFacade ) {
33+ EventDispatcher (
34+ ResourceController <R > controller ,
35+ String finalizer ,
36+ CustomResourceFacade <R > customResourceFacade ) {
3537 this .controller = controller ;
3638 this .customResourceFacade = customResourceFacade ;
3739 this .resourceFinalizer = finalizer ;
3840 }
3941
42+ public EventDispatcher (
43+ ResourceController <R > controller ,
44+ String finalizer ,
45+ MixedOperation <R , KubernetesResourceList <R >, Resource <R >> client ) {
46+ this (controller , finalizer , new CustomResourceFacade <>(client ));
47+ }
48+
4049 public void setEventSourceManager (EventSourceManager eventSourceManager ) {
4150 this .eventSourceManager = eventSourceManager ;
4251 }
4352
44- public PostExecutionControl handleExecution (ExecutionScope executionScope ) {
53+ public PostExecutionControl handleExecution (ExecutionScope < R > executionScope ) {
4554 try {
4655 return handleDispatch (executionScope );
4756 } catch (RuntimeException e ) {
@@ -50,8 +59,8 @@ public PostExecutionControl handleExecution(ExecutionScope executionScope) {
5059 }
5160 }
5261
53- private PostExecutionControl handleDispatch (ExecutionScope executionScope ) {
54- CustomResource resource = executionScope .getCustomResource ();
62+ private PostExecutionControl handleDispatch (ExecutionScope < R > executionScope ) {
63+ R resource = executionScope .getCustomResource ();
5564 log .debug (
5665 "Handling events: {} for resource {}" , executionScope .getEvents (), resource .getMetadata ());
5766
@@ -68,8 +77,8 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
6877 executionScope );
6978 return PostExecutionControl .defaultDispatch ();
7079 }
71- Context context =
72- new DefaultContext (
80+ Context < R > context =
81+ new DefaultContext <> (
7382 eventSourceManager ,
7483 new EventList (executionScope .getEvents ()),
7584 executionScope .getRetryInfo ());
@@ -81,7 +90,7 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
8190 }
8291
8392 private PostExecutionControl handleCreateOrUpdate (
84- ExecutionScope executionScope , CustomResource resource , Context context ) {
93+ ExecutionScope < R > executionScope , R resource , Context < R > context ) {
8594 if (!resource .hasFinalizer (resourceFinalizer ) && !resource .isMarkedForDeletion ()) {
8695 /* We always add the finalizer if missing and not marked for deletion.
8796 We execute the controller processing only for processing the event sent as a results
@@ -96,9 +105,8 @@ private PostExecutionControl handleCreateOrUpdate(
96105 getUID (resource ),
97106 getVersion (resource ),
98107 executionScope );
99- UpdateControl <? extends CustomResource > updateControl =
100- controller .createOrUpdateResource (resource , context );
101- CustomResource updatedCustomResource = null ;
108+ UpdateControl <R > updateControl = controller .createOrUpdateResource (resource , context );
109+ R updatedCustomResource = null ;
102110 if (updateControl .isUpdateCustomResourceAndStatusSubResource ()) {
103111 updatedCustomResource = updateCustomResource (updateControl .getCustomResource ());
104112 updateControl
@@ -122,15 +130,15 @@ private PostExecutionControl handleCreateOrUpdate(
122130 }
123131 }
124132
125- private PostExecutionControl handleDelete (CustomResource resource , Context context ) {
133+ private PostExecutionControl handleDelete (R resource , Context < R > context ) {
126134 log .debug (
127135 "Executing delete for resource: {} with version: {}" ,
128136 getUID (resource ),
129137 getVersion (resource ));
130138 DeleteControl deleteControl = controller .deleteResource (resource , context );
131139 boolean hasFinalizer = resource .hasFinalizer (resourceFinalizer );
132140 if (deleteControl == DeleteControl .DEFAULT_DELETE && hasFinalizer ) {
133- CustomResource customResource = removeFinalizer (resource );
141+ R customResource = removeFinalizer (resource );
134142 return PostExecutionControl .customResourceUpdated (customResource );
135143 } else {
136144 log .debug (
@@ -143,20 +151,20 @@ private PostExecutionControl handleDelete(CustomResource resource, Context conte
143151 }
144152 }
145153
146- private void updateCustomResourceWithFinalizer (CustomResource resource ) {
154+ private void updateCustomResourceWithFinalizer (R resource ) {
147155 log .debug (
148156 "Adding finalizer for resource: {} version: {}" , getUID (resource ), getVersion (resource ));
149157 resource .addFinalizer (resourceFinalizer );
150158 replace (resource );
151159 }
152160
153- private CustomResource updateCustomResource (CustomResource resource ) {
161+ private R updateCustomResource (R resource ) {
154162 log .debug ("Updating resource: {} with version: {}" , getUID (resource ), getVersion (resource ));
155163 log .trace ("Resource before update: {}" , resource );
156164 return replace (resource );
157165 }
158166
159- private CustomResource removeFinalizer (CustomResource resource ) {
167+ private R removeFinalizer (R resource ) {
160168 log .debug (
161169 "Removing finalizer on resource: {} with version: {}" ,
162170 getUID (resource ),
@@ -165,7 +173,7 @@ private CustomResource removeFinalizer(CustomResource resource) {
165173 return customResourceFacade .replaceWithLock (resource );
166174 }
167175
168- private CustomResource replace (CustomResource resource ) {
176+ private R replace (R resource ) {
169177 log .debug (
170178 "Trying to replace resource {}, version: {}" ,
171179 resource .getMetadata ().getName (),
@@ -174,7 +182,7 @@ private CustomResource replace(CustomResource resource) {
174182 }
175183
176184 // created to support unit testing
177- public static class CustomResourceFacade <R extends CustomResource > {
185+ static class CustomResourceFacade <R extends CustomResource > {
178186
179187 private final MixedOperation <R , KubernetesResourceList <R >, Resource <R >> resourceOperation ;
180188
0 commit comments