Skip to content

Commit dfe897e

Browse files
authored
fix: fast namespace deletion (#2617)
* fix: fast namespace deletion Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix tests Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
1 parent 17b1260 commit dfe897e

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

pkg/cleanup/cleaner/cleaner.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@ type Cleaner interface {
2525
Run(ctx context.Context, stepReport *model.StepReport) []error
2626
}
2727

28-
func New(timeout time.Duration, delay *time.Duration, propagation metav1.DeletionPropagation) Cleaner {
28+
func New(timeout time.Duration, waitForDeletion bool, delay *time.Duration, propagation metav1.DeletionPropagation) Cleaner {
2929
return &cleaner{
30-
delay: delay,
31-
timeout: timeout,
32-
propagation: propagation,
30+
delay: delay,
31+
timeout: timeout,
32+
propagation: propagation,
33+
waitForDeletion: waitForDeletion,
3334
}
3435
}
3536

3637
type cleaner struct {
37-
delay *time.Duration
38-
timeout time.Duration
39-
propagation metav1.DeletionPropagation
40-
entries []cleanupEntry
38+
delay *time.Duration
39+
timeout time.Duration
40+
propagation metav1.DeletionPropagation
41+
waitForDeletion bool
42+
entries []cleanupEntry
4143
}
4244

4345
func (c *cleaner) Add(client client.Client, object client.Object) {
@@ -82,8 +84,10 @@ func (c *cleaner) delete(ctx context.Context, entry cleanupEntry) error {
8284
if !kerrors.IsNotFound(err) {
8385
return err
8486
}
85-
} else if err := client.WaitForDeletion(ctx, entry.client, entry.object); err != nil {
86-
return err
87+
} else if c.waitForDeletion {
88+
if err := client.WaitForDeletion(ctx, entry.client, entry.object); err != nil {
89+
return err
90+
}
8791
}
8892
return nil
8993
}

pkg/cleanup/cleaner/cleaner_test.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,46 @@ import (
1919

2020
func TestNew(t *testing.T) {
2121
tests := []struct {
22-
name string
23-
timeout time.Duration
24-
delay *time.Duration
25-
want Cleaner
22+
name string
23+
timeout time.Duration
24+
delay *time.Duration
25+
waitForDeletion bool
26+
want Cleaner
2627
}{{
27-
name: "with timeout",
28-
timeout: time.Minute,
29-
delay: nil,
28+
name: "with timeout",
29+
timeout: time.Minute,
30+
delay: nil,
31+
waitForDeletion: false,
3032
want: &cleaner{
3133
timeout: time.Minute,
3234
delay: nil,
3335
propagation: metav1.DeletePropagationBackground,
3436
},
3537
}, {
36-
name: "with delay",
37-
timeout: time.Minute,
38-
delay: ptr.To(10 * time.Second),
38+
name: "with delay",
39+
timeout: time.Minute,
40+
delay: ptr.To(10 * time.Second),
41+
waitForDeletion: false,
3942
want: &cleaner{
4043
timeout: time.Minute,
4144
delay: ptr.To(10 * time.Second),
4245
propagation: metav1.DeletePropagationBackground,
4346
},
47+
}, {
48+
name: "with waitForDeletion",
49+
timeout: time.Minute,
50+
delay: nil,
51+
waitForDeletion: true,
52+
want: &cleaner{
53+
timeout: time.Minute,
54+
delay: nil,
55+
propagation: metav1.DeletePropagationBackground,
56+
waitForDeletion: true,
57+
},
4458
}}
4559
for _, tt := range tests {
4660
t.Run(tt.name, func(t *testing.T) {
47-
got := New(tt.timeout, tt.delay, metav1.DeletePropagationBackground)
61+
got := New(tt.timeout, tt.waitForDeletion, tt.delay, metav1.DeletePropagationBackground)
4862
assert.Equal(t, tt.want, got)
4963
})
5064
}
@@ -193,9 +207,10 @@ func Test_cleaner_Run(t *testing.T) {
193207
for _, tt := range tests {
194208
t.Run(tt.name, func(t *testing.T) {
195209
c := &cleaner{
196-
delay: ptr.To(time.Second),
197-
timeout: 1 * time.Second,
198-
entries: tt.entries,
210+
delay: ptr.To(time.Second),
211+
timeout: 1 * time.Second,
212+
entries: tt.entries,
213+
waitForDeletion: true,
199214
}
200215
got := c.Run(context.TODO(), &model.StepReport{})
201216
assert.Equal(t, tt.want, got)

pkg/runner/runner.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runner
33
import (
44
"context"
55
"fmt"
6+
"sync"
67
"testing"
78
"time"
89

@@ -72,10 +73,15 @@ func (r *runner) run(ctx context.Context, m mainstart, nsOptions v1alpha2.Namesp
7273
ctx = logging.WithSink(ctx, newSink(r.clock, tc.Quiet(), t.Log))
7374
// setup logger
7475
ctx = logging.WithLogger(ctx, logging.NewLogger(t.Name(), "", "@chainsaw"))
76+
wg := sync.WaitGroup{}
77+
t.Cleanup(func() {
78+
wg.Wait()
79+
})
7580
// setup cleanup
76-
cleanup := cleaner.New(tc.Timeouts().Cleanup, nil, tc.DeletionPropagation())
81+
cleanup := cleaner.New(tc.Timeouts().Cleanup, false, nil, tc.DeletionPropagation())
7782
t.Cleanup(func() {
7883
fail(t, r.cleanup(ctx, tc, cleanup))
84+
logging.Log(ctx, logging.Internal, logging.LogStatus, nil, color.BoldRed)
7985
})
8086
// setup namespace
8187
tc, err := r.setupNamespace(ctx, nsOptions, tc, cleanup)
@@ -103,6 +109,8 @@ func (r *runner) run(ctx context.Context, m mainstart, nsOptions v1alpha2.Namesp
103109
// helper to run test
104110
runTest := func(ctx context.Context, t *testing.T, testId int, scenarioId int, scenarioName string, tc enginecontext.TestContext, bindings ...v1alpha1.Binding) {
105111
t.Helper()
112+
wg.Add(1)
113+
defer wg.Done()
106114
// setup logger sink
107115
ctx = logging.WithSink(ctx, newSink(r.clock, tc.Quiet(), t.Log))
108116
// setup concurrency
@@ -147,7 +155,7 @@ func (r *runner) run(ctx context.Context, m mainstart, nsOptions v1alpha2.Namesp
147155
return
148156
}
149157
// setup cleaner
150-
cleanup := cleaner.New(tc.Timeouts().Cleanup, nil, tc.DeletionPropagation())
158+
cleanup := cleaner.New(tc.Timeouts().Cleanup, false, nil, tc.DeletionPropagation())
151159
t.Cleanup(func() {
152160
fail(t, r.testCleanup(ctx, tc, cleanup, report))
153161
})
@@ -267,7 +275,7 @@ func (r *runner) runStep(
267275
r.onFail()
268276
return true
269277
}
270-
cleaner := cleaner.New(tc.Timeouts().Cleanup, tc.DelayBeforeCleanup(), tc.DeletionPropagation())
278+
cleaner := cleaner.New(tc.Timeouts().Cleanup, true, tc.DelayBeforeCleanup(), tc.DeletionPropagation())
271279
cleanup(func() {
272280
if !cleaner.Empty() || len(step.Cleanup) != 0 {
273281
report := &model.StepReport{

0 commit comments

Comments
 (0)