in pkg/provider/ip/eni/eni.go [197:253]
func (e *eniManager) DeleteIPV4Address(ipList []string, ec2APIHelper api.EC2APIHelper, log logr.Logger) ([]string, error) {
e.lock.Lock()
defer e.lock.Unlock()
var failedToUnAssign []string
var errors []error
log = log.WithValues("node name", e.instance.Name())
ipList = e.stripSubnetMaskFromIPSlice(ipList)
groupedIPs := e.groupIPsPerENI(ipList)
for eni, ips := range groupedIPs {
err := ec2APIHelper.UnassignPrivateIpAddresses(eni.eniID, ips)
if err != nil {
errors = append(errors, err)
log.Info("failed to deleted secondary IPv4 address", "eni", eni.eniID,
"IPv4 addresses", ips)
failedToUnAssign = append(failedToUnAssign, ips...)
continue
}
eni.remainingCapacity += len(ips)
for _, ip := range ips {
delete(e.ipToENIMap, ip)
}
log.Info("deleted secondary IPv4 address", "eni", eni.eniID, "IPv4 addresses", ips)
}
ipLimit := vpc.Limits[e.instance.Type()].IPv4PerInterface - 1
primaryENIID := e.instance.PrimaryNetworkInterfaceID()
// Clean up ENIs that just have the primary network interface attached to them
i := 0
for _, eni := range e.attachedENIs {
// ENI doesn't have any secondary IP attached to it and is not the primary network interface
if eni.remainingCapacity == ipLimit && primaryENIID != eni.eniID {
err := ec2APIHelper.DeleteNetworkInterface(&eni.eniID)
if err != nil {
errors = append(errors, err)
e.attachedENIs[i] = eni
i++
continue
}
log.Info("deleted ENI successfully as it has no secondary IP attached",
"id", eni.eniID)
} else {
e.attachedENIs[i] = eni
i++
}
}
e.attachedENIs = e.attachedENIs[:i]
if errors != nil && len(errors) > 0 {
return failedToUnAssign, fmt.Errorf("failed to unassign one or more ip addresses %v", errors)
}
return nil, nil
}