-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cleanup tearing down machine links without finalizers
This should help with the cleanup of machines removed from Omni completely. If the link is torn down, it will automatically trigger the removal of `MachineSetNode` if it exists. When the link has no linked resources it will be automatically removed from the state. Signed-off-by: Artem Chernyshev <[email protected]>
- Loading branch information
Showing
8 changed files
with
145 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
internal/backend/runtime/omni/controllers/omni/machine_cleanup.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2024 Sidero Labs, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
|
||
package omni | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/controller/generic" | ||
"github.com/cosi-project/runtime/pkg/controller/generic/cleanup" | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/xerrors" | ||
"go.uber.org/zap" | ||
|
||
"github.com/siderolabs/omni/client/pkg/omni/resources" | ||
"github.com/siderolabs/omni/client/pkg/omni/resources/omni" | ||
) | ||
|
||
// MachineCleanupController manages MachineCleanup resource lifecycle. | ||
type MachineCleanupController = cleanup.Controller[*omni.Machine] | ||
|
||
type sameIDHandler[I, O generic.ResourceWithRD] struct{} | ||
|
||
func (h *sameIDHandler[I, O]) FinalizerRemoval(ctx context.Context, r controller.Runtime, _ *zap.Logger, input I) error { | ||
var zeroOut O | ||
|
||
md := resource.NewMetadata( | ||
zeroOut.ResourceDefinition().DefaultNamespace, | ||
zeroOut.ResourceDefinition().Type, | ||
input.Metadata().ID(), | ||
resource.VersionUndefined, | ||
) | ||
|
||
res, err := r.Get(ctx, md) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
if res.Metadata().Owner() != "" { | ||
return nil | ||
} | ||
|
||
ready, err := r.Teardown(ctx, md) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !ready { | ||
return xerrors.NewTagged[cleanup.SkipReconcileTag](errors.New("waiting for resources to be destroyed")) | ||
} | ||
|
||
return r.Destroy(ctx, md, controller.WithOwner("")) | ||
} | ||
|
||
func (h *sameIDHandler[I, O]) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Type: omni.MachineSetNodeType, | ||
Namespace: resources.DefaultNamespace, | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
func (h *sameIDHandler[I, O]) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: omni.MachineSetNodeType, | ||
Kind: controller.OutputShared, | ||
}, | ||
} | ||
} | ||
|
||
// NewMachineCleanupController returns a new MachineCleanup controller. | ||
// This controller should remove all MachineSetNodes for a tearing down machine. | ||
// If the MachineSetNode is owned by some controller, it is skipped. | ||
func NewMachineCleanupController() *MachineCleanupController { | ||
return cleanup.NewController( | ||
cleanup.Settings[*omni.Machine]{ | ||
Name: "MachineCleanupController", | ||
Handler: &sameIDHandler[*omni.Machine, *omni.MachineSetNode]{}, | ||
}, | ||
) | ||
} |
Oops, something went wrong.