Skip to content

Commit

Permalink
Setting machine instance state to unknown on machine failure
Browse files Browse the repository at this point in the history
Bug 1808971: Machine status shows "running" when an instance was terminated
  • Loading branch information
Danil-Grigorev committed May 5, 2020
1 parent 078f137 commit 1de9a9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ const (
// Machine has a deletion timestamp
phaseDeleting = "Deleting"

// Hardcoded instance state set on machine failure
unknownInstanceState = "Unknown"

skipWaitForDeleteTimeoutSeconds = 60 * 5
)

Expand Down Expand Up @@ -426,9 +429,17 @@ func (r *ReconcileMachine) setPhase(machine *machinev1.Machine, phase string, er
machine.Status.LastUpdated = &now
if phase == phaseFailed && errorMessage != "" {
machine.Status.ErrorMessage = &errorMessage
if machine.Annotations == nil {
machine.Annotations = map[string]string{}
}
machine.Annotations[MachineInstanceStateAnnotationName] = unknownInstanceState
if err := r.Client.Patch(context.Background(), machine, baseToPatch); err != nil {
klog.Errorf("Failed to update machine %q: %v", machine.GetName(), err)
return err
}
}
if err := r.Client.Status().Patch(context.Background(), machine, baseToPatch); err != nil {
klog.Errorf("Failed to update machine %q: %v", machine.GetName(), err)
klog.Errorf("Failed to update machine status %q: %v", machine.GetName(), err)
return err
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/machine/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ func TestSetPhase(t *testing.T) {

// Set phaseFailed with an errorMessage should store the message
expecterErrorMessage := "test"
// Set phaseFailed will result to force Unknown state for VM state display
expectedInstanceState := "Unknown"
if err := reconciler.setPhase(machine, phaseFailed, expecterErrorMessage); err != nil {
t.Fatal(err)
}
Expand All @@ -407,6 +409,15 @@ func TestSetPhase(t *testing.T) {
if expecterErrorMessage != *machine.Status.ErrorMessage {
t.Errorf("Expected: %v, got: %v", expecterErrorMessage, *machine.Status.ErrorMessage)
}
if got.Status.Phase == nil {
t.Fatal("Got phase nil")
}
if *got.Status.Phase != phaseFailed {
t.Errorf("Got: %v, expected: %v", *got.Status.Phase, phaseRunning)
}
if got.Annotations[MachineInstanceStateAnnotationName] != expectedInstanceState {
t.Errorf("Expected VM to go into: %v state, got: %v", expectedInstanceState, got.Annotations[MachineInstanceStateAnnotationName])
}
}

func TestMachineIsProvisioned(t *testing.T) {
Expand Down

0 comments on commit 1de9a9b

Please sign in to comment.