Skip to content

Commit

Permalink
Extend fix to OOT provider
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanth26 committed Jun 28, 2020
1 parent 3484d7c commit 66fee5c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/util/provider/machinecontroller/controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import (
"k8s.io/utils/pointer"
)

func TestMachineControllerManagerSuite(t *testing.T) {
func TestMachineControllerSuite(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Machine Controller Manager Suite")
RunSpecs(t, "Machine Controller Suite")
}

var (
Expand Down
102 changes: 102 additions & 0 deletions pkg/util/provider/machinecontroller/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,108 @@ var _ = Describe("machine", func() {
),
},
}),
Entry("Drain skipping as nodeName is not valid", &data{
setup: setup{
secrets: []*corev1.Secret{
{
ObjectMeta: *newObjectMeta(objMeta, 0),
},
},
machineClasses: []*v1alpha1.MachineClass{
{
ObjectMeta: *newObjectMeta(objMeta, 0),
SecretRef: newSecretReference(objMeta, 0),
},
},
machines: newMachines(
1,
&v1alpha1.MachineTemplateSpec{
ObjectMeta: *newObjectMeta(objMeta, 0),
Spec: v1alpha1.MachineSpec{
Class: v1alpha1.ClassSpec{
Kind: "MachineClass",
Name: "machine-0",
},
ProviderID: "fakeID",
},
},
&v1alpha1.MachineStatus{
Node: "fakeNode",
CurrentStatus: v1alpha1.CurrentStatus{
Phase: v1alpha1.MachineTerminating,
LastUpdateTime: metav1.Now(),
},
LastOperation: v1alpha1.LastOperation{
Description: machineutils.InitiateDrain,
State: v1alpha1.MachineStateProcessing,
Type: v1alpha1.MachineOperationDelete,
LastUpdateTime: metav1.Now(),
},
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionUnknown,
LastTransitionTime: metav1.NewTime(time.Now().Add(-6 * time.Minute)),
},
},
},
nil,
map[string]string{
machineutils.MachinePriority: "3",
},
map[string]string{
"node": "",
},
true,
),
},
action: action{
machine: "machine-0",
fakeDriver: &driver.FakeDriver{
VMExists: true,
ProviderID: "fakeID-0",
NodeName: "",
Err: nil,
},
},
expect: expect{
err: fmt.Errorf("Skipping drain as nodeName is not a valid one for machine. Initiate VM deletion"),
retry: machineutils.RetryOp,
machine: newMachine(
&v1alpha1.MachineTemplateSpec{
ObjectMeta: *newObjectMeta(objMeta, 0),
Spec: v1alpha1.MachineSpec{
Class: v1alpha1.ClassSpec{
Kind: "MachineClass",
Name: "machine-0",
},
ProviderID: "fakeID",
},
},
&v1alpha1.MachineStatus{
Node: "fakeNode",
CurrentStatus: v1alpha1.CurrentStatus{
Phase: v1alpha1.MachineTerminating,
LastUpdateTime: metav1.Now(),
},
LastOperation: v1alpha1.LastOperation{
Description: fmt.Sprintf("Skipping drain as nodeName is not a valid one for machine. Initiate VM deletion"),
State: v1alpha1.MachineStateProcessing,
Type: v1alpha1.MachineOperationDelete,
LastUpdateTime: metav1.Now(),
},
},
nil,
map[string]string{
machineutils.MachinePriority: "3",
},
map[string]string{
"node": "",
},
true,
),
},
}),
Entry("Drain skipping as machine is NotReady for a long time (5 minutes)", &data{
setup: setup{
secrets: []*corev1.Secret{
Expand Down
33 changes: 25 additions & 8 deletions pkg/util/provider/machinecontroller/machine_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,16 @@ func (c *controller) getVMStatus(getMachineStatusRequest *driver.GetMachineStatu
return retry, err
}

// isValidNodeName checks if the nodeName is valid
func isValidNodeName(nodeName string) bool {
if nodeName == "" {
// if nodeName is empty
return false
}

return true
}

// drainNode attempts to drain the node backed by the machine object
func (c *controller) drainNode(deleteMachineRequest *driver.DeleteMachineRequest) (machineutils.Retry, error) {
var (
Expand All @@ -826,20 +836,27 @@ func (c *controller) drainNode(deleteMachineRequest *driver.DeleteMachineRequest
nodeNotReadyDuration = 5 * time.Minute
)

for _, condition := range machine.Status.Conditions {
if condition.Type == v1.NodeReady && condition.Status != corev1.ConditionTrue && (time.Since(condition.LastTransitionTime.Time) > nodeNotReadyDuration) {
klog.Warningf("Skipping drain for NotReady machine %q", machine.Name)
err = fmt.Errorf("Skipping drain as machine is NotReady for over 5minutes. %s", machineutils.InitiateVMDeletion)
skipDrain = true
if !isValidNodeName(nodeName) {
klog.Warningf("Skipping drain as nodeName is not a valid one for machine %q", machine.Name)
err = fmt.Errorf("Skipping drain as nodeName is not a valid one for machine. %s", machineutils.InitiateVMDeletion)
description = fmt.Sprintf("Skipping drain as nodeName is not a valid one for machine. %s", machineutils.InitiateVMDeletion)
skipDrain = true
} else {
for _, condition := range machine.Status.Conditions {
if condition.Type == v1.NodeReady && condition.Status != corev1.ConditionTrue && (time.Since(condition.LastTransitionTime.Time) > nodeNotReadyDuration) {
klog.Warningf("Skipping drain for NotReady machine %q", machine.Name)
err = fmt.Errorf("Skipping drain as machine is NotReady for over 5minutes. %s", machineutils.InitiateVMDeletion)
description = fmt.Sprintf("Skipping drain as machine is NotReady for over 5minutes. %s", machineutils.InitiateVMDeletion)
skipDrain = true

break
}
}
}

if skipDrain {
// If not is not ready for over 5 minutes, skip draining this machine
description = fmt.Sprintf("Skipping drain as machine is NotReady for over 5minutes. %s", machineutils.InitiateVMDeletion)
state = v1alpha1.MachineStateProcessing
phase = v1alpha1.MachineTerminating

} else {
// Timeout value obtained by subtracting last operation with expected time out period
timeOut := metav1.Now().Add(-timeOutDuration).Sub(machine.Status.CurrentStatus.LastUpdateTime.Time)
Expand Down

0 comments on commit 66fee5c

Please sign in to comment.