Skip to content

Commit 36320f9

Browse files
committed
infra/capi: add a platform-specific post-destroy hook
For some platforms, it might be necessary to execute destroy tasks _after_ the cluster-api system has stopped so that it won't try to reconcile the changes. This commits adds a post-destroy hook that runs during the bootstrap destroy process but after cluster-api has shutdown.
1 parent 6bc75df commit 36320f9

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

pkg/infrastructure/clusterapi/clusterapi.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ func (i *InfraProvider) DestroyBootstrap(ctx context.Context, dir string) error
394394

395395
machineDeletionTimeout := 5 * time.Minute
396396
logrus.Infof("Waiting up to %v for bootstrap machine deletion %s/%s...", machineDeletionTimeout, machineNamespace, machineName)
397-
ctx, cancel := context.WithTimeout(ctx, machineDeletionTimeout)
398-
wait.UntilWithContext(ctx, func(context.Context) {
399-
err := sys.Client().Get(ctx, client.ObjectKey{
397+
cctx, cancel := context.WithTimeout(ctx, machineDeletionTimeout)
398+
wait.UntilWithContext(cctx, func(context.Context) {
399+
err := sys.Client().Get(cctx, client.ObjectKey{
400400
Name: machineName,
401401
Namespace: machineNamespace,
402402
}, &clusterv1.Machine{})
@@ -410,14 +410,25 @@ func (i *InfraProvider) DestroyBootstrap(ctx context.Context, dir string) error
410410
}
411411
}, 2*time.Second)
412412

413-
err = ctx.Err()
413+
err = cctx.Err()
414414
if err != nil && !errors.Is(err, context.Canceled) {
415415
logrus.Warnf("Timeout deleting bootstrap machine: %s", err)
416-
} else {
417-
logrus.Infof("Finished destroying bootstrap resources")
418416
}
419417
clusterapi.System().Teardown()
420418

419+
if p, ok := i.impl.(PostDestroyer); ok {
420+
postDestroyInput := PostDestroyerInput{
421+
Metadata: *metadata,
422+
}
423+
if err := p.PostDestroy(ctx, postDestroyInput); err != nil {
424+
return fmt.Errorf("failed during post-destroy hook: %w", err)
425+
}
426+
logrus.Debugf("Finished running post-destroy hook")
427+
} else {
428+
logrus.Infof("no post-destroy requirements for the %s provider", i.impl.Name())
429+
}
430+
431+
logrus.Infof("Finished destroying bootstrap resources")
421432
return nil
422433
}
423434

pkg/infrastructure/clusterapi/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,14 @@ type BootstrapDestroyInput struct {
106106
Client client.Client
107107
Metadata types.ClusterMetadata
108108
}
109+
110+
// PostDestroyer allows platform-specific behavior after bootstrap has been destroyed and
111+
// ClusterAPI has stopped running.
112+
type PostDestroyer interface {
113+
PostDestroy(ctx context.Context, in PostDestroyerInput) error
114+
}
115+
116+
// PostDestroyerInput collects args passed to the PostDestroyer hook.
117+
type PostDestroyerInput struct {
118+
Metadata types.ClusterMetadata
119+
}

0 commit comments

Comments
 (0)