@@ -121,10 +121,10 @@ public void handleEvent(Event event) {
121
121
}
122
122
123
123
private void handleMarkedEventForResource (ResourceID resourceID ) {
124
- if (!eventMarker .deleteEventPresent (resourceID )) {
125
- submitReconciliationExecution (resourceID );
126
- } else {
124
+ if (eventMarker .deleteEventPresent (resourceID )) {
127
125
cleanupForDeletedEvent (resourceID );
126
+ } else if (!eventMarker .processedMarkForDeletionPresent (resourceID )) {
127
+ submitReconciliationExecution (resourceID );
128
128
}
129
129
}
130
130
@@ -157,18 +157,50 @@ private void submitReconciliationExecution(ResourceID resourceID) {
157
157
}
158
158
159
159
private void handleEventMarking (Event event ) {
160
- if (event instanceof ResourceEvent
161
- && ((ResourceEvent ) event ).getAction () == ResourceAction .DELETED ) {
162
- log .debug ("Marking delete event received for: {}" , event .getRelatedCustomResourceID ());
163
- eventMarker .markDeleteEventReceived (event );
164
- } else if (!eventMarker .deleteEventPresent (event .getRelatedCustomResourceID ())) {
165
- log .debug ("Marking event received for: {}" , event .getRelatedCustomResourceID ());
166
- eventMarker .markEventReceived (event );
160
+ final var relatedCustomResourceID = event .getRelatedCustomResourceID ();
161
+ if (event instanceof ResourceEvent ) {
162
+ var resourceEvent = (ResourceEvent ) event ;
163
+ if (resourceEvent .getAction () == ResourceAction .DELETED ) {
164
+ log .debug ("Marking delete event received for: {}" , relatedCustomResourceID );
165
+ eventMarker .markDeleteEventReceived (event );
166
+ } else {
167
+ if (eventMarker .processedMarkForDeletionPresent (relatedCustomResourceID )
168
+ && isResourceMarkedForDeletion (resourceEvent )) {
169
+ log .debug (
170
+ "Skipping mark of event received, since already processed mark for deletion and resource marked for deletion: {}" ,
171
+ relatedCustomResourceID );
172
+ return ;
173
+ }
174
+ // Normally when eventMarker is in state PROCESSED_MARK_FOR_DELETION it is expected to
175
+ // receive a Delete event or an event where resource is marked for deletion. In a rare edge
176
+ // case however it can happen that the finalizer related to the current controller is
177
+ // removed, but also the informers websocket is disconnected and later reconnected. So
178
+ // meanwhile the resource could be deleted and recreated. In this case we just mark a new
179
+ // event as below.
180
+ markEventReceived (event );
181
+ }
182
+ } else if (!eventMarker .deleteEventPresent (relatedCustomResourceID ) ||
183
+ !eventMarker .processedMarkForDeletionPresent (relatedCustomResourceID )) {
184
+ markEventReceived (event );
185
+ } else if (log .isDebugEnabled ()) {
186
+ log .debug (
187
+ "Skipped marking event as received. Delete event present: {}, processed mark for deletion: {}" ,
188
+ eventMarker .deleteEventPresent (relatedCustomResourceID ),
189
+ eventMarker .processedMarkForDeletionPresent (relatedCustomResourceID ));
167
190
}
168
191
}
169
192
170
- private RetryInfo retryInfo (ResourceID customResourceUid ) {
171
- return retryState .get (customResourceUid );
193
+ private void markEventReceived (Event event ) {
194
+ log .debug ("Marking event received for: {}" , event .getRelatedCustomResourceID ());
195
+ eventMarker .markEventReceived (event );
196
+ }
197
+
198
+ private boolean isResourceMarkedForDeletion (ResourceEvent resourceEvent ) {
199
+ return resourceEvent .getResource ().map (HasMetadata ::isMarkedForDeletion ).orElse (false );
200
+ }
201
+
202
+ private RetryInfo retryInfo (ResourceID resourceID ) {
203
+ return retryState .get (resourceID );
172
204
}
173
205
174
206
void eventProcessingFinished (
@@ -199,6 +231,8 @@ void eventProcessingFinished(
199
231
metrics .finishedReconciliation (resourceID );
200
232
if (eventMarker .deleteEventPresent (resourceID )) {
201
233
cleanupForDeletedEvent (executionScope .getResourceID ());
234
+ } else if (postExecutionControl .isFinalizerRemoved ()) {
235
+ eventMarker .markProcessedMarkForDeletion (resourceID );
202
236
} else {
203
237
postExecutionControl
204
238
.getUpdatedCustomResource ()
@@ -247,13 +281,13 @@ TimerEventSource<R> retryEventSource() {
247
281
private void handleRetryOnException (
248
282
ExecutionScope <R > executionScope , Exception exception ) {
249
283
RetryExecution execution = getOrInitRetryExecution (executionScope );
250
- var customResourceID = executionScope .getResourceID ();
251
- boolean eventPresent = eventMarker .eventPresent (customResourceID );
252
- eventMarker .markEventReceived (customResourceID );
284
+ var resourceID = executionScope .getResourceID ();
285
+ boolean eventPresent = eventMarker .eventPresent (resourceID );
286
+ eventMarker .markEventReceived (resourceID );
253
287
254
288
if (eventPresent ) {
255
- log .debug ("New events exists for for resource id: {}" , customResourceID );
256
- submitReconciliationExecution (customResourceID );
289
+ log .debug ("New events exists for for resource id: {}" , resourceID );
290
+ submitReconciliationExecution (resourceID );
257
291
return ;
258
292
}
259
293
Optional <Long > nextDelay = execution .nextDelay ();
@@ -263,8 +297,8 @@ private void handleRetryOnException(
263
297
log .debug (
264
298
"Scheduling timer event for retry with delay:{} for resource: {}" ,
265
299
delay ,
266
- customResourceID );
267
- metrics .failedReconciliation (customResourceID , exception );
300
+ resourceID );
301
+ metrics .failedReconciliation (resourceID , exception );
268
302
retryEventSource ().scheduleOnce (executionScope .getResource (), delay );
269
303
},
270
304
() -> log .error ("Exhausted retries for {}" , executionScope ));
@@ -288,22 +322,22 @@ private RetryExecution getOrInitRetryExecution(ExecutionScope<R> executionScope)
288
322
return retryExecution ;
289
323
}
290
324
291
- private void cleanupForDeletedEvent (ResourceID customResourceUid ) {
292
- log .debug ("Cleaning up for delete event for: {}" , customResourceUid );
293
- eventMarker .cleanup (customResourceUid );
294
- metrics .cleanupDoneFor (customResourceUid );
325
+ private void cleanupForDeletedEvent (ResourceID resourceID ) {
326
+ log .debug ("Cleaning up for delete event for: {}" , resourceID );
327
+ eventMarker .cleanup (resourceID );
328
+ metrics .cleanupDoneFor (resourceID );
295
329
}
296
330
297
- private boolean isControllerUnderExecution (ResourceID customResourceUid ) {
298
- return underProcessing .contains (customResourceUid );
331
+ private boolean isControllerUnderExecution (ResourceID resourceID ) {
332
+ return underProcessing .contains (resourceID );
299
333
}
300
334
301
- private void setUnderExecutionProcessing (ResourceID customResourceUid ) {
302
- underProcessing .add (customResourceUid );
335
+ private void setUnderExecutionProcessing (ResourceID resourceID ) {
336
+ underProcessing .add (resourceID );
303
337
}
304
338
305
- private void unsetUnderExecution (ResourceID customResourceUid ) {
306
- underProcessing .remove (customResourceUid );
339
+ private void unsetUnderExecution (ResourceID resourceID ) {
340
+ underProcessing .remove (resourceID );
307
341
}
308
342
309
343
private boolean isRetryConfigured () {
0 commit comments