-
Notifications
You must be signed in to change notification settings - Fork 107
Instance sorting #138
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
Instance sorting #138
Changes from 11 commits
6c596cd
8ea3f10
ef7b781
6c5ce45
5035033
27a434d
f0fcb1b
36bbfb3
60fdce7
2871abf
244789b
bdf0a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,19 +48,32 @@ func main() { | |
} | ||
|
||
// Pass the Filter struct to the FilteredInstanceTypes function of your | ||
// selector instance to get a list of filtered instance types and their details | ||
// selector instance to get a list of filtered instance types and their details. | ||
instanceTypesSlice, err := instanceSelector.FilterInstanceTypes(filters) | ||
if err != nil { | ||
fmt.Printf("Oh no, there was an error getting instance types: %v", err) | ||
return | ||
} | ||
|
||
// Pass in your list of instance type details to the appropriate output function | ||
// in order to format the instance types as printable strings. | ||
maxResults := 100 | ||
// Pass in the list of instance type details to the SortInstanceTypes function | ||
// if you wish to sort the instances based on set filters. | ||
sortFilter := "instance-type-name" | ||
sortDirection := "ascending" | ||
instanceTypesSlice, err = instanceSelector.SortInstanceTypes(instanceTypesSlice, &sortFilter, &sortDirection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking on this more, I think that we should make this compatible w/ v2 of instance-selector. The original Filter functions could maintain functionality. In fact, I think it makes sense to decouple sorting from the selector pkg all together. It's trivial to sort the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does make sense to me how a user of the instance-selector as a library could just manually sort the instance types themselves, so I do agree that sorting could just be part of the CLI. The idea of adding sorting to v2 is also one that I definitely agree with. There is one issue: in order to reduce the number of API calls relating to fetching spot and OD pricing, the pricing caches are only hydrated if the user sets the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think docs would be fine for now. If there's a strong signal from users that the limitation is hindering the use of the lib, then we can reevaluate at that time. |
||
if err != nil { | ||
fmt.Printf("Oh no, there was an error sorting instance types: %v", err) | ||
return | ||
} | ||
|
||
// Truncate results and format them for output with your desired formatting function. | ||
// All formatting functions can be found here: | ||
// https://github.com/aws/amazon-ec2-instance-selector/blob/main/pkg/selector/outputs/outputs.go | ||
// Examples of formatted outputs can be found here: | ||
// https://github.com/aws/amazon-ec2-instance-selector#examples | ||
maxResults := 10 | ||
instanceTypesSlice, _, err = outputs.TruncateResults(&maxResults, instanceTypesSlice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we really need this for outputs. It's trivial to truncate a slice. It's not clear why this returns 3 values too. What error could be returned from a truncation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is a good point. I am going to move
The 3 values are the truncated list, the number of truncated items, and an error. The error exists to catch invalid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this type of func, I'd drop the For example,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh that is a very good idea. I'll change the output of |
||
if err != nil { | ||
fmt.Printf("Oh no, there was an error truncating instnace types: %v", err) | ||
fmt.Printf("Oh no, there was an error truncating instance types: %v", err) | ||
return | ||
} | ||
instanceTypes := outputs.SimpleInstanceTypeOutput(instanceTypesSlice) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,15 +100,17 @@ const ( | |
|
||
// Configuration Flag Constants | ||
const ( | ||
maxResults = "max-results" | ||
profile = "profile" | ||
help = "help" | ||
verbose = "verbose" | ||
version = "version" | ||
region = "region" | ||
output = "output" | ||
cacheTTL = "cache-ttl" | ||
cacheDir = "cache-dir" | ||
maxResults = "max-results" | ||
profile = "profile" | ||
help = "help" | ||
verbose = "verbose" | ||
version = "version" | ||
region = "region" | ||
output = "output" | ||
cacheTTL = "cache-ttl" | ||
cacheDir = "cache-dir" | ||
sortDirection = "sort-direction" | ||
sortFilter = "sort-filter" | ||
|
||
// Output constants | ||
|
||
|
@@ -117,6 +119,17 @@ const ( | |
oneLineOutput = "one-line" | ||
simpleOutput = "simple" | ||
verboseOutput = "verbose" | ||
|
||
// Sorting constants | ||
|
||
sortODPrice = "on-demand-price" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these should be more generic where you could select an arbitrary jsonpath of the struct def. For example, if I wanted to sort on memory I could do: ec2-instance-selector --vcpus-min=4 --sort-by='.MemoryInfo.SizeInMiB' maybe using this lib: https://github.com/oliveagle/jsonpath I think some flag shorthands could be modeled as well, but it needs to be more generic than hardcoded options. Maybe you could just make all quantity fields sortable by inspecting the type and then using the base of the option to determine the sort key. For example, ec2-instance-selector --vcpu=4 --sort-by='vcpus'
ec2-instance-selector --vcpu=4 --sort-by='ebs-optimized-basedline-bandwidth' Here's an example of this in the kubectl cli. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woah yea this is a great idea! I'll look into changing sorting to be more generic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stretch goal is to allow multiple sort dimensions. For example, if I wanted to sort by memory and then by vcpus when memory is equal, I could do this:
:) |
||
sortSpotPrice = "spot-price" | ||
sortVCPU = "vcpu" | ||
sortMemory = "memory" | ||
sortName = "instance-type-name" | ||
|
||
sortAscending = "ascending" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make shorthands for ascending and descending (i.e. |
||
sortDescending = "descending" | ||
) | ||
|
||
var ( | ||
|
@@ -147,6 +160,19 @@ Full docs can be found at github.com/aws/amazon-` + binName | |
simpleOutput, | ||
} | ||
|
||
cliSortCriteria := []string{ | ||
sortODPrice, | ||
sortSpotPrice, | ||
sortVCPU, | ||
sortMemory, | ||
sortName, | ||
} | ||
|
||
cliSortDirections := []string{ | ||
sortAscending, | ||
sortDescending, | ||
} | ||
|
||
// Registers flags with specific input types from the cli pkg | ||
// Filter Flags - These will be grouped at the top of the help flags | ||
|
||
|
@@ -211,6 +237,8 @@ Full docs can be found at github.com/aws/amazon-` + binName | |
cli.ConfigBoolFlag(verbose, cli.StringMe("v"), nil, "Verbose - will print out full instance specs") | ||
cli.ConfigBoolFlag(help, cli.StringMe("h"), nil, "Help") | ||
cli.ConfigBoolFlag(version, nil, nil, "Prints CLI version") | ||
cli.ConfigStringOptionsFlag(sortDirection, nil, cli.StringMe(sortAscending), fmt.Sprintf("Specify the direction to sort in (%s)", strings.Join(cliSortDirections, ", ")), cliSortDirections) | ||
cli.ConfigStringOptionsFlag(sortFilter, nil, cli.StringMe(sortName), fmt.Sprintf("Specify the field to sort by (%s)", strings.Join(cliSortCriteria, ", ")), cliSortCriteria) | ||
|
||
// Parses the user input with the registered flags and runs type specific validation on the user input | ||
flags, err := cli.ParseAndValidateFlags() | ||
|
@@ -335,6 +363,15 @@ Full docs can be found at github.com/aws/amazon-` + binName | |
os.Exit(1) | ||
} | ||
|
||
// sort instance types | ||
sortFilterFlag := cli.StringMe(flags[sortFilter]) | ||
sortDirectionFlag := cli.StringMe(flags[sortDirection]) | ||
instanceTypeDetails, err = instanceSelector.SortInstanceTypes(instanceTypeDetails, sortFilterFlag, sortDirectionFlag) | ||
if err != nil { | ||
fmt.Printf("An error occurred when sorting instance types: %v", err) | ||
AustinSiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
os.Exit(1) | ||
} | ||
|
||
// format instance types as strings | ||
maxOutputResults := cli.IntMe(flags[maxResults]) | ||
instanceTypes, itemsTruncated, err := formatInstanceTypes(instanceTypeDetails, maxOutputResults, outputFlag) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: what do you think about changing this to
sort-by
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I agree with this change.
sort-by
makes more intuitive sense for what the purpose of the flag is.