Skip to content

Commit df529e7

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 8731191 commit df529e7

File tree

9 files changed

+163
-18
lines changed

9 files changed

+163
-18
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/ReconciliationDispatcher.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ && shouldNotDispatchToCleanupWhenMarkedForDeletion(originalResource)) {
101101

102102
// checking the cleaner for all-event-mode
103103
if (markedForDeletion && controller.isCleaner()) {
104-
return handleCleanup(resourceForExecution, originalResource, context);
104+
return handleCleanup(resourceForExecution, originalResource, context, executionScope);
105105
} else {
106106
return handleReconcile(executionScope, resourceForExecution, originalResource, context);
107107
}
@@ -166,7 +166,7 @@ private PostExecutionControl<P> reconcileExecution(
166166
P updatedCustomResource = null;
167167
if (useSSA) {
168168
if (updateControl.isNoUpdate()) {
169-
return createPostExecutionControl(null, updateControl);
169+
return createPostExecutionControl(null, updateControl, executionScope);
170170
} else {
171171
toUpdate = updateControl.getResource().orElseThrow();
172172
}
@@ -187,7 +187,7 @@ private PostExecutionControl<P> reconcileExecution(
187187
if (updateControl.isPatchStatus()) {
188188
customResourceFacade.patchStatus(toUpdate, originalResource);
189189
}
190-
return createPostExecutionControl(updatedCustomResource, updateControl);
190+
return createPostExecutionControl(updatedCustomResource, updateControl, executionScope);
191191
}
192192

193193
private PostExecutionControl<P> handleErrorStatusHandler(
@@ -247,25 +247,40 @@ public boolean isLastAttempt() {
247247
}
248248

249249
private PostExecutionControl<P> createPostExecutionControl(
250-
P updatedCustomResource, UpdateControl<P> updateControl) {
250+
P updatedCustomResource, UpdateControl<P> updateControl, ExecutionScope<P> executionScope) {
251251
PostExecutionControl<P> postExecutionControl;
252252
if (updatedCustomResource != null) {
253253
postExecutionControl =
254254
PostExecutionControl.customResourceStatusPatched(updatedCustomResource);
255255
} else {
256256
postExecutionControl = PostExecutionControl.defaultDispatch();
257257
}
258-
updatePostExecutionControlWithReschedule(postExecutionControl, updateControl);
258+
updatePostExecutionControlWithReschedule(postExecutionControl, updateControl, executionScope);
259259
return postExecutionControl;
260260
}
261261

262+
// todo test
262263
private void updatePostExecutionControlWithReschedule(
263-
PostExecutionControl<P> postExecutionControl, BaseControl<?> baseControl) {
264-
baseControl.getScheduleDelay().ifPresent(postExecutionControl::withReSchedule);
264+
PostExecutionControl<P> postExecutionControl,
265+
BaseControl<?> baseControl,
266+
ExecutionScope<P> executionScope) {
267+
baseControl
268+
.getScheduleDelay()
269+
.ifPresent(
270+
r -> {
271+
if (executionScope.isDeleteEvent()) {
272+
log.warn("No re-schedules allowed when delete event present. Will be ignored.");
273+
} else {
274+
postExecutionControl.withReSchedule(r);
275+
}
276+
});
265277
}
266278

267279
private PostExecutionControl<P> handleCleanup(
268-
P resourceForExecution, P originalResource, Context<P> context) {
280+
P resourceForExecution,
281+
P originalResource,
282+
Context<P> context,
283+
ExecutionScope<P> executionScope) {
269284
if (log.isDebugEnabled()) {
270285
log.debug(
271286
"Executing delete for resource: {} with version: {}",
@@ -274,7 +289,7 @@ private PostExecutionControl<P> handleCleanup(
274289
}
275290
DeleteControl deleteControl = controller.cleanup(resourceForExecution, context);
276291
final var useFinalizer = controller.useFinalizer();
277-
if (useFinalizer) {
292+
if (useFinalizer && !configuration().isAllEventReconcileMode()) {
278293
// note that we don't reschedule here even if instructed. Removing finalizer means that
279294
// cleanup is finished, nothing left to be done
280295
final var finalizerName = configuration().getFinalizerName();
@@ -308,7 +323,7 @@ private PostExecutionControl<P> handleCleanup(
308323
deleteControl,
309324
useFinalizer);
310325
PostExecutionControl<P> postExecutionControl = PostExecutionControl.defaultDispatch();
311-
updatePostExecutionControlWithReschedule(postExecutionControl, deleteControl);
326+
updatePostExecutionControlWithReschedule(postExecutionControl, deleteControl, executionScope);
312327
return postExecutionControl;
313328
}
314329

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/alleventmode/AbstractAllEventReconciler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public class AbstractAllEventReconciler {
66

77
public static final String FINALIZER = "all.event.mode/finalizer";
8+
public static final String ADDITIONAL_FINALIZER = "all.event.mode/finalizer2";
89

910
private boolean resourceEvent = false;
1011
private boolean deleteEvent = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.javaoperatorsdk.operator.baseapi.alleventmode.cleaner;
2+
3+
import io.fabric8.kubernetes.api.model.Namespaced;
4+
import io.fabric8.kubernetes.client.CustomResource;
5+
import io.fabric8.kubernetes.model.annotation.Group;
6+
import io.fabric8.kubernetes.model.annotation.ShortNames;
7+
import io.fabric8.kubernetes.model.annotation.Version;
8+
9+
@Group("sample.javaoperatorsdk")
10+
@Version("v1")
11+
@ShortNames("aeccs")
12+
public class AllEventCleanerCustomResource extends CustomResource<AllEventCleanerSpec, Void>
13+
implements Namespaced {}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,56 @@
11
package io.javaoperatorsdk.operator.baseapi.alleventmode.cleaner;
22

3-
public class AllEventCleanerIT {}
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.RegisterExtension;
5+
6+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
7+
import io.javaoperatorsdk.operator.baseapi.alleventmode.onlyreconcile.AllEventCustomResource;
8+
import io.javaoperatorsdk.operator.baseapi.alleventmode.onlyreconcile.AllEventReconciler;
9+
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
10+
11+
import static io.javaoperatorsdk.operator.baseapi.alleventmode.AbstractAllEventReconciler.FINALIZER;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.awaitility.Awaitility.await;
14+
15+
public class AllEventCleanerIT {
16+
17+
public static final String TEST = "test1";
18+
19+
@RegisterExtension
20+
LocallyRunOperatorExtension extension =
21+
LocallyRunOperatorExtension.builder().withReconciler(new AllEventCleanerReconciler()).build();
22+
23+
@Test
24+
void eventsPresent() {
25+
var reconciler = extension.getReconcilerOfType(AllEventReconciler.class);
26+
extension.serverSideApply(testResource());
27+
28+
await()
29+
.untilAsserted(
30+
() -> {
31+
assertThat(reconciler.isResourceEvent()).isTrue();
32+
assertThat(getResource().hasFinalizer(FINALIZER)).isTrue();
33+
});
34+
35+
extension.delete(getResource());
36+
37+
await()
38+
.untilAsserted(
39+
() -> {
40+
var r = getResource();
41+
assertThat(r).isNull();
42+
assertThat(reconciler.isDeleteEvent()).isTrue();
43+
assertThat(reconciler.isEventOnMarkedForDeletion()).isTrue();
44+
});
45+
}
46+
47+
AllEventCustomResource getResource() {
48+
return extension.get(AllEventCustomResource.class, TEST);
49+
}
50+
51+
AllEventCustomResource testResource() {
52+
var res = new AllEventCustomResource();
53+
res.setMetadata(new ObjectMetaBuilder().withName(TEST).build());
54+
return res;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.javaoperatorsdk.operator.baseapi.alleventmode.cleaner;
2+
3+
import io.javaoperatorsdk.operator.api.config.ControllerMode;
4+
import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
5+
import io.javaoperatorsdk.operator.api.reconciler.Context;
6+
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
7+
import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
8+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
9+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
10+
import io.javaoperatorsdk.operator.baseapi.alleventmode.AbstractAllEventReconciler;
11+
12+
@ControllerConfiguration(mode = ControllerMode.RECONCILE_ALL_EVENT)
13+
public class AllEventCleanerReconciler extends AbstractAllEventReconciler
14+
implements Reconciler<AllEventCleanerCustomResource>, Cleaner<AllEventCleanerCustomResource> {
15+
16+
@Override
17+
public UpdateControl<AllEventCleanerCustomResource> reconcile(
18+
AllEventCleanerCustomResource resource, Context<AllEventCleanerCustomResource> context) {
19+
20+
increaseEventCount();
21+
if (!resource.isMarkedForDeletion()) {
22+
setResourceEvent(true);
23+
}
24+
25+
if (!resource.hasFinalizer(FINALIZER)) {
26+
resource.addFinalizer(FINALIZER);
27+
context.getClient().resource(resource).update();
28+
return UpdateControl.noUpdate();
29+
}
30+
31+
return UpdateControl.noUpdate();
32+
}
33+
34+
@Override
35+
public DeleteControl cleanup(
36+
AllEventCleanerCustomResource resource, Context<AllEventCleanerCustomResource> context)
37+
throws Exception {
38+
39+
increaseEventCount();
40+
if (resource.isMarkedForDeletion() && !context.isDeleteEventPresent()) {
41+
setEventOnMarkedForDeletion(true);
42+
if (resource.hasFinalizer(FINALIZER)) {
43+
resource.removeFinalizer(FINALIZER);
44+
context.getClient().resource(resource).update();
45+
}
46+
}
47+
48+
if (context.isDeleteEventPresent()) {
49+
setDeleteEvent(true);
50+
}
51+
// todo handle this document
52+
return DeleteControl.defaultDelete();
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.javaoperatorsdk.operator.baseapi.alleventmode.cleaner;
2+
3+
public class AllEventCleanerSpec {
4+
5+
private String value;
6+
7+
public String getValue() {
8+
return value;
9+
}
10+
11+
public void setValue(String value) {
12+
this.value = value;
13+
}
14+
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/alleventmode/cleanerfinalizer/AllEventCleanerFinalizerIT.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/alleventmode/finalizer/AllEventFinalizerIT.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/alleventmode/onlyreconcile/AllEventIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public class AllEventIT {
1818
LocallyRunOperatorExtension extension =
1919
LocallyRunOperatorExtension.builder().withReconciler(new AllEventReconciler()).build();
2020

21+
// todo additional finalizer
22+
// todo retry
2123
@Test
2224
void eventsPresent() {
2325
var reconciler = extension.getReconcilerOfType(AllEventReconciler.class);
2426
extension.serverSideApply(testResource());
25-
2627
await()
2728
.untilAsserted(
2829
() -> {

0 commit comments

Comments
 (0)