21
21
/**
22
22
* Dispatches events to the Controller and handles Finalizers for a single type of Custom Resource.
23
23
*/
24
- public class EventDispatcher {
24
+ public class EventDispatcher < R extends CustomResource > {
25
25
26
26
private static final Logger log = LoggerFactory .getLogger (EventDispatcher .class );
27
27
28
- private final ResourceController controller ;
28
+ private final ResourceController < R > controller ;
29
29
private final String resourceFinalizer ;
30
- private final CustomResourceFacade customResourceFacade ;
30
+ private final CustomResourceFacade < R > customResourceFacade ;
31
31
private EventSourceManager eventSourceManager ;
32
32
33
- public EventDispatcher (
34
- ResourceController controller , String finalizer , CustomResourceFacade customResourceFacade ) {
33
+ EventDispatcher (
34
+ ResourceController <R > controller ,
35
+ String finalizer ,
36
+ CustomResourceFacade <R > customResourceFacade ) {
35
37
this .controller = controller ;
36
38
this .customResourceFacade = customResourceFacade ;
37
39
this .resourceFinalizer = finalizer ;
38
40
}
39
41
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
+
40
49
public void setEventSourceManager (EventSourceManager eventSourceManager ) {
41
50
this .eventSourceManager = eventSourceManager ;
42
51
}
43
52
44
- public PostExecutionControl handleExecution (ExecutionScope executionScope ) {
53
+ public PostExecutionControl handleExecution (ExecutionScope < R > executionScope ) {
45
54
try {
46
55
return handleDispatch (executionScope );
47
56
} catch (RuntimeException e ) {
@@ -50,8 +59,8 @@ public PostExecutionControl handleExecution(ExecutionScope executionScope) {
50
59
}
51
60
}
52
61
53
- private PostExecutionControl handleDispatch (ExecutionScope executionScope ) {
54
- CustomResource resource = executionScope .getCustomResource ();
62
+ private PostExecutionControl handleDispatch (ExecutionScope < R > executionScope ) {
63
+ R resource = executionScope .getCustomResource ();
55
64
log .debug (
56
65
"Handling events: {} for resource {}" , executionScope .getEvents (), resource .getMetadata ());
57
66
@@ -68,8 +77,8 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
68
77
executionScope );
69
78
return PostExecutionControl .defaultDispatch ();
70
79
}
71
- Context context =
72
- new DefaultContext (
80
+ Context < R > context =
81
+ new DefaultContext <> (
73
82
eventSourceManager ,
74
83
new EventList (executionScope .getEvents ()),
75
84
executionScope .getRetryInfo ());
@@ -81,7 +90,7 @@ private PostExecutionControl handleDispatch(ExecutionScope executionScope) {
81
90
}
82
91
83
92
private PostExecutionControl handleCreateOrUpdate (
84
- ExecutionScope executionScope , CustomResource resource , Context context ) {
93
+ ExecutionScope < R > executionScope , R resource , Context < R > context ) {
85
94
if (!resource .hasFinalizer (resourceFinalizer ) && !resource .isMarkedForDeletion ()) {
86
95
/* We always add the finalizer if missing and not marked for deletion.
87
96
We execute the controller processing only for processing the event sent as a results
@@ -96,9 +105,8 @@ private PostExecutionControl handleCreateOrUpdate(
96
105
getUID (resource ),
97
106
getVersion (resource ),
98
107
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 ;
102
110
if (updateControl .isUpdateCustomResourceAndStatusSubResource ()) {
103
111
updatedCustomResource = updateCustomResource (updateControl .getCustomResource ());
104
112
updateControl
@@ -122,15 +130,15 @@ private PostExecutionControl handleCreateOrUpdate(
122
130
}
123
131
}
124
132
125
- private PostExecutionControl handleDelete (CustomResource resource , Context context ) {
133
+ private PostExecutionControl handleDelete (R resource , Context < R > context ) {
126
134
log .debug (
127
135
"Executing delete for resource: {} with version: {}" ,
128
136
getUID (resource ),
129
137
getVersion (resource ));
130
138
DeleteControl deleteControl = controller .deleteResource (resource , context );
131
139
boolean hasFinalizer = resource .hasFinalizer (resourceFinalizer );
132
140
if (deleteControl == DeleteControl .DEFAULT_DELETE && hasFinalizer ) {
133
- CustomResource customResource = removeFinalizer (resource );
141
+ R customResource = removeFinalizer (resource );
134
142
return PostExecutionControl .customResourceUpdated (customResource );
135
143
} else {
136
144
log .debug (
@@ -143,20 +151,20 @@ private PostExecutionControl handleDelete(CustomResource resource, Context conte
143
151
}
144
152
}
145
153
146
- private void updateCustomResourceWithFinalizer (CustomResource resource ) {
154
+ private void updateCustomResourceWithFinalizer (R resource ) {
147
155
log .debug (
148
156
"Adding finalizer for resource: {} version: {}" , getUID (resource ), getVersion (resource ));
149
157
resource .addFinalizer (resourceFinalizer );
150
158
replace (resource );
151
159
}
152
160
153
- private CustomResource updateCustomResource (CustomResource resource ) {
161
+ private R updateCustomResource (R resource ) {
154
162
log .debug ("Updating resource: {} with version: {}" , getUID (resource ), getVersion (resource ));
155
163
log .trace ("Resource before update: {}" , resource );
156
164
return replace (resource );
157
165
}
158
166
159
- private CustomResource removeFinalizer (CustomResource resource ) {
167
+ private R removeFinalizer (R resource ) {
160
168
log .debug (
161
169
"Removing finalizer on resource: {} with version: {}" ,
162
170
getUID (resource ),
@@ -165,7 +173,7 @@ private CustomResource removeFinalizer(CustomResource resource) {
165
173
return customResourceFacade .replaceWithLock (resource );
166
174
}
167
175
168
- private CustomResource replace (CustomResource resource ) {
176
+ private R replace (R resource ) {
169
177
log .debug (
170
178
"Trying to replace resource {}, version: {}" ,
171
179
resource .getMetadata ().getName (),
@@ -174,7 +182,7 @@ private CustomResource replace(CustomResource resource) {
174
182
}
175
183
176
184
// created to support unit testing
177
- public static class CustomResourceFacade <R extends CustomResource > {
185
+ static class CustomResourceFacade <R extends CustomResource > {
178
186
179
187
private final MixedOperation <R , KubernetesResourceList <R >, Resource <R >> resourceOperation ;
180
188
0 commit comments