Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alibabacloud: Support selecting subnet by IDs #23131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 36 additions & 5 deletions pkg/alibabacloud/eni/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,25 @@ func (m *InstancesManager) UpdateENI(instanceID string, eni *eniTypes.ENI) {
m.instances.Update(instanceID, eniRevision)
}

// FindOneVSwitch returns the vSwitch with the fewest available addresses, matching vpc, az and tags
func (m *InstancesManager) FindOneVSwitch(vpc, az string, toAllocate int, required ipamTypes.Tags) *ipamTypes.Subnet {
// FindOneVSwitch returns the vSwitch with the fewest available addresses, matching vpc and az.
// If we have explicit ID or tag constraints, chose a matching vSwitch. ID constraints take
// precedence.
func (m *InstancesManager) FindOneVSwitch(spec eniTypes.Spec, toAllocate int) *ipamTypes.Subnet {
if len(spec.VSwitches) > 0 {
return m.FindVSwitchByIDs(spec, toAllocate)
}
var bestSubnet *ipamTypes.Subnet
for _, vSwitch := range m.GetVSwitches() {
if vSwitch.VirtualNetworkID != vpc {
if vSwitch.VirtualNetworkID != spec.VPCID {
continue
}
if vSwitch.AvailabilityZone != az {
if vSwitch.AvailabilityZone != spec.AvailabilityZone {
continue
}
if vSwitch.AvailableAddresses < toAllocate {
continue
}
if !vSwitch.Tags.Match(required) {
if !vSwitch.Tags.Match(spec.VSwitchTags) {
continue
}
if bestSubnet == nil || bestSubnet.AvailableAddresses > vSwitch.AvailableAddresses {
Expand All @@ -184,6 +189,32 @@ func (m *InstancesManager) FindOneVSwitch(vpc, az string, toAllocate int, requir
return bestSubnet
}

// FindVSwitchByIDs returns the vSwitch within a provided list of vSwitch IDs with the fewest available addresses,
// matching vpc and az.
func (m *InstancesManager) FindVSwitchByIDs(spec eniTypes.Spec, toAllocate int) *ipamTypes.Subnet {
m.mutex.RLock()
defer m.mutex.RUnlock()

var bestSubnet *ipamTypes.Subnet
for _, vSwitch := range m.vSwitches {
if vSwitch.VirtualNetworkID != spec.VPCID || vSwitch.AvailabilityZone != spec.AvailabilityZone {
continue
}
if vSwitch.AvailableAddresses < toAllocate {
continue
}
for _, vSwitchID := range spec.VSwitches {
if vSwitch.ID != vSwitchID {
continue
}
if bestSubnet == nil || bestSubnet.AvailableAddresses > vSwitch.AvailableAddresses {
bestSubnet = vSwitch
}
}
}
return bestSubnet
}

// FindSecurityGroupByTags returns the security groups matching VPC ID and all required tags
// The returned security groups slice is immutable so it can be safely accessed
func (m *InstancesManager) FindSecurityGroupByTags(vpcID string, required ipamTypes.Tags) []*types.SecurityGroup {
Expand Down
3 changes: 1 addition & 2 deletions pkg/alibabacloud/eni/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func (n *Node) CreateInterface(ctx context.Context, allocation *ipam.AllocationA
return 0, "", nil
}

bestSubnet := n.manager.FindOneVSwitch(resource.Spec.AlibabaCloud.VPCID, resource.Spec.AlibabaCloud.AvailabilityZone,
toAllocate, resource.Spec.AlibabaCloud.VSwitchTags)
bestSubnet := n.manager.FindOneVSwitch(resource.Spec.AlibabaCloud, toAllocate)
if bestSubnet == nil {
return 0,
unableToFindSubnet,
Expand Down