Skip to content

Commit

Permalink
validate the eni status to inUse
Browse files Browse the repository at this point in the history
resent eni attach behave changed, it will fail and  rollback to Available , so check is necessary

Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Dec 12, 2024
1 parent a030031 commit 3679aed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/aliyun/client/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (a *OpenAPI) DescribeNetworkInterface(ctx context.Context, vpcID string, en
result = append(result, FromDescribeResp(&r))
}

l.V(4).Info("describe enis")
l.WithValues(LogFieldRequestID, resp.RequestId).Info("describe enis")

if len(resp.NetworkInterfaceSets.NetworkInterfaceSet) < maxSinglePageSize {
break
Expand Down
31 changes: 31 additions & 0 deletions pkg/factory/aliyun/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aliyun

import (
"context"
"fmt"
"net/netip"
"strings"
"time"
Expand Down Expand Up @@ -180,6 +181,8 @@ func (a *Aliyun) CreateNetworkInterface(ipv4, ipv6 int, eniType string) (*daemon
return r, nil, nil, err
}

timeout := time.After(2 * time.Second)

// 3. wait metadata ready & update cidr
err = validateIPInMetadata(ctx, v4Set, func() []netip.Addr {
exists, err := metadata.GetIPv4ByMac(r.MAC)
Expand Down Expand Up @@ -242,6 +245,34 @@ func (a *Aliyun) CreateNetworkInterface(ipv4, ipv6 int, eniType string) (*daemon
r.GatewayIP.SetIP(gw.String())
}

// safe to use in go 1.23
<-timeout

var innerErr error
// we check openAPI at last to ensure the eni is at InUse status
err = wait.ExponentialBackoffWithContext(a.ctx, backoff.Backoff(backoff.ENIOps), func(ctx context.Context) (done bool, err error) {
var eniSet []*client.NetworkInterface
eniSet, innerErr = a.openAPI.DescribeNetworkInterface(ctx, "", []string{r.ID}, "", "", "", nil)
if innerErr != nil {
return false, nil
}
if len(eniSet) != 1 {
innerErr = fmt.Errorf("can not found eni %s, resp %#v", r.ID, eniSet)
return false, nil
}
if eniSet[0].Status != client.ENIStatusInUse {
innerErr = fmt.Errorf("eni %s at %s", r.ID, eniSet[0].Status)
return false, nil
}
return true, nil
})
if err != nil {
if innerErr != nil {
err = innerErr
}
return r, v4Set, v6Set, err
}

return r, v4Set, v6Set, nil
}

Expand Down

0 comments on commit 3679aed

Please sign in to comment.