Skip to content

Commit a2dadba

Browse files
author
attila.meszaros
committed
- skip delete if no finalizer
- fix tests for EventDispatcher
1 parent 0453d72 commit a2dadba

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

operator-framework/src/main/java/com/github/containersolutions/operator/processing/EventDispatcher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void handleEvent(CustomResourceEvent event) {
4242
/* Its interesting problem if we should call delete if received event after object is marked for deletion
4343
but finalizer is not on the object. Since it can happen that there are multiple finalizers, also other events after
4444
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?
45+
if (markedForDeletion(resource) && !ControllerUtils.hasDefaultFinalizer(resource, resourceDefaultFinalizer)) {
46+
return;
47+
}
4648
if (markedForDeletion(resource)) {
4749
boolean removeFinalizer = controller.deleteResource(resource, context);
4850
if (removeFinalizer && ControllerUtils.hasDefaultFinalizer(resource, resourceDefaultFinalizer)) {

operator-framework/src/test/java/com/github/containersolutions/operator/EventDispatcherTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
9494
@Test
9595
void removesDefaultFinalizerOnDelete() {
9696
markForDeletion(testCustomResource);
97-
testCustomResource.getMetadata().getFinalizers().add(DEFAULT_FINALIZER);
9897

9998
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
10099

@@ -106,7 +105,6 @@ void removesDefaultFinalizerOnDelete() {
106105
void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse() {
107106
when(resourceController.deleteResource(eq(testCustomResource), any())).thenReturn(false);
108107
markForDeletion(testCustomResource);
109-
testCustomResource.getMetadata().getFinalizers().add(DEFAULT_FINALIZER);
110108

111109
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
112110

@@ -116,7 +114,6 @@ void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse() {
116114

117115
@Test
118116
void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
119-
testCustomResource.getMetadata().getFinalizers().add(DEFAULT_FINALIZER);
120117
when(resourceController.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
121118

122119
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
@@ -126,6 +123,7 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
126123

127124
@Test
128125
void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
126+
removeFinalizers(testCustomResource);
129127
when(resourceController.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
130128

131129
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
@@ -135,20 +133,24 @@ void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
135133
}
136134

137135
@Test
138-
void doesNotAddFinalizerIfNoUpdateIsReturnedButMarkedForDeletion() {
136+
void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
137+
removeFinalizers(testCustomResource);
139138
markForDeletion(testCustomResource);
140-
when(resourceController.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
141139

142140
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
143141

144-
assertEquals(0, testCustomResource.getMetadata().getFinalizers().size());
145142
verify(customResourceFacade, never()).replaceWithLock(any());
143+
verify(resourceController, never()).deleteResource(eq(testCustomResource), any());
146144
}
147145

148146
private void markForDeletion(CustomResource customResource) {
149147
customResource.getMetadata().setDeletionTimestamp("2019-8-10");
150148
}
151149

150+
private void removeFinalizers(CustomResource customResource) {
151+
customResource.getMetadata().getFinalizers().clear();
152+
}
153+
152154
CustomResource getResource() {
153155
TestCustomResource resource = new TestCustomResource();
154156
resource.setMetadata(new ObjectMetaBuilder()
@@ -157,6 +159,7 @@ CustomResource getResource() {
157159
.withDeletionGracePeriodSeconds(10L)
158160
.withGeneration(10L)
159161
.withName("name")
162+
.withFinalizers(DEFAULT_FINALIZER)
160163
.withNamespace("namespace")
161164
.withResourceVersion("resourceVersion")
162165
.withSelfLink("selfLink")

operator-framework/src/test/java/com/github/containersolutions/operator/SubResourceUpdateIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public void updatesSubResourceStatus() {
4444
});
4545
}
4646

47+
@Test
48+
public void ifNoFinalizerPresentFirstAddsTheFinalizerThenExecutesControllerAgain() {
49+
initAndCleanup();
50+
integrationTestSupport.teardownIfSuccess(() -> {
51+
SubResourceTestCustomResource resource = createTestCustomResource("1");
52+
resource.getMetadata().getFinalizers().clear();
53+
integrationTestSupport.getCrOperations().inNamespace(TEST_NAMESPACE).create(resource);
54+
55+
awaitStatusUpdated(resource.getMetadata().getName());
56+
// wait for sure, there are no more events
57+
waitXms(200);
58+
// there is no event on status update processed
59+
assertThat(integrationTestSupport.numberOfControllerExecutions()).isEqualTo(2);
60+
});
61+
}
4762

4863
void awaitStatusUpdated(String name) {
4964
await("cr status updated").atMost(5, TimeUnit.SECONDS)

0 commit comments

Comments
 (0)