Skip to content

Commit

Permalink
feat: logging of conflic errors
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <csviri@gmail.com>
  • Loading branch information
csviri committed Feb 2, 2024
1 parent 8342b91 commit 593a774
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.javaoperatorsdk.operator.processing.event;

import java.net.HttpURLConnection;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -10,6 +11,7 @@
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.javaoperatorsdk.operator.OperatorException;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
Expand Down Expand Up @@ -224,6 +226,7 @@ synchronized void eventProcessingFinished(
postExecutionControl);
unsetUnderExecution(resourceID);

logErrorIfNoRetryConfigured(executionScope, postExecutionControl);
// If a delete event present at this phase, it was received during reconciliation.
// So we either removed the finalizer during reconciliation or we don't use finalizers.
// Either way we don't want to retry.
Expand Down Expand Up @@ -261,6 +264,17 @@ synchronized void eventProcessingFinished(
}
}

/**
* In case retry is configured more complex error logging takes place, see handleRetryOnException
*/
private void logErrorIfNoRetryConfigured(ExecutionScope<P> executionScope,
PostExecutionControl<P> postExecutionControl) {
if (!isRetryConfigured() && postExecutionControl.exceptionDuringExecution()) {
log.error("Error during event processing {}", executionScope,
postExecutionControl.getRuntimeException().orElseThrow());
}
}

private void reScheduleExecutionIfInstructed(
PostExecutionControl<P> postExecutionControl, P customResource) {

Expand Down Expand Up @@ -302,6 +316,7 @@ private void handleRetryOnException(
boolean eventPresent = state.eventPresent();
state.markEventReceived();

retryAwareErrorLogging(state.getRetry(), eventPresent, exception, executionScope);
if (eventPresent) {
log.debug("New events exists for for resource id: {}", resourceID);
submitReconciliationExecution(state);
Expand All @@ -319,11 +334,29 @@ private void handleRetryOnException(
retryEventSource().scheduleOnce(resourceID, delay);
},
() -> {
log.error("Exhausted retries for {}", executionScope);
log.error("Exhausted retries for scope {}.", executionScope);
scheduleExecutionForMaxReconciliationInterval(executionScope.getResource());
});
}

private void retryAwareErrorLogging(RetryExecution retry, boolean eventPresent,
Exception exception,
ExecutionScope<P> executionScope) {
if (!eventPresent && !retry.isLastAttempt() && exception instanceof KubernetesClientException) {
KubernetesClientException ex = (KubernetesClientException) exception;
if (ex.getCode() == HttpURLConnection.HTTP_CONFLICT) {
log.debug("Full client conflict error during event processing {}", executionScope,
exception);
log.warn(
"Resource Kubernetes Resource Creator/Update Conflict during reconciliation. Message: {} Resource name: {}",
ex.getMessage(), ex.getFullResourceName());
return;
}
}
log.error("Error during event processing {}", executionScope,
exception);
}

private void cleanupOnSuccessfulExecution(ExecutionScope<P> executionScope) {
log.debug(
"Cleanup for successful execution for resource: {}", getName(executionScope.getResource()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public PostExecutionControl<P> handleExecution(ExecutionScope<P> executionScope)
try {
return handleDispatch(executionScope);
} catch (Exception e) {
log.error("Error during event processing {} failed.", executionScope, e);
return PostExecutionControl.exceptionDuringExecution(e);
}
}
Expand Down

0 comments on commit 593a774

Please sign in to comment.