From 66d33239b1ab29f97b1a6dfb0583e92b7683ccd3 Mon Sep 17 00:00:00 2001 From: Vacant2333 Date: Fri, 1 Nov 2024 16:59:33 +0800 Subject: [PATCH] fix: avoid selecting subnets with insufficient available IP addresses Signed-off-by: Vacant2333 --- pkg/providers/subnet/subnet.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/providers/subnet/subnet.go b/pkg/providers/subnet/subnet.go index 959b8bc4ddc8..98d19e479705 100644 --- a/pkg/providers/subnet/subnet.go +++ b/pkg/providers/subnet/subnet.go @@ -125,6 +125,7 @@ func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass) } // ZonalSubnetsForLaunch returns a mapping of zone to the subnet with the most available IP addresses and deducts the passed ips from the available count +// nolint:gocyclo func (p *DefaultProvider) ZonalSubnetsForLaunch(ctx context.Context, nodeClass *v1.EC2NodeClass, instanceTypes []*cloudprovider.InstanceType, capacityType string) (map[string]*Subnet, error) { if len(nodeClass.Status.Subnets) == 0 { return nil, fmt.Errorf("no subnets matched selector %v", nodeClass.Spec.SubnetSelectorTerms) @@ -168,6 +169,13 @@ func (p *DefaultProvider) ZonalSubnetsForLaunch(ctx context.Context, nodeClass * if trackedIPs, ok := p.inflightIPs[subnet.ID]; ok { prevIPs = trackedIPs } + + // Check if the remaining IP count is insufficient to meet the predicted IP usage; + // if so, remove this subnet zone record from inflightIPs and continue to the next item in the loop。 + if prevIPs-predictedIPsUsed <= 0 { + delete(zonalSubnets, subnet.Zone) + continue + } p.inflightIPs[subnet.ID] = prevIPs - predictedIPsUsed } return zonalSubnets, nil