diff --git a/pkg/actuators/machine/actuator.go b/pkg/actuators/machine/actuator.go index 1dab50fc95..c3c1aa5deb 100644 --- a/pkg/actuators/machine/actuator.go +++ b/pkg/actuators/machine/actuator.go @@ -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) } diff --git a/pkg/actuators/machine/actuator_test.go b/pkg/actuators/machine/actuator_test.go index 6f8b0d45fa..1f268e2bcf 100644 --- a/pkg/actuators/machine/actuator_test.go +++ b/pkg/actuators/machine/actuator_test.go @@ -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 {