From 4334376930525cce67aed6f0b20ae0c6c56514aa Mon Sep 17 00:00:00 2001 From: Claes Mogren Date: Mon, 29 Apr 2019 18:13:49 -0700 Subject: [PATCH] Avoid using force detach of ENIs * Not using force * Added retries * Related to #359 --- pkg/awsutils/awsutils.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pkg/awsutils/awsutils.go b/pkg/awsutils/awsutils.go index 6a8e0e6137..77de0e9da7 100644 --- a/pkg/awsutils/awsutils.go +++ b/pkg/awsutils/awsutils.go @@ -731,22 +731,31 @@ func (cache *EC2InstanceMetadataCache) FreeENI(eniName string) error { log.Errorf("Failed to retrieve ENI %s attachment id: %v", eniName, err) return errors.Wrap(err, "FreeENI: failed to retrieve ENI's attachment id") } - log.Debugf("Found ENI %s attachment id: %s ", eniName, aws.StringValue(attachID)) - // Detach it first detachInput := &ec2.DetachNetworkInterfaceInput{ AttachmentId: attachID, - Force: aws.Bool(true), } - start := time.Now() - _, err = cache.ec2SVC.DetachNetworkInterface(detachInput) - awsAPILatency.WithLabelValues("DetachNetworkInterface", fmt.Sprint(err != nil)).Observe(msSince(start)) - if err != nil { - awsAPIErrInc("DetachNetworkInterface", err) - log.Errorf("Failed to detach ENI %s %v", eniName, err) - return errors.Wrap(err, "FreeENI: failed to detach ENI from instance") + // Retry detaching the ENI from the instance + var retry int + for retry = 0; retry <= maxENIDeleteRetries; retry++ { + start := time.Now() + _, err = cache.ec2SVC.DetachNetworkInterface(detachInput) + awsAPILatency.WithLabelValues("DetachNetworkInterface", fmt.Sprint(err != nil)).Observe(msSince(start)) + if err != nil { + awsAPIErrInc("DetachNetworkInterface", err) + log.Errorf("Failed to detach ENI %s %v", eniName, err) + if retry == maxENIDeleteRetries { + return errors.New("unable to detach ENI from EC2 instance, giving up") + } + } else { + log.Infof("Successfully detached ENI: %s", eniName) + break + } + + log.Debugf("Not able to detach ENI yet (attempt %d/%d): %v ", retry, maxENIDeleteRetries, err) + time.Sleep(retryDeleteENIInternal) } // It may take awhile for EC2-VPC to detach ENI from instance