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

chore: Truncate instance types requirements list #212

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/cloudprovider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"errors"
"fmt"
"math"
"sort"

"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -67,6 +69,27 @@ type CloudProvider interface {
Name() string
}

type InstanceTypes []*InstanceType

func (its InstanceTypes) OrderByPrice(reqs scheduling.Requirements) InstanceTypes {
// Order instance types so that we get the cheapest instance types of the available offerings
sort.Slice(its, func(i, j int) bool {
iPrice := math.MaxFloat64
jPrice := math.MaxFloat64
if len(its[i].Offerings.Available().Requirements(reqs)) > 0 {
iPrice = its[i].Offerings.Available().Requirements(reqs).Cheapest().Price
}
if len(its[j].Offerings.Available().Requirements(reqs)) > 0 {
jPrice = its[j].Offerings.Available().Requirements(reqs).Cheapest().Price
}
if iPrice == jPrice {
return its[i].Name < its[j].Name
}
return iPrice < jPrice
})
return its
}

// InstanceType describes the properties of a potential node (either concrete attributes of an instance of this type
// or supported options in the case of arrays)
type InstanceType struct {
Expand Down
6 changes: 4 additions & 2 deletions pkg/controllers/provisioning/scheduling/machinetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// for fields like Requirements to be able to be stored more efficiently.
type MachineTemplate struct {
ProvisionerName string
InstanceTypeOptions []*cloudprovider.InstanceType
InstanceTypeOptions cloudprovider.InstanceTypes
Provider *v1alpha5.Provider
ProviderRef *v1alpha5.ProviderRef
Annotations map[string]string
Expand Down Expand Up @@ -75,7 +75,9 @@ func (i *MachineTemplate) ToNode() *v1.Node {
}

func (i *MachineTemplate) ToMachine(owner *v1alpha5.Provisioner) *v1alpha5.Machine {
i.Requirements.Add(scheduling.NewRequirement(v1.LabelInstanceTypeStable, v1.NodeSelectorOpIn, lo.Map(i.InstanceTypeOptions, func(i *cloudprovider.InstanceType, _ int) string {
// Order the instance types by price and only take the first 100 of them to decrease the instance type size in the requirements
instanceTypes := lo.Slice(i.InstanceTypeOptions.OrderByPrice(i.Requirements), 0, 100)
i.Requirements.Add(scheduling.NewRequirement(v1.LabelInstanceTypeStable, v1.NodeSelectorOpIn, lo.Map(instanceTypes, func(i *cloudprovider.InstanceType, _ int) string {
return i.Name
})...))
m := &v1alpha5.Machine{
Expand Down