1
1
package com .github .containersolutions .operator .processing ;
2
2
3
+ import com .github .containersolutions .operator .ControllerUtils ;
3
4
import com .github .containersolutions .operator .api .*;
4
5
import io .fabric8 .kubernetes .client .CustomResource ;
5
6
import io .fabric8 .kubernetes .client .Watcher ;
@@ -39,29 +40,31 @@ public void handleEvent(CustomResourceEvent event) {
39
40
}
40
41
Context context = new DefaultContext (new RetryInfo (event .getRetryCount (), event .getRetryExecution ().isLastExecution ()));
41
42
/* Its interesting problem if we should call delete if received event after object is marked for deletion
42
- but there is not our finalizer. Since it can happen that there are multiple finalizers, also other events after
43
- we called delete and remove finalizers already. But also it can happen that we did not manage to put
44
- finalizer into the resource before marked for delete. So for now we will call delete every time, since delete
45
- operation should be idempotent too, and this way we cover the corner case. */
46
- if (markedForDeletion (resource ) || action == Watcher .Action .DELETED ) {
43
+ but finalizer is not on the object. Since it can happen that there are multiple finalizers, also other events after
44
+ we called delete and remove finalizers already. Delete should be also idempotent, we call it now. */
45
+ // todo should we check if it has also finalizer?
46
+ if (markedForDeletion (resource )) {
47
47
boolean removeFinalizer = controller .deleteResource (resource , context );
48
- if (removeFinalizer && hasDefaultFinalizer (resource )) {
48
+ if (removeFinalizer && ControllerUtils . hasDefaultFinalizer (resource , resourceDefaultFinalizer )) {
49
49
removeDefaultFinalizer (resource );
50
50
}
51
51
} else {
52
- UpdateControl <? extends CustomResource > updateControl = controller .createOrUpdateResource (resource , context );
53
- // note that we do the status sub-resource update first, since if there is an event from Custom resource
54
- // update as next step, the new status is already present.
55
- if (updateControl .isUpdateStatusSubResource ()) {
56
- customResourceFacade .updateStatus (updateControl .getCustomResource ());
57
- }
58
- if (updateControl .isUpdateCustomResource ()) {
59
- updateCustomResource (updateControl .getCustomResource ());
60
- } else if (!hasDefaultFinalizer (resource ) && !markedForDeletion (resource )) {
61
- // We always add the default finalizer if missing and not marked for deletion.
62
- // Adding it for original resource since the update was not requested. (Although we are not cloning
63
- // passing it as argument)
52
+ if (!ControllerUtils .hasDefaultFinalizer (resource , resourceDefaultFinalizer ) && !markedForDeletion (resource )) {
53
+ /* We always add the default finalizer if missing and not marked for deletion.
54
+ We execute the controller processing only for processing the event sent as a results
55
+ of the finalizer add. This will make sure that the resources are not created before
56
+ there is a finalizer.
57
+ */
64
58
updateCustomResourceWithFinalizer (resource );
59
+ } else {
60
+ UpdateControl <? extends CustomResource > updateControl = controller .createOrUpdateResource (resource , context );
61
+ // note that we do the status sub-resource update first, since if there is an event from Custom resource
62
+ // update as next step, the new status is already present.
63
+ if (updateControl .isUpdateStatusSubResource ()) {
64
+ customResourceFacade .updateStatus (updateControl .getCustomResource ());
65
+ } else if (updateControl .isUpdateCustomResource ()) {
66
+ updateCustomResource (updateControl .getCustomResource ());
67
+ }
65
68
}
66
69
}
67
70
}
@@ -83,13 +86,6 @@ private void updateCustomResource(CustomResource updatedResource) {
83
86
}
84
87
85
88
86
- private boolean hasDefaultFinalizer (CustomResource resource ) {
87
- if (resource .getMetadata ().getFinalizers () != null ) {
88
- return resource .getMetadata ().getFinalizers ().contains (resourceDefaultFinalizer );
89
- }
90
- return false ;
91
- }
92
-
93
89
private void removeDefaultFinalizer (CustomResource resource ) {
94
90
log .debug ("Removing finalizer on {}: {}" , resource );
95
91
resource .getMetadata ().getFinalizers ().remove (resourceDefaultFinalizer );
@@ -102,7 +98,7 @@ private void replace(CustomResource resource) {
102
98
}
103
99
104
100
private void addFinalizerIfNotPresent (CustomResource resource ) {
105
- if (!hasDefaultFinalizer (resource ) && !markedForDeletion (resource )) {
101
+ if (!ControllerUtils . hasDefaultFinalizer (resource , resourceDefaultFinalizer ) && !markedForDeletion (resource )) {
106
102
log .info ("Adding default finalizer to {}" , resource .getMetadata ());
107
103
if (resource .getMetadata ().getFinalizers () == null ) {
108
104
resource .getMetadata ().setFinalizers (new ArrayList <>(1 ));
0 commit comments