Skip to content

Commit 8600a7b

Browse files
authored
Merge pull request #516 from prashanth26/bugfix/continue-deletion-on-error2
AWS: Allow deletion of VMs even on list image or modify instance failure
2 parents 54db989 + 480f1b5 commit 8600a7b

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

pkg/driver/driver_aws.go

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -286,44 +286,30 @@ func (d *AWSDriver) Delete(machineID string) error {
286286
metrics.APIRequestCount.With(prometheus.Labels{"provider": "aws", "service": "ecs"}).Inc()
287287

288288
if len(describeImageOutput.Images) < 1 {
289-
klog.Errorf("Image %s not found", *imageID)
290-
return fmt.Errorf("Image %s not found", *imageID)
291-
}
292-
293-
// returns instanceBlockDevices whose DeleteOnTermination field is nil on machineAPIs
294-
instanceBlkDeviceMappings, err := d.checkBlockDevices(instanceID, describeImageOutput.Images[0].RootDeviceName)
295-
if err != nil {
296-
klog.Errorf("Could not Default deletionOnTermination while terminating machine: %s", err.Error())
297-
return err
298-
}
299-
300-
// Default deletionOnTermination to true when unset on API field
301-
if len(instanceBlkDeviceMappings) > 0 {
302-
input := &ec2.ModifyInstanceAttributeInput{
303-
InstanceId: aws.String(instanceID),
304-
BlockDeviceMappings: instanceBlkDeviceMappings,
305-
}
306-
_, err = svc.ModifyInstanceAttribute(input)
289+
// Disk image not found at provider
290+
klog.Warningf("Disk image %s not found at provider for machineID %q", *imageID, machineID)
291+
} else {
292+
// returns instanceBlockDevices whose DeleteOnTermination field is nil on machineAPIs
293+
instanceBlkDeviceMappings, err := d.checkBlockDevices(instanceID, describeImageOutput.Images[0].RootDeviceName)
307294
if err != nil {
308-
if aerr, ok := err.(awserr.Error); ok {
309-
switch aerr.Code() {
310-
case "InvalidInstanceAttributeValue":
311-
// Case when disk is not yet attached to the VM
312-
klog.Warning(aerr.Error())
313-
break
314-
default:
315-
klog.Error(aerr.Error())
316-
metrics.APIFailedRequestCount.With(prometheus.Labels{"provider": "aws", "service": "ecs"}).Inc()
317-
return err
318-
}
319-
} else {
320-
klog.Error(err.Error())
295+
klog.Errorf("Could not Default deletionOnTermination while terminating machine: %s", err.Error())
296+
}
297+
298+
// Default deletionOnTermination to true when unset on API field
299+
if err == nil && len(instanceBlkDeviceMappings) > 0 {
300+
input := &ec2.ModifyInstanceAttributeInput{
301+
InstanceId: aws.String(instanceID),
302+
BlockDeviceMappings: instanceBlkDeviceMappings,
303+
}
304+
_, err = svc.ModifyInstanceAttribute(input)
305+
if err != nil {
321306
metrics.APIFailedRequestCount.With(prometheus.Labels{"provider": "aws", "service": "ecs"}).Inc()
322-
return err
307+
klog.Warningf("Couldn't complete modify instance with machineID %q. Error: %s. Continuing machine deletion", machineID, err.Error())
308+
} else {
309+
metrics.APIRequestCount.With(prometheus.Labels{"provider": "aws", "service": "ecs"}).Inc()
310+
klog.V(2).Infof("Successfully defaulted deletionOnTermination to true for disks (with nil pointer) for machineID: %q", machineID)
323311
}
324312
}
325-
metrics.APIRequestCount.With(prometheus.Labels{"provider": "aws", "service": "ecs"}).Inc()
326-
klog.V(2).Infof("Successfully defaulted deletionOnTermination to true for disks (with nil pointer) for instanceID: %q", instanceID)
327313
}
328314

329315
// Terminate instance call

0 commit comments

Comments
 (0)