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 6, 2020
1 parent 7979129 commit 3d74c45
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 @@ -370,17 +370,20 @@ func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *machinev1.
return a.handleMachineError(machine, err, deleteEventAction)
}

instances, err := getRunningInstances(machine, client)
// Get all instances not terminated.
existingInstances, err := a.getMachineInstances(cluster, 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 @@ -142,6 +142,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, cluster *clusterv1.Cluster, machine *machinev1.Machine) {
actuator.DeleteMachine(cluster, 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, cluster *clusterv1.Cluster, machine *machinev1.Machine) {
actuator.DeleteMachine(cluster, machine)
},
event: "Normal Deleted Deleted machine aws-actuator-testing-machine",
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 3d74c45

Please sign in to comment.