|
| 1 | +package selector |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws" |
| 8 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // AggregateLowPercentile is the default lower percentile for resource ranges on similar instance type comparisons |
| 13 | + AggregateLowPercentile = 0.8 |
| 14 | + // AggregateHighPercentile is the default upper percentile for resource ranges on similar instance type comparisons |
| 15 | + AggregateHighPercentile = 1.2 |
| 16 | +) |
| 17 | + |
| 18 | +// FiltersTransform can be implemented to provide custom transforms |
| 19 | +type FiltersTransform interface { |
| 20 | + Transform(Filters) (Filters, error) |
| 21 | +} |
| 22 | + |
| 23 | +// TransformFn is the func type definition for a FiltersTransform |
| 24 | +type TransformFn func(Filters) (Filters, error) |
| 25 | + |
| 26 | +// Transform implements FiltersTransform interface on TransformFn |
| 27 | +// This allows any TransformFn to be passed into funcs accepting FiltersTransform interface |
| 28 | +func (fn TransformFn) Transform(filters Filters) (Filters, error) { |
| 29 | + return fn(filters) |
| 30 | +} |
| 31 | + |
| 32 | +// TransformBaseInstanceType transforms lower level filters based on the instanceTypeBase specs |
| 33 | +func (itf Selector) TransformBaseInstanceType(filters Filters) (Filters, error) { |
| 34 | + if filters.InstanceTypeBase == nil { |
| 35 | + return filters, nil |
| 36 | + } |
| 37 | + instanceTypesOutput, err := itf.EC2.DescribeInstanceTypes(&ec2.DescribeInstanceTypesInput{ |
| 38 | + InstanceTypes: []*string{filters.InstanceTypeBase}, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + return filters, err |
| 42 | + } |
| 43 | + if len(instanceTypesOutput.InstanceTypes) == 0 { |
| 44 | + return filters, fmt.Errorf("error instance type %s is not a valid instance type", *filters.InstanceTypeBase) |
| 45 | + } |
| 46 | + instanceTypeInfo := instanceTypesOutput.InstanceTypes[0] |
| 47 | + if filters.BareMetal == nil { |
| 48 | + filters.BareMetal = instanceTypeInfo.BareMetal |
| 49 | + } |
| 50 | + if filters.CPUArchitecture == nil { |
| 51 | + filters.CPUArchitecture = instanceTypeInfo.ProcessorInfo.SupportedArchitectures[0] |
| 52 | + } |
| 53 | + if filters.Fpga == nil { |
| 54 | + isFpgaSupported := instanceTypeInfo.FpgaInfo != nil |
| 55 | + filters.Fpga = &isFpgaSupported |
| 56 | + } |
| 57 | + if filters.GpusRange == nil { |
| 58 | + if instanceTypeInfo.GpuInfo != nil { |
| 59 | + gpuCount := int(*getTotalGpusCount(instanceTypeInfo.GpuInfo)) |
| 60 | + filters.GpusRange = &IntRangeFilter{LowerBound: gpuCount, UpperBound: gpuCount} |
| 61 | + } |
| 62 | + } |
| 63 | + if filters.MemoryRange == nil { |
| 64 | + lowerBound := int(float64(*instanceTypeInfo.MemoryInfo.SizeInMiB) * AggregateLowPercentile) |
| 65 | + upperBound := int(float64(*instanceTypeInfo.MemoryInfo.SizeInMiB) * AggregateHighPercentile) |
| 66 | + filters.MemoryRange = &IntRangeFilter{LowerBound: lowerBound, UpperBound: upperBound} |
| 67 | + } |
| 68 | + if filters.VCpusRange == nil { |
| 69 | + lowerBound := int(float64(*instanceTypeInfo.VCpuInfo.DefaultVCpus) * AggregateLowPercentile) |
| 70 | + upperBound := int(float64(*instanceTypeInfo.VCpuInfo.DefaultVCpus) * AggregateHighPercentile) |
| 71 | + filters.VCpusRange = &IntRangeFilter{LowerBound: lowerBound, UpperBound: upperBound} |
| 72 | + } |
| 73 | + filters.InstanceTypeBase = nil |
| 74 | + |
| 75 | + return filters, nil |
| 76 | +} |
| 77 | + |
| 78 | +// TransformFlexible transforms lower level filters based on a set of opinions |
| 79 | +func (itf Selector) TransformFlexible(filters Filters) (Filters, error) { |
| 80 | + if filters.Flexible == nil { |
| 81 | + return filters, nil |
| 82 | + } |
| 83 | + if filters.CPUArchitecture == nil { |
| 84 | + filters.CPUArchitecture = aws.String("x86_64") |
| 85 | + } |
| 86 | + if filters.BareMetal == nil { |
| 87 | + filters.BareMetal = aws.Bool(false) |
| 88 | + } |
| 89 | + if filters.Fpga == nil { |
| 90 | + filters.Fpga = aws.Bool(false) |
| 91 | + } |
| 92 | + |
| 93 | + if filters.AllowList == nil { |
| 94 | + baseAllowedInstanceTypes, err := regexp.Compile("^[cmr][3-9][ag]?\\..*$|^a[1-9]\\..*$|^t[2-9]\\..*$") |
| 95 | + if err != nil { |
| 96 | + return filters, err |
| 97 | + } |
| 98 | + filters.AllowList = baseAllowedInstanceTypes |
| 99 | + } |
| 100 | + |
| 101 | + if filters.VCpusRange == nil && filters.MemoryRange == nil { |
| 102 | + defaultVcpus := 4 |
| 103 | + filters.VCpusRange = &IntRangeFilter{LowerBound: defaultVcpus, UpperBound: defaultVcpus} |
| 104 | + } |
| 105 | + |
| 106 | + return filters, nil |
| 107 | +} |
0 commit comments