Skip to content

Commit f3d0611

Browse files
committed
now possible to not remove finalizer after delete, added simple adapter for interface
1 parent a3cc7eb commit f3d0611

File tree

8 files changed

+67
-8
lines changed

8 files changed

+67
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public class CustomServiceController implements ResourceController<CustomService
6060
public static final String GROUP = "sample.javaoperatorsdk";
6161

6262
@Override
63-
public void deleteResource(CustomService resource, Context<CustomService> context) {
63+
public boolean deleteResource(CustomService resource, Context<CustomService> context) {
6464
// ... your logic ...
65+
return true;
6566
}
6667

6768
// Return the changed resource, so it gets updated. See javadoc for details.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ private void handleEvent(Action action, R resource) {
5353
// we don't want to call delete resource if it not contains our finalizer,
5454
// since the resource still can be updates when marked for deletion and contains other finalizers
5555
if (markedForDeletion(resource) && hasDefaultFinalizer(resource)) {
56-
controller.deleteResource(resource, new Context(k8sClient, resourceClient));
57-
removeDefaultFinalizer(resource);
56+
boolean removeFinalizer = controller.deleteResource(resource, new Context(k8sClient, resourceClient));
57+
if (removeFinalizer) {
58+
removeDefaultFinalizer(resource);
59+
}
5860
} else {
5961
Optional<R> updateResult = controller.createOrUpdateResource(resource, new Context<>(k8sClient, resourceClient));
6062
if (updateResult.isPresent()) {

operator-framework/src/main/java/com/github/containersolutions/operator/api/ResourceController.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77

88
public interface ResourceController<R extends CustomResource> {
99

10-
void deleteResource(R resource, Context<R> context);
10+
/**
11+
* The implementation should delete the associated component(s). Note that this is method is called when an object
12+
* is marked for deletion. After its executed the default finalizer is automatically removed by the framework;
13+
* unless the return value is false - note that this is almost never the case.
14+
*
15+
* @param resource
16+
* @param context
17+
* @return true - so the finalizer is automatically removed after the call.
18+
* false if you don't want to remove the finalizer. Note that this is ALMOST NEVER the case.
19+
*/
20+
boolean deleteResource(R resource, Context<R> context);
1121

1222
/**
1323
* The implementation of this operation is required to be idempotent.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.containersolutions.operator.api;
2+
3+
import com.github.containersolutions.operator.Context;
4+
import io.fabric8.kubernetes.client.CustomResource;
5+
6+
import java.util.Optional;
7+
8+
/**
9+
* Provides a more clear interface for the most common use case.
10+
*
11+
* @param <R>
12+
*/
13+
public abstract class ResourceControllerAdapter<R extends CustomResource> implements ResourceController<R> {
14+
15+
16+
@Override
17+
public boolean deleteResource(R resource, Context<R> context) {
18+
delete(resource, context);
19+
return true;
20+
}
21+
22+
@Override
23+
public Optional<R> createOrUpdateResource(R resource, Context<R> context) {
24+
createOrUpdate(resource, context);
25+
return Optional.of(resource);
26+
}
27+
28+
public abstract void delete(R resource, Context<R> context);
29+
30+
public abstract void createOrUpdate(R resource, Context<R> context);
31+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void setup() {
4242
testCustomResource.getMetadata().setFinalizers(new ArrayList<>());
4343

4444
when(resourceController.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(Optional.of(testCustomResource));
45+
when(resourceController.deleteResource(eq(testCustomResource), any())).thenReturn(true);
4546
}
4647

4748
@Test
@@ -100,6 +101,18 @@ public void removesDefaultFinalizerOnDelete() {
100101
verify(resourceOperation, times(1)).lockResourceVersion(any());
101102
}
102103

104+
@Test
105+
public void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse() {
106+
when(resourceController.deleteResource(eq(testCustomResource), any())).thenReturn(false);
107+
markForDeletion(testCustomResource);
108+
testCustomResource.getMetadata().getFinalizers().add(Controller.DEFAULT_FINALIZER);
109+
110+
eventDispatcher.eventReceived(Watcher.Action.MODIFIED, testCustomResource);
111+
112+
assertEquals(1, testCustomResource.getMetadata().getFinalizers().size());
113+
verify(resourceOperation, never()).lockResourceVersion(any());
114+
}
115+
103116
@Test
104117
public void doesNotUpdateTheResourceIfEmptyOptionalReturned() {
105118
testCustomResource.getMetadata().getFinalizers().add(Controller.DEFAULT_FINALIZER);

operator-framework/src/test/java/com/github/containersolutions/operator/sample/TestCustomResourceController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class TestCustomResourceController implements ResourceController<TestCust
1818
public static final String TEST_GROUP = "test.group";
1919

2020
@Override
21-
public void deleteResource(TestCustomResource resource, Context<TestCustomResource> context) {
21+
public boolean deleteResource(TestCustomResource resource, Context<TestCustomResource> context) {
22+
return true;
2223
}
2324

2425
@Override

samples/common/src/main/java/com/github/containersolutions/operator/sample/CustomServiceController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ public class CustomServiceController implements ResourceController<CustomService
2626
public static final String GROUP = "sample.javaoperatorsdk";
2727

2828
@Override
29-
public void deleteResource(CustomService resource, Context<CustomService> context) {
29+
public boolean deleteResource(CustomService resource, Context<CustomService> context) {
3030
log.info("Execution deleteResource for: {}", resource.getMetadata().getName());
3131
context.getK8sClient().services().inNamespace(resource.getMetadata().getNamespace())
3232
.withName(resource.getMetadata().getName()).delete();
33+
return true;
3334
}
3435

3536
@Override

spring-boot-starter/src/test/java/com/github/containersolutions/operator/spingboot/starter/TestController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class TestController implements ResourceController {
2222
public static final String KIND = "testKind";
2323

2424
@Override
25-
public void deleteResource(CustomResource resource, Context context) {
26-
25+
public boolean deleteResource(CustomResource resource, Context context) {
26+
return true;
2727
}
2828

2929
@Override

0 commit comments

Comments
 (0)