Skip to content

Commit

Permalink
add sort function instancetype for aws and openstack, modified existi…
Browse files Browse the repository at this point in the history
…ng in azure
  • Loading branch information
Nam Nguyen committed Sep 25, 2019
1 parent 927cda1 commit 4c05a31
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
27 changes: 27 additions & 0 deletions environs/instances/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package instances
import (
"fmt"
"sort"
"strings"

"github.com/juju/juju/core/constraints"
)
Expand Down Expand Up @@ -210,3 +211,29 @@ func (s byMemory) Less(i, j int) bool {
// Result is in descending order of cost so instance with lowest cost is used.
return inst0.Cost > inst1.Cost
}

//by Type is used to sort a slice by name by best effort. As we have different separators for different providers
//we do it by best effort using "-" and "."
//A possible sort could be:
//a1.2xlarge, a1.large, lexical sort would put a1.2xlarge before a1.large -- aws

type ByName []InstanceType

func (bt ByName) Len() int { return len(bt) }
func (bt ByName) Swap(i, j int) { bt[i], bt[j] = bt[j], bt[i] }
func (bt ByName) Less(i, j int) bool {
inst0, inst1 := &bt[i], &bt[j]
baseInst0 := strings.FieldsFunc(inst0.Name, splitDelimiters)
baseInst1 := strings.FieldsFunc(inst1.Name, splitDelimiters)
if baseInst0[0] != baseInst1[0] {
return baseInst0[0] < baseInst1[0]
}
// Name is equal, so use cost as a tie breaker.
// Result is in ascending order of cost so instance with lowest cost is first.
return inst0.Cost > inst1.Cost

}

func splitDelimiters(r rune) bool {
return r == ',' || r == '-' || r == '.'
}
17 changes: 13 additions & 4 deletions provider/azure/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,12 @@ func (env *azureEnviron) ConstraintsValidator(ctx context.ProviderCallContext) (
if err != nil {
return nil, err
}
instTypeNames := make([]string, 0, len(instanceTypes))
for instTypeName := range instanceTypes {
instTypeNames = append(instTypeNames, instTypeName)
instTypes := convertInstanceTypeMapToSlice(instanceTypes)
sort.Sort(instances.ByName(instTypes))
instTypeNames := make([]string, len(instTypes))
for i, itype := range instTypes {
instTypeNames[i] = itype.Name
}
sort.Strings(instTypeNames)

validator := constraints.NewValidator()
validator.RegisterUnsupported([]string{
Expand All @@ -402,6 +403,14 @@ func (env *azureEnviron) ConstraintsValidator(ctx context.ProviderCallContext) (
return validator, nil
}

func convertInstanceTypeMapToSlice(instanceTypes map[string]instances.InstanceType) []instances.InstanceType {
instTypes := make([]instances.InstanceType, 0, len(instanceTypes))
for _, instanceType := range instanceTypes {
instTypes = append(instTypes, instanceType)
}
return instTypes
}

// PrecheckInstance is defined on the environs.InstancePrechecker interface.
func (env *azureEnviron) PrecheckInstance(ctx context.ProviderCallContext, args environs.PrecheckInstanceParams) error {
if args.Placement != "" {
Expand Down
4 changes: 4 additions & 0 deletions provider/ec2/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math/rand"
"net"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -177,13 +178,16 @@ func (e *environ) ConstraintsValidator(ctx context.ProviderCallContext) (constra
[]string{constraints.Mem, constraints.Cores, constraints.CpuPower})
validator.RegisterUnsupported(unsupportedConstraints)
instanceTypes, err := e.supportedInstanceTypes(ctx)

if err != nil {
return nil, errors.Trace(err)
}
sort.Sort(instances.ByName(instanceTypes))
instTypeNames := make([]string, len(instanceTypes))
for i, itype := range instanceTypes {
instTypeNames[i] = itype.Name
}

validator.RegisterVocabulary(constraints.InstanceType, instTypeNames)
return validator, nil
}
Expand Down
2 changes: 2 additions & 0 deletions provider/openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"net/url"
"path"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -562,6 +563,7 @@ func (e *Environ) ConstraintsValidator(ctx context.ProviderCallContext) (constra
for i, flavor := range flavors {
instTypeNames[i] = flavor.Name
}
sort.Strings(instTypeNames)
validator.RegisterVocabulary(constraints.InstanceType, instTypeNames)
validator.RegisterVocabulary(constraints.VirtType, []string{"kvm", "lxd"})
validator.RegisterVocabulary(constraints.RootDiskSource, []string{rootDiskSourceVolume})
Expand Down

0 comments on commit 4c05a31

Please sign in to comment.