Skip to content
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

feat: logging of conflict exceptions #2230

Merged
merged 1 commit into from
Feb 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
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