Skip to content

Commit

Permalink
alibabacloud: use DescribeVSwitches to get vswitch tags
Browse files Browse the repository at this point in the history
Obtaining the vswitch tags through ListTagResources is incomplete,
which will cause the IP allocation to fail and require additional
queries. Therefore, use DescribeVSwitches to get the vswitch tags.

Signed-off-by: Hao Zhang <hao.zhang.am.i@gmail.com>
  • Loading branch information
haozhangami authored and sayboras committed Feb 28, 2023
1 parent 197b763 commit 4269350
Showing 1 changed file with 3 additions and 40 deletions.
43 changes: 3 additions & 40 deletions pkg/alibabacloud/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ import (
"github.com/cilium/cilium/pkg/api/helpers"
"github.com/cilium/cilium/pkg/cidr"
ipamTypes "github.com/cilium/cilium/pkg/ipam/types"
"github.com/cilium/cilium/pkg/math"
"github.com/cilium/cilium/pkg/spanstat"
)

const (
VPCID = "VPCID"

MaxListByTagSize = 20

// MaxResults is the number of entities on each page,
// it ranges from 1 to 50, the default value is 30.
MaxResults = 30
)

var maxAttachRetries = wait.Backoff{
Expand Down Expand Up @@ -94,7 +87,6 @@ func (c *Client) GetInstances(ctx context.Context, vpcs ipamTypes.VirtualNetwork
// GetVSwitches returns all ecs vSwitches as a subnetMap
func (c *Client) GetVSwitches(ctx context.Context) (ipamTypes.SubnetMap, error) {
var result ipamTypes.SubnetMap
vsws := []string{}
for i := 1; ; {
req := vpc.CreateDescribeVSwitchesRequest()
req.PageNumber = requests.NewInteger(i)
Expand Down Expand Up @@ -125,45 +117,16 @@ func (c *Client) GetVSwitches(ctx context.Context) (ipamTypes.SubnetMap, error)
AvailableAddresses: int(v.AvailableIpAddressCount),
Tags: map[string]string{},
}
vsws = append(vsws, v.VSwitchId)
for _, tag := range v.Tags.Tag {
result[v.VSwitchId].Tags[tag.Key] = tag.Value
}
}
if resp.TotalCount < resp.PageNumber*resp.PageSize {
break
}
i++
}

for i := 0; i <= (len(vsws)-1)/MaxListByTagSize; i++ {
var ids []string

tail := math.IntMin((i+1)*MaxListByTagSize, len(vsws))
ids = vsws[i*MaxListByTagSize : tail]

req := vpc.CreateListTagResourcesRequest()
req.ResourceType = "VSWITCH"
req.ResourceId = &ids
req.MaxResults = requests.NewInteger(MaxResults)
c.limiter.Limit(ctx, "ListTagResources")
for {
resp, err := c.vpcClient.ListTagResources(req)
if err != nil {
return nil, err
}
for _, tagRes := range resp.TagResources.TagResource {
subnet, ok := result[tagRes.ResourceId]
if !ok {
continue
}
subnet.Tags[tagRes.TagKey] = tagRes.TagValue
}
if resp.NextToken == "" {
break
} else {
req.NextToken = resp.NextToken
}
}
}

return result, nil
}

Expand Down

0 comments on commit 4269350

Please sign in to comment.