From 945f3031bb457fb05ac396955690de7c7daa42f9 Mon Sep 17 00:00:00 2001 From: Yann Ramin Date: Tue, 2 Jan 2018 22:18:37 -0800 Subject: [PATCH] Do not allocate a second IP on freshly minted interfaces (#17) Previously, we would create a second IP immediately instead of just using the previous IP address. This patch will use the initial IP address from a newly made interface if it is the only IP known to the system. --- plugin/ipam/main.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugin/ipam/main.go b/plugin/ipam/main.go index 71ea00c..c112267 100644 --- a/plugin/ipam/main.go +++ b/plugin/ipam/main.go @@ -95,14 +95,17 @@ func cmdAdd(args *skel.CmdArgs) error { if err != nil { // failed, so attempt to add an IP to a new interface newIf, err := aws.NewInterface(conf.IPAM.SecGroupIds, conf.IPAM.SubnetTags) - if err != nil { + // If this interface has somehow gained more than one IP since being allocated, + // abort this process and let a subsequent run find a valid IP. + if err != nil || len(newIf.IPv4s) != 1 { return fmt.Errorf("unable to create a new elastic network interface due to %v", err) } - alloc, err = aws.AllocateIPOn(*newIf) - if err != nil { - return fmt.Errorf("unable to allocate an IP due to %v", - err) + // Freshly allocated interfaces will always have one valid IP - use + // this IP address. + alloc = &aws.AllocationResult{ + &newIf.IPv4s[0], + *newIf, } } }