Skip to content

Commit f179603

Browse files
committed
fix: fix the bug in IgnoreTeardownUntil logic
The recently added `IgnoreTeardownWhile` function to the QTransform controllers introduced a bug in the existing `IgnoreTeardownUntil` logic: we were wrongly short-circuiting in the finalizer check loop, causing the controller to run teardown prematurely. Fix the logic and modify the `IgnoreTeardownUntil` test to catch the issue before the fix. Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
1 parent de18545 commit f179603

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

pkg/controller/generic/qtransform/qtransform.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,23 @@ func (ctrl *QController[Input, Output]) Reconcile(ctx context.Context, logger *z
198198
case resource.PhaseTearingDown:
199199
ignoreTearingDown := false
200200

201-
// if there's an option to ignore finalizers, check if we should ignore tearing down
202-
// and perform "normal" reconcile instead
203-
if ctrl.options.ignoreTeardownUntilFinalizers != nil || ctrl.options.ignoreTeardownWhileFinalizers != nil {
204-
for _, fin := range *in.Metadata().Finalizers() {
205-
if fin == ctrl.ControllerName {
206-
continue
207-
}
201+
for _, fin := range *in.Metadata().Finalizers() {
202+
if fin == ctrl.ControllerName {
203+
continue
204+
}
205+
206+
if ctrl.options.ignoreTeardownUntilFinalizers != nil {
207+
if _, allowed := ctrl.options.ignoreTeardownUntilFinalizers[fin]; !allowed {
208+
ignoreTearingDown = true // found an unexpected finalizer, skip teardown
208209

209-
if _, present = ctrl.options.ignoreTeardownUntilFinalizers[fin]; present {
210-
continue
210+
break
211211
}
212+
}
212213

213-
if _, present = ctrl.options.ignoreTeardownWhileFinalizers[fin]; present {
214+
// Check ignoreTeardownWhileFinalizers
215+
if ctrl.options.ignoreTeardownWhileFinalizers != nil {
216+
if _, present := ctrl.options.ignoreTeardownWhileFinalizers[fin]; present {
217+
// Found a matching "while" finalizer, skip teardown
214218
ignoreTearingDown = true
215219

216220
break

pkg/controller/generic/qtransform/qtransform_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ func TestDestroy(t *testing.T) {
417417
})
418418
}
419419

420-
//nolint:dupl
421420
func TestDestroyWithIgnoreTeardownUntil(t *testing.T) {
422421
setup(t, func(ctx context.Context, st state.State, runtime *runtime.Runtime) {
423422
require.NoError(t, runtime.RegisterQController(NewABCController(qtransform.WithIgnoreTeardownUntil("extra-finalizer"))))
@@ -437,7 +436,7 @@ func TestDestroyWithIgnoreTeardownUntil(t *testing.T) {
437436
rtestutils.AssertNoResource[*B](ctx, t, st, "transformed-1")
438437

439438
// add two finalizers to '2'
440-
require.NoError(t, st.AddFinalizer(ctx, NewA("2", ASpec{}).Metadata(), "extra-finalizer", "other-finalizer"))
439+
require.NoError(t, st.AddFinalizer(ctx, NewA("2", ASpec{}).Metadata(), "extra-finalizer", "other-finalizer", "yet-another-finalizer"))
441440

442441
// teardown input '2'
443442
_, err := st.Teardown(ctx, NewA("2", ASpec{}).Metadata())
@@ -451,6 +450,14 @@ func TestDestroyWithIgnoreTeardownUntil(t *testing.T) {
451450
// remove other-finalizer
452451
require.NoError(t, st.RemoveFinalizer(ctx, NewA("2", ASpec{}).Metadata(), "other-finalizer"))
453452

453+
// the output 'transformed-2' should still not be torn down yet
454+
rtestutils.AssertResources(ctx, t, st, []resource.ID{"transformed-2"}, func(r *B, asrt *assert.Assertions) {
455+
asrt.Equal(resource.PhaseRunning, r.Metadata().Phase())
456+
})
457+
458+
// remove yet-another-finalizer
459+
require.NoError(t, st.RemoveFinalizer(ctx, NewA("2", ASpec{}).Metadata(), "yet-another-finalizer"))
460+
454461
// the output 'transformed-2' should be destroyed now
455462
rtestutils.AssertNoResource[*B](ctx, t, st, "transformed-2")
456463

@@ -467,7 +474,6 @@ func TestDestroyWithIgnoreTeardownUntil(t *testing.T) {
467474
})
468475
}
469476

470-
//nolint:dupl
471477
func TestDestroyWithIgnoreTeardownWhile(t *testing.T) {
472478
setup(t, func(ctx context.Context, st state.State, runtime *runtime.Runtime) {
473479
require.NoError(t, runtime.RegisterQController(NewABCController(qtransform.WithIgnoreTeardownWhile("extra-finalizer"))))

0 commit comments

Comments
 (0)