Skip to content

Commit

Permalink
Fix deletion to account for any instance state
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed Feb 5, 2020
1 parent 0df2d10 commit 6398c56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,20 @@ func (a *Actuator) DeleteMachine(machine *machinev1.Machine) error {
return a.handleMachineError(machine, err, deleteEventAction)
}

instances, err := getRunningInstances(machine, client)
// Get all instances not terminated.
existingInstances, err := a.getMachineInstances(machine)
if err != nil {
glog.Errorf("%s: error getting running instances: %v", machine.Name, err)
glog.Errorf("%s: error getting existing instances: %v", machine.Name, err)
return err
}
if len(instances) == 0 {
existingLen := len(existingInstances)
glog.Infof("%s: found %d existing instances for machine", machine.Name, existingLen)
if existingLen == 0 {
glog.Warningf("%s: no instances found to delete for machine", machine.Name)
return nil
}

terminatingInstances, err := terminateInstances(client, instances)
terminatingInstances, err := terminateInstances(client, existingInstances)
if err != nil {
return a.handleMachineError(machine, mapierrors.DeleteMachine(err.Error()), noEventAction)
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ func TestMachineEvents(t *testing.T) {
},
event: "Normal Deleted Deleted machine aws-actuator-testing-machine",
},
{
name: "Delete machine event succeed with pending instances",
machine: machine,
describeInstancesOutput: stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c", ec2.InstanceStateNamePending),
operation: func(actuator *Actuator, machine *machinev1.Machine) {
actuator.DeleteMachine(machine)
},
event: "Normal Deleted Deleted machine aws-actuator-testing-machine",
},
{
name: "Delete machine event succeed with stopped instances",
machine: machine,
describeInstancesOutput: stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c", ec2.InstanceStateNameStopped),
operation: func(actuator *Actuator, machine *machinev1.Machine) {
actuator.DeleteMachine(machine)
},
event: "Normal Deleted Deleted machine aws-actuator-testing-machine",
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 6398c56

Please sign in to comment.