Skip to content

Commit 95a9ca4

Browse files
committed
feat: support non blocking mode in TeardownAndDestroy state method
This mode is useful in the controllers. It can call this method repeatedly. Signed-off-by: Artem Chernyshev <artem.chernyshev@talos-systems.com>
1 parent b1de5a5 commit 95a9ca4

4 files changed

Lines changed: 48 additions & 14 deletions

File tree

pkg/state/conformance/state.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,12 +1406,14 @@ func (suite *StateSuite) TestContextWithTeardown() {
14061406
func (suite *StateSuite) TestTeardownAndDestroy() {
14071407
ns := suite.getNamespace()
14081408

1409-
res := NewPathResource(ns, "tmp/4")
1409+
path1 := NewPathResource(ns, "tmp/4")
1410+
path2 := NewPathResource(ns, "tmp/5")
14101411

14111412
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
14121413
defer cancel()
14131414

1414-
finalizer := "A"
1415+
finalizerA := "A"
1416+
finalizerB := "B"
14151417

14161418
eg := errgroup.Group{}
14171419

@@ -1436,23 +1438,42 @@ func (suite *StateSuite) TestTeardownAndDestroy() {
14361438
continue
14371439
}
14381440

1439-
if !e.Resource.Metadata().Finalizers().Has(finalizer) {
1441+
if !e.Resource.Metadata().Finalizers().Has(finalizerA) {
14401442
continue
14411443
}
14421444

1443-
if err = suite.State.RemoveFinalizer(ctx, e.Resource.Metadata(), finalizer); err != nil {
1445+
if err = suite.State.RemoveFinalizer(ctx, e.Resource.Metadata(), finalizerA); err != nil {
14441446
return err
14451447
}
14461448
}
14471449
}
14481450
})
14491451

1450-
suite.Require().NoError(suite.State.Create(ctx, res))
1452+
suite.Require().NoError(suite.State.Create(ctx, path1))
1453+
suite.Require().NoError(suite.State.Create(ctx, path2))
1454+
1455+
suite.Assert().NoError(suite.State.AddFinalizer(ctx, path1.Metadata(), finalizerA))
1456+
suite.Assert().NoError(suite.State.AddFinalizer(ctx, path2.Metadata(), finalizerB))
14511457

1452-
suite.Assert().NoError(suite.State.AddFinalizer(ctx, res.Metadata(), finalizer))
1458+
_, err := suite.State.TeardownAndDestroy(ctx, path1.Metadata())
1459+
suite.Require().NoError(err)
1460+
1461+
ready, err := suite.State.TeardownAndDestroy(ctx, path2.Metadata(), state.WithNoBlocking())
1462+
suite.Require().NoError(err)
1463+
suite.Assert().False(ready)
14531464

1454-
err := suite.State.TeardownAndDestroy(ctx, res.Metadata())
1465+
r, err := suite.State.Get(ctx, path2.Metadata())
14551466
suite.Require().NoError(err)
1467+
suite.Assert().Equal(resource.PhaseTearingDown, r.Metadata().Phase())
1468+
1469+
suite.Assert().NoError(suite.State.RemoveFinalizer(ctx, path2.Metadata(), finalizerB))
1470+
1471+
ready, err = suite.State.TeardownAndDestroy(ctx, path2.Metadata(), state.WithNoBlocking())
1472+
suite.Assert().True(ready)
1473+
suite.Require().NoError(err)
1474+
1475+
_, err = suite.State.Get(ctx, path2.Metadata())
1476+
suite.Require().True(state.IsNotFoundError(err))
14561477

14571478
cancel()
14581479

pkg/state/options.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ type TeardownAndDestroyOption func(*TeardownAndDestroyOptions)
157157

158158
// TeardownAndDestroyOptions for the CoreState.TeardownAndDestroy function.
159159
type TeardownAndDestroyOptions struct {
160-
Owner string
160+
Owner string
161+
NonBlocking bool
161162
}
162163

163164
// WithTeardownAndDestroyOwner checks an owner on the object being destroyed.
@@ -167,6 +168,14 @@ func WithTeardownAndDestroyOwner(owner string) TeardownAndDestroyOption {
167168
}
168169
}
169170

171+
// WithNoBlocking makes teardown and destroy not wait for finalizers empty.
172+
// Then if the finalizers empty it will try destroying the resource.
173+
func WithNoBlocking() TeardownAndDestroyOption {
174+
return func(opts *TeardownAndDestroyOptions) {
175+
opts.NonBlocking = true
176+
}
177+
}
178+
170179
// WatchOptions for the CoreState.Watch function.
171180
type WatchOptions struct {
172181
StartFromBookmark Bookmark

pkg/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,5 @@ type State interface {
143143
// If a resource doesn't exist, error is returned.
144144
// It's not an error to tear down a resource which is already being torn down.
145145
// The call blocks until all resource finalizers are empty.
146-
TeardownAndDestroy(context.Context, resource.Pointer, ...TeardownAndDestroyOption) error
146+
TeardownAndDestroy(context.Context, resource.Pointer, ...TeardownAndDestroyOption) (bool, error)
147147
}

pkg/state/wrap.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (state coreWrapper) ContextWithTeardown(ctx context.Context, resourcePointe
214214
// If a resource doesn't exist, error is returned.
215215
// It's not an error to tear down a resource which is already being torn down.
216216
// The call blocks until all resource finalizers are empty.
217-
func (state coreWrapper) TeardownAndDestroy(ctx context.Context, resourcePointer resource.Pointer, opts ...TeardownAndDestroyOption) error {
217+
func (state coreWrapper) TeardownAndDestroy(ctx context.Context, resourcePointer resource.Pointer, opts ...TeardownAndDestroyOption) (bool, error) {
218218
var options TeardownAndDestroyOptions
219219

220220
for _, opt := range opts {
@@ -223,17 +223,21 @@ func (state coreWrapper) TeardownAndDestroy(ctx context.Context, resourcePointer
223223

224224
ready, err := state.Teardown(ctx, resourcePointer, WithTeardownOwner(options.Owner))
225225
if err != nil {
226-
return err
226+
return ready, err
227227
}
228228

229229
if ready {
230-
return state.Destroy(ctx, resourcePointer, WithDestroyOwner(options.Owner))
230+
return ready, state.Destroy(ctx, resourcePointer, WithDestroyOwner(options.Owner))
231+
}
232+
233+
if options.NonBlocking {
234+
return ready, nil
231235
}
232236

233237
_, err = state.WatchFor(ctx, resourcePointer, WithFinalizerEmpty())
234238
if err != nil {
235-
return err
239+
return ready, err
236240
}
237241

238-
return state.Destroy(ctx, resourcePointer, WithDestroyOwner(options.Owner))
242+
return true, state.Destroy(ctx, resourcePointer, WithDestroyOwner(options.Owner))
239243
}

0 commit comments

Comments
 (0)