Skip to content

Commit

Permalink
fixed error when deleting already deleted object (#45)
Browse files Browse the repository at this point in the history
When deleting with "-cleanup" flag, some of the objects might have
already expired or been deleted previously.
In such cases knavigator prints a warning instead of returning an error.

Signed-off-by: Dmitry Shmulevich <[email protected]>
  • Loading branch information
dmitsh authored May 17, 2024
1 parent 7456f5c commit 8be8b2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
21 changes: 16 additions & 5 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
9 changes: 3 additions & 6 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 8be8b2b

Please sign in to comment.