Skip to content

Commit

Permalink
feat: select the most available ip count subnet when launchInstance
Browse files Browse the repository at this point in the history
Signed-off-by: Vacant2333 <vacant2333@gmail.com>
  • Loading branch information
Vacant2333 committed Nov 1, 2024
1 parent 348df5e commit 22ed451
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/providers/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,30 @@ func (p *DefaultProvider) getOverrides(instanceTypes []*cloudprovider.InstanceTy
if reqs.Compatible(offering.Requirements, scheduling.AllowUndefinedWellKnownLabels) != nil {
continue
}
subnet, ok := zonalSubnets[offering.Requirements.Get(corev1.LabelTopologyZone).Any()]
if !ok {

// Select the best subnet with the highest available IP count within the required zones.
var bestSubnet *subnet.Subnet
for _, zone := range offering.Requirements.Get(corev1.LabelTopologyZone).Values() {
if subnet, ok := zonalSubnets[zone]; !ok {
continue
} else if bestSubnet == nil || bestSubnet.AvailableIPAddressCount < subnet.AvailableIPAddressCount {
bestSubnet = subnet
}
}
if bestSubnet == nil {
continue
}

overrides = append(overrides, &ec2.FleetLaunchTemplateOverridesRequest{
InstanceType: aws.String(offering.parentInstanceTypeName),
SubnetId: lo.ToPtr(subnet.ID),
SubnetId: lo.ToPtr(bestSubnet.ID),
ImageId: aws.String(image),
// Set Priority based on the available IP address count of the best subnet.
// This prioritization helps AWS choose subnets with higher IP availability first.
Priority: aws.Float64(float64(bestSubnet.AvailableIPAddressCount)),
// This is technically redundant, but is useful if we have to parse insufficient capacity errors from
// CreateFleet so that we can figure out the zone rather than additional API calls to look up the subnet
AvailabilityZone: lo.ToPtr(subnet.Zone),
AvailabilityZone: lo.ToPtr(bestSubnet.Zone),
})
}
return overrides
Expand Down
7 changes: 7 additions & 0 deletions pkg/providers/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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
Expand Down

0 comments on commit 22ed451

Please sign in to comment.