Skip to content

Commit

Permalink
move lowest-n instance type options to generic packing pkg (#456)
Browse files Browse the repository at this point in the history
* move lowest-n instance type options to generic packing pkg

* make maxinstancetypes a public var so that cloud providers can adjust
  • Loading branch information
bwagner5 authored Jun 15, 2021
1 parent 35483f3 commit 8500d7d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
19 changes: 2 additions & 17 deletions pkg/cloudprovider/aws/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ import (
v1 "k8s.io/api/core/v1"
)

const (
// maxInstanceTypes defines the number of instance type options to pass to fleet
maxInstanceTypes = 20
)

type InstanceProvider struct {
ec2api ec2iface.EC2API
}
Expand All @@ -48,17 +43,7 @@ func (p *InstanceProvider) Create(ctx context.Context,
zonalSubnetOptions map[string][]*ec2.Subnet,
capacityType string,
) (*string, error) {
// 1. Trim the instanceTypeOptions so that the fleet request doesn't get too large
// If ~130 instance types are passed into fleet, the request can exceed the EC2 request size limit (145kb)
// due to the overrides expansion for subnetId (depends on number of AZs), Instance Type, and Priority.
// For spot capacity-optimized-prioritized, the request should be smaller to prevent using
// excessively large instance types that are more plentiful in capacity which the algorithm will bias towards.
// packing.InstanceTypes is sorted by vcpus and memory ascending so it's safe to trim the end of the list
// to remove excessively large instance types
if len(instanceTypeOptions) > maxInstanceTypes {
instanceTypeOptions = instanceTypeOptions[:maxInstanceTypes]
}
// 2. Construct override options.
// 1. Construct override options.
var overrides []*ec2.FleetLaunchTemplateOverridesRequest
for i, instanceType := range instanceTypeOptions {
for _, zone := range instanceType.Zones() {
Expand All @@ -80,7 +65,7 @@ func (p *InstanceProvider) Create(ctx context.Context,
overrides = append(overrides, override)
}
}
// 3. Create fleet
// 2. Create fleet
createFleetOutput, err := p.ec2api.CreateFleetWithContext(ctx, &ec2.CreateFleetInput{
Type: aws.String(ec2.FleetTypeInstant),
TargetCapacitySpecification: &ec2.TargetCapacitySpecificationRequest{
Expand Down
10 changes: 10 additions & 0 deletions pkg/packing/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
)

var (
// MaxInstanceTypes defines the number of instance type options to return to the cloud provider
MaxInstanceTypes = 20
)

// Constraints for an efficient binpacking solution of pods onto nodes, given
// overhead and node constraints.
type Constraints struct {
Expand Down Expand Up @@ -103,6 +108,11 @@ func (p *packer) packWithLargestPod(unpackedPods []*v1.Pod, constraints *Constra
}
}
sortByResources(bestInstances)
// Trim the bestInstances so that provisioning APIs in cloud providers are not overwhelmed by the number of instance type options
// For example, the AWS EC2 Fleet API only allows the request to be 145kb which equates to about 130 instance type options.
if len(bestInstances) > MaxInstanceTypes {
bestInstances = bestInstances[:MaxInstanceTypes]
}
return &cloudprovider.Packing{Pods: bestPackedPods, Constraints: constraints.Constraints, InstanceTypeOptions: bestInstances}, remainingPods
}

Expand Down

0 comments on commit 8500d7d

Please sign in to comment.