Skip to content

feat: re-schedule from error handler #2023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class BaseControl<T extends BaseControl<T>> {
private Long scheduleDelay = null;

public T rescheduleAfter(long delay) {
this.scheduleDelay = delay;
rescheduleAfter(Duration.ofMillis(delay));
return (T) this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.time.Duration;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;

public class ErrorStatusUpdateControl<P extends HasMetadata> {
public class ErrorStatusUpdateControl<P extends HasMetadata>
extends BaseControl<ErrorStatusUpdateControl<P>> {

private final P resource;
private final boolean patch;
private boolean noRetry = false;


public static <T extends HasMetadata> ErrorStatusUpdateControl<T> patchStatus(T resource) {
return new ErrorStatusUpdateControl<>(resource, true);
}
Expand Down Expand Up @@ -49,4 +50,16 @@ public boolean isNoRetry() {
public boolean isPatch() {
return patch;
}

/**
* If re-scheduled using this method, it is not considered as retry, it effectively cancels retry.
*
* @param delay for next execution
* @return ErrorStatusUpdateControl
*/
@Override
public ErrorStatusUpdateControl<P> rescheduleAfter(Duration delay) {
withNoRetry();
return super.rescheduleAfter(delay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,17 @@ public boolean isLastAttempt() {
.updateStatus(errorStatusUpdateControl.getResource().orElseThrow());
}
if (errorStatusUpdateControl.isNoRetry()) {
PostExecutionControl<P> postExecutionControl;
if (updatedResource != null) {
return errorStatusUpdateControl.isPatch()
postExecutionControl = errorStatusUpdateControl.isPatch()
? PostExecutionControl.customResourceStatusPatched(updatedResource)
: PostExecutionControl.customResourceUpdated(updatedResource);
} else {
return PostExecutionControl.defaultDispatch();
postExecutionControl = PostExecutionControl.defaultDispatch();
}
errorStatusUpdateControl.getScheduleDelay()
.ifPresent(postExecutionControl::withReSchedule);
return postExecutionControl;
}
} catch (RuntimeException ex) {
log.error("Error during error status handling.", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,24 @@ void retriesAddingFinalizer() {
verify(customResourceFacade, times(2)).updateResource(any());
}

@Test
void reSchedulesFromErrorHandler() {
var delay = 1000L;
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
reconciler.reconcile = (r, c) -> {
throw new IllegalStateException("Error Status Test");
};
reconciler.errorHandler =
(r, ri, e) -> ErrorStatusUpdateControl.<TestCustomResource>noStatusUpdate()
.rescheduleAfter(delay);

var res = reconciliationDispatcher.handleExecution(
new ExecutionScope(null).setResource(testCustomResource));

assertThat(res.getReScheduleDelay()).contains(delay);
assertThat(res.getRuntimeException()).isEmpty();
}

private ObservedGenCustomResource createObservedGenCustomResource() {
ObservedGenCustomResource observedGenCustomResource = new ObservedGenCustomResource();
observedGenCustomResource.setMetadata(new ObjectMeta());
Expand Down