diff --git a/pkg/apis/machine/v1beta1/machine_types.go b/pkg/apis/machine/v1beta1/machine_types.go index 0bbc48f5a768..279d8fca26db 100644 --- a/pkg/apis/machine/v1beta1/machine_types.go +++ b/pkg/apis/machine/v1beta1/machine_types.go @@ -37,6 +37,10 @@ const ( // MachineClusterIDLabel is the label that a machine must have to identify the // cluster to which it belongs. MachineClusterIDLabel = "machine.openshift.io/cluster-api-cluster" + + // PreserveInstanceAnnotation prevents a VM from being deleted by the + // machine-controller and will cause machine-controller to requeue. + PreserveInstanceAnnotation = "machine.openshift.io/preserve-instance" ) // +genclient diff --git a/pkg/controller/machine/controller.go b/pkg/controller/machine/controller.go index f5aa0e182ee2..34530c0565d8 100644 --- a/pkg/controller/machine/controller.go +++ b/pkg/controller/machine/controller.go @@ -335,24 +335,8 @@ func (r *ReconcileMachine) getCluster(ctx context.Context, machine *machinev1.Ma } func (r *ReconcileMachine) isDeleteAllowed(machine *machinev1.Machine) bool { - if r.nodeName == "" || machine.Status.NodeRef == nil { - return true - } - - if machine.Status.NodeRef.Name != r.nodeName { - return true - } - - node := &corev1.Node{} - if err := r.Client.Get(context.Background(), client.ObjectKey{Name: r.nodeName}, node); err != nil { - klog.Infof("Failed to determine if controller's node %q is associated with machine %q: %v", r.nodeName, machine.Name, err) - return true - } - - // When the UID of the machine's node reference and this controller's actual node match then then the request is to - // delete the machine this machine-controller is running on. Return false to not allow machine controller to delete its - // own machine. - return node.UID != machine.Status.NodeRef.UID + _, exists := machine.ObjectMeta.Annotations[machinev1.PreserveInstanceAnnotation] + return !exists } func (r *ReconcileMachine) deleteNode(ctx context.Context, name string) error {