From de1fbb86947df8b7a2f2d4a902f855050321dca5 Mon Sep 17 00:00:00 2001 From: Dmitry Shmulevich Date: Fri, 17 May 2024 10:44:10 -0700 Subject: [PATCH] fixed error when deleting already deleted object Signed-off-by: Dmitry Shmulevich --- pkg/engine/engine.go | 21 ++++++++++++++++----- pkg/engine/engine_test.go | 9 +++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 4c46907..947dee5 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -35,7 +35,7 @@ import ( type Engine interface { RunTask(context.Context, *config.Task) error Reset(context.Context) error - DeleteAllObjects(context.Context) error + DeleteAllObjects(context.Context) } type Eng struct { @@ -263,11 +263,23 @@ func (eng *Eng) Reset(ctx context.Context) error { ctx, cancel := context.WithTimeout(ctx, eng.cleanup.Timeout) defer cancel() - return eng.DeleteAllObjects(ctx) + stop := make(chan struct{}) + + go func() { + eng.DeleteAllObjects(ctx) + stop <- struct{}{} + }() + + select { + case <-stop: + return nil + case <-ctx.Done(): + return ctx.Err() + } } // DeleteAllObjects deletes all objects -func (eng *Eng) DeleteAllObjects(ctx context.Context) error { +func (eng *Eng) DeleteAllObjects(ctx context.Context) { deletePolicy := metav1.DeletePropagationBackground deletions := metav1.DeleteOptions{ PropagationPolicy: &deletePolicy, @@ -278,11 +290,10 @@ func (eng *Eng) DeleteAllObjects(ctx context.Context) error { for _, name := range objInfo.Names { err := eng.dynamicClient.Resource(objInfo.GVR).Namespace(ns).Delete(ctx, name, deletions) if err != nil { - return err + eng.log.Info("Warning: cannot delete object", "name", name, "err", err.Error()) } } } eng.log.Info("Deleted all objects") - return nil } diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 0ddca66..7d6056a 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -40,9 +40,8 @@ var ( ) type testEngine struct { - execErr error - resetErr error - deleteErr error + execErr error + resetErr error } func (eng *testEngine) RunTask(context.Context, *config.Task) error { @@ -53,9 +52,7 @@ func (eng *testEngine) Reset(context.Context) error { return eng.resetErr } -func (eng *testEngine) DeleteAllObjects(context.Context) error { - return eng.deleteErr -} +func (eng *testEngine) DeleteAllObjects(context.Context) {} func TestRunEngine(t *testing.T) { testCases := []struct {