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

Dont emit event for application if context is canceled #244

Merged
merged 1 commit into from
Apr 18, 2022
Merged
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
27 changes: 26 additions & 1 deletion internal/controllers/app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (r *AppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}

scheduleResult := r.reconcile(ctx, &app, logger)
if scheduleResult.isConflictError() {
if scheduleResult.isConflictError() || isCanceledError(scheduleResult.err) {
// we don't want to create an event with this conflict error and show it to the user.
// ketch will eventually reconcile the app.
logger.Error(scheduleResult.err, "failed to reconcile app")
Expand Down Expand Up @@ -224,6 +224,19 @@ func (r appReconcileResult) isConflictError() bool {
}
}

// isCanceledError returns true if the given error is "context.Canceled" error.
func isCanceledError(err error) bool {
for {
if err == nil {
return false
}
if err == context.Canceled {
return true
}
err = errors.Unwrap(err)
}
}

type condition struct {
Type string
Reason string
Expand Down Expand Up @@ -471,6 +484,12 @@ func (r *AppReconciler) watchDeployEvents(ctx context.Context, app *ketchv1.App,
timeout := time.After(DefaultPodRunningTimeout)
for wl.ObservedGeneration < wl.Generation {
wl, err = cli.Get(ctx)
if isCanceledError(err) {
// if the context is canceled,
// probably, the app CR has been updated and controller-runtime is canceling our operation.
// we don't want to emit an event in this case
return err
}
if err != nil {
recorder.Eventf(app, v1.EventTypeWarning, ketchv1.AppReconcileError, "error getting deployments: %s", err.Error())
return err
Expand Down Expand Up @@ -577,6 +596,12 @@ func (r *AppReconciler) watchFunc(ctx context.Context, cleanup cleanupFunc, app
}

newWorkload, err := cli.Get(ctx)
if isCanceledError(err) {
// if the context is canceled,
// probably, the app CR has been updated and controller-runtime is canceling our operation.
// we don't want to emit an event in this case
return err
}
if err != nil && !k8sErrors.IsNotFound(err) {
deploymentErrorEvent := newAppDeploymentEvent(app, ketchv1.AppReconcileError, fmt.Sprintf("error getting deployments: %s", err.Error()), processName)
recorder.AnnotatedEventf(app, deploymentErrorEvent.Annotations, v1.EventTypeWarning, deploymentErrorEvent.Reason, deploymentErrorEvent.Description)
Expand Down