-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement the --flexible suite filter (#20)
* implement the --flexible suite filter * fix readme usage
- Loading branch information
Showing
7 changed files
with
183 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package selector | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
) | ||
|
||
const ( | ||
// AggregateLowPercentile is the default lower percentile for resource ranges on similar instance type comparisons | ||
AggregateLowPercentile = 0.8 | ||
// AggregateHighPercentile is the default upper percentile for resource ranges on similar instance type comparisons | ||
AggregateHighPercentile = 1.2 | ||
) | ||
|
||
// FiltersTransform can be implemented to provide custom transforms | ||
type FiltersTransform interface { | ||
Transform(Filters) (Filters, error) | ||
} | ||
|
||
// TransformFn is the func type definition for a FiltersTransform | ||
type TransformFn func(Filters) (Filters, error) | ||
|
||
// Transform implements FiltersTransform interface on TransformFn | ||
// This allows any TransformFn to be passed into funcs accepting FiltersTransform interface | ||
func (fn TransformFn) Transform(filters Filters) (Filters, error) { | ||
return fn(filters) | ||
} | ||
|
||
// TransformBaseInstanceType transforms lower level filters based on the instanceTypeBase specs | ||
func (itf Selector) TransformBaseInstanceType(filters Filters) (Filters, error) { | ||
if filters.InstanceTypeBase == nil { | ||
return filters, nil | ||
} | ||
instanceTypesOutput, err := itf.EC2.DescribeInstanceTypes(&ec2.DescribeInstanceTypesInput{ | ||
InstanceTypes: []*string{filters.InstanceTypeBase}, | ||
}) | ||
if err != nil { | ||
return filters, err | ||
} | ||
if len(instanceTypesOutput.InstanceTypes) == 0 { | ||
return filters, fmt.Errorf("error instance type %s is not a valid instance type", *filters.InstanceTypeBase) | ||
} | ||
instanceTypeInfo := instanceTypesOutput.InstanceTypes[0] | ||
if filters.BareMetal == nil { | ||
filters.BareMetal = instanceTypeInfo.BareMetal | ||
} | ||
if filters.CPUArchitecture == nil { | ||
filters.CPUArchitecture = instanceTypeInfo.ProcessorInfo.SupportedArchitectures[0] | ||
} | ||
if filters.Fpga == nil { | ||
isFpgaSupported := instanceTypeInfo.FpgaInfo != nil | ||
filters.Fpga = &isFpgaSupported | ||
} | ||
if filters.GpusRange == nil { | ||
if instanceTypeInfo.GpuInfo != nil { | ||
gpuCount := int(*getTotalGpusCount(instanceTypeInfo.GpuInfo)) | ||
filters.GpusRange = &IntRangeFilter{LowerBound: gpuCount, UpperBound: gpuCount} | ||
} | ||
} | ||
if filters.MemoryRange == nil { | ||
lowerBound := int(float64(*instanceTypeInfo.MemoryInfo.SizeInMiB) * AggregateLowPercentile) | ||
upperBound := int(float64(*instanceTypeInfo.MemoryInfo.SizeInMiB) * AggregateHighPercentile) | ||
filters.MemoryRange = &IntRangeFilter{LowerBound: lowerBound, UpperBound: upperBound} | ||
} | ||
if filters.VCpusRange == nil { | ||
lowerBound := int(float64(*instanceTypeInfo.VCpuInfo.DefaultVCpus) * AggregateLowPercentile) | ||
upperBound := int(float64(*instanceTypeInfo.VCpuInfo.DefaultVCpus) * AggregateHighPercentile) | ||
filters.VCpusRange = &IntRangeFilter{LowerBound: lowerBound, UpperBound: upperBound} | ||
} | ||
filters.InstanceTypeBase = nil | ||
|
||
return filters, nil | ||
} | ||
|
||
// TransformFlexible transforms lower level filters based on a set of opinions | ||
func (itf Selector) TransformFlexible(filters Filters) (Filters, error) { | ||
if filters.Flexible == nil { | ||
return filters, nil | ||
} | ||
if filters.CPUArchitecture == nil { | ||
filters.CPUArchitecture = aws.String("x86_64") | ||
} | ||
if filters.BareMetal == nil { | ||
filters.BareMetal = aws.Bool(false) | ||
} | ||
if filters.Fpga == nil { | ||
filters.Fpga = aws.Bool(false) | ||
} | ||
|
||
if filters.AllowList == nil { | ||
baseAllowedInstanceTypes, err := regexp.Compile("^[cmr][3-9][ag]?\\..*$|^a[1-9]\\..*$|^t[2-9]\\..*$") | ||
if err != nil { | ||
return filters, err | ||
} | ||
filters.AllowList = baseAllowedInstanceTypes | ||
} | ||
|
||
if filters.VCpusRange == nil && filters.MemoryRange == nil { | ||
defaultVcpus := 4 | ||
filters.VCpusRange = &IntRangeFilter{LowerBound: defaultVcpus, UpperBound: defaultVcpus} | ||
} | ||
|
||
return filters, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package selector_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/amazon-ec2-instance-selector/pkg/selector" | ||
h "github.com/aws/amazon-ec2-instance-selector/pkg/test" | ||
) | ||
|
||
// Tests | ||
|
||
func TestTransformBaseInstanceType(t *testing.T) { | ||
ec2Mock := mockedEC2{ | ||
DescribeInstanceTypesResp: setupMock(t, describeInstanceTypes, "c4_large.json").DescribeInstanceTypesResp, | ||
DescribeInstanceTypesPagesResp: setupMock(t, describeInstanceTypesPages, "25_instances.json").DescribeInstanceTypesPagesResp, | ||
DescribeInstanceTypeOfferingsResp: setupMock(t, describeInstanceTypeOfferings, "us-east-2a.json").DescribeInstanceTypeOfferingsResp, | ||
} | ||
itf := selector.Selector{ | ||
EC2: ec2Mock, | ||
} | ||
instanceTypeBase := "c4.large" | ||
filters := selector.Filters{ | ||
InstanceTypeBase: &instanceTypeBase, | ||
} | ||
filters, err := itf.TransformBaseInstanceType(filters) | ||
h.Ok(t, err) | ||
h.Assert(t, *filters.BareMetal == false, " should filter out bare metal instances") | ||
h.Assert(t, *filters.Fpga == false, "should filter out FPGA instances") | ||
h.Assert(t, *filters.CPUArchitecture == "x86_64", "should only return x86_64 instance types") | ||
} | ||
|
||
func TestTransformFamilyFlexibile(t *testing.T) { | ||
itf := selector.Selector{} | ||
flexible := true | ||
filters := selector.Filters{ | ||
Flexible: &flexible, | ||
} | ||
filters, err := itf.TransformFlexible(filters) | ||
h.Ok(t, err) | ||
h.Assert(t, *filters.BareMetal == false, " should filter out bare metal instances") | ||
h.Assert(t, *filters.Fpga == false, "should filter out FPGA instances") | ||
h.Assert(t, *filters.CPUArchitecture == "x86_64", "should only return x86_64 instance types") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters