From ec0d2c20a7c2c3535d4b6bd57a50bf5e16bb6071 Mon Sep 17 00:00:00 2001 From: Jacob Weinstock Date: Mon, 25 Nov 2024 11:08:42 -0700 Subject: [PATCH] Allow tinkerbellmachine objects to be removed: When a Hardware object corresponding to a tinkerbellmachine object no longer exists, allow the tinkerbellmachine object to be removed and clean up associated template and workflow. At the moment this will skip any BMC operations to power the machine off and modify the Hardware object. Since the Hardware object doesn't exist this seems reasonable. Signed-off-by: Jacob Weinstock --- controller/machine/scope.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/controller/machine/scope.go b/controller/machine/scope.go index 0991176..54d47f1 100644 --- a/controller/machine/scope.go +++ b/controller/machine/scope.go @@ -199,10 +199,30 @@ func (scope *machineReconcileScope) DeleteMachineWithDependencies() error { scope.log.Info("Removing machine", "hardwareName", scope.tinkerbellMachine.Spec.HardwareName) // Fetch hw for the machine. hw := &tinkv1.Hardware{} - if err := scope.getHardwareForMachine(hw); err != nil { + + err := scope.getHardwareForMachine(hw) + if err != nil && !apierrors.IsNotFound(err) { return err } + // If the Hardware is not found, we can't do any BMC operations. In this case we just remove all + // the other dependencies and remove the finalize from the TinkerbellMachine object. + if apierrors.IsNotFound(err) { + scope.log.Info("Hardware not found, only template, workflow and finalizer will be removed", + "hardwareName", scope.tinkerbellMachine.Spec.HardwareName, + ) + + if err := scope.removeTemplate(); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("removing Template: %w", err) + } + + if err := scope.removeWorkflow(); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("removing Workflow: %w", err) + } + + return scope.removeFinalizer() + } + if err := scope.removeDependencies(hw); err != nil { return err }