From 97184c36a69ef3be0b1e42bd73b57e00f27fcb79 Mon Sep 17 00:00:00 2001 From: joey Date: Wed, 25 Oct 2023 10:35:29 +0800 Subject: [PATCH] fix wait entrypoint cancellation error log output. context canceled should be treated as normal completion Signed-off-by: chengjoey --- cmd/entrypoint/main.go | 2 +- pkg/entrypoint/entrypointer.go | 12 +++++++++++- pkg/entrypoint/entrypointer_test.go | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cmd/entrypoint/main.go b/cmd/entrypoint/main.go index 9f690e14113..54422d789e5 100644 --- a/cmd/entrypoint/main.go +++ b/cmd/entrypoint/main.go @@ -183,7 +183,7 @@ func main() { log.Print(err.Error()) os.Exit(1) case entrypoint.ContextError: - if errors.Is(err, entrypoint.ErrContextCanceled) { + if entrypoint.IsContextCanceledError(err) { log.Print("Step was cancelled") // use the SIGKILL signal to distinguish normal exit programs, just like kill -9 PID os.Exit(int(syscall.SIGKILL)) diff --git a/pkg/entrypoint/entrypointer.go b/pkg/entrypoint/entrypointer.go index ba0efd66aba..ac7baf16dfb 100644 --- a/pkg/entrypoint/entrypointer.go +++ b/pkg/entrypoint/entrypointer.go @@ -60,6 +60,16 @@ var ( ErrContextCanceled = ContextError(context.Canceled.Error()) ) +// IsContextDeadlineError determine whether the error is context deadline +func IsContextDeadlineError(err error) bool { + return errors.Is(err, ErrContextDeadlineExceeded) +} + +// IsContextCanceledError determine whether the error is context canceled +func IsContextCanceledError(err error) bool { + return errors.Is(err, ErrContextCanceled) +} + // Entrypointer holds fields for running commands with redirected // entrypoints. type Entrypointer struct { @@ -174,7 +184,7 @@ func (e Entrypointer) Go() error { defer cancel() // start a goroutine to listen for cancellation file go func() { - if err := e.waitingCancellation(ctx, cancel); err != nil { + if err := e.waitingCancellation(ctx, cancel); err != nil && (!IsContextCanceledError(err) && !IsContextDeadlineError(err)) { logger.Error("Error while waiting for cancellation", zap.Error(err)) } }() diff --git a/pkg/entrypoint/entrypointer_test.go b/pkg/entrypoint/entrypointer_test.go index 06113dbb628..84731daeb24 100644 --- a/pkg/entrypoint/entrypointer_test.go +++ b/pkg/entrypoint/entrypointer_test.go @@ -686,6 +686,28 @@ func TestEntrypointerStopOnCancel(t *testing.T) { } } +func TestIsContextDeadlineError(t *testing.T) { + ctxErr := ContextError(context.DeadlineExceeded.Error()) + if !IsContextDeadlineError(ctxErr) { + t.Errorf("expected context deadline error, got %v", ctxErr) + } + normalErr := ContextError("normal error") + if IsContextDeadlineError(normalErr) { + t.Errorf("expected normal error, got %v", normalErr) + } +} + +func TestIsContextCanceledError(t *testing.T) { + ctxErr := ContextError(context.Canceled.Error()) + if !IsContextCanceledError(ctxErr) { + t.Errorf("expected context canceled error, got %v", ctxErr) + } + normalErr := ContextError("normal error") + if IsContextCanceledError(normalErr) { + t.Errorf("expected normal error, got %v", normalErr) + } +} + type fakeWaiter struct { sync.Mutex waited []string