Skip to content

Commit 5dd8215

Browse files
committed
update cli and selector pkg to byte quantity for memory filters
1 parent 0e05ac2 commit 5dd8215

File tree

14 files changed

+428
-174
lines changed

14 files changed

+428
-174
lines changed

cmd/examples/example1.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/aws/amazon-ec2-instance-selector/pkg/bytequantity"
67
"github.com/aws/amazon-ec2-instance-selector/pkg/selector"
78
"github.com/aws/aws-sdk-go/aws"
89
"github.com/aws/aws-sdk-go/aws/session"
@@ -27,10 +28,10 @@ func main() {
2728
LowerBound: 2,
2829
UpperBound: 4,
2930
}
30-
// Instantiate a float64 range filter to specify min and max memory in GiB
31-
memoryRange := selector.Float64RangeFilter{
32-
LowerBound: 1.0,
33-
UpperBound: 4.0,
31+
// Instantiate a byte quantity range filter to specify min and max memory in GiB
32+
memoryRange := selector.ByteQuantityRangeFilter{
33+
LowerBound: bytequantity.FromGiB(2),
34+
UpperBound: bytequantity.FromGiB(4),
3435
}
3536
// Create a string for the CPU Architecture so that it can be passed as a pointer
3637
// when creating the Filter struct

cmd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ Full docs can be found at github.com/aws/amazon-` + binName
116116
// Filter Flags - These will be grouped at the top of the help flags
117117

118118
cli.IntMinMaxRangeFlags(vcpus, cli.StringMe("c"), nil, "Number of vcpus available to the instance type.")
119-
cli.Float64MinMaxRangeFlags(memory, cli.StringMe("m"), nil, "Amount of Memory available in GiB (Example: 4)")
119+
cli.ByteQuantityMinMaxRangeFlags(memory, cli.StringMe("m"), nil, "Amount of Memory available in GiB (Example: 4)")
120120
cli.RatioFlag(vcpusToMemoryRatio, nil, nil, "The ratio of vcpus to memory in MiB. (Example: 1:2)")
121121
cli.StringFlag(cpuArchitecture, cli.StringMe("a"), nil, "CPU architecture [x86_64, i386, or arm64]", nil)
122122
cli.IntMinMaxRangeFlags(gpus, cli.StringMe("g"), nil, "Total Number of GPUs (Example: 4)")
123-
cli.Float64MinMaxRangeFlags(gpuMemoryTotal, nil, nil, "Number of GPUs' total memory in GiB (Example: 4)")
123+
cli.ByteQuantityMinMaxRangeFlags(gpuMemoryTotal, nil, nil, "Number of GPUs' total memory in GiB (Example: 4)")
124124
cli.StringFlag(placementGroupStrategy, nil, nil, "Placement group strategy: [cluster, partition, spread]", nil)
125125
cli.StringFlag(usageClass, cli.StringMe("u"), nil, "Usage class: [spot or on-demand]", nil)
126126
cli.StringFlag(rootDeviceType, nil, nil, "Supported root device types: [ebs or instance-store]", nil)
@@ -189,11 +189,11 @@ Full docs can be found at github.com/aws/amazon-` + binName
189189

190190
filters := selector.Filters{
191191
VCpusRange: cli.IntRangeMe(flags[vcpus]),
192-
MemoryRange: cli.Float64RangeMe(flags[memory]),
192+
MemoryRange: cli.ByteQuantityRangeMe(flags[memory]),
193193
VCpusToMemoryRatio: cli.Float64Me(flags[vcpusToMemoryRatio]),
194194
CPUArchitecture: cli.StringMe(flags[cpuArchitecture]),
195195
GpusRange: cli.IntRangeMe(flags[gpus]),
196-
GpuMemoryRange: cli.Float64RangeMe(flags[gpuMemoryTotal]),
196+
GpuMemoryRange: cli.ByteQuantityRangeMe(flags[gpuMemoryTotal]),
197197
PlacementGroupStrategy: cli.StringMe(flags[placementGroupStrategy]),
198198
UsageClass: cli.StringMe(flags[usageClass]),
199199
RootDeviceType: cli.StringMe(flags[rootDeviceType]),

pkg/cli/cli.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"strings"
2222

23+
"github.com/aws/amazon-ec2-instance-selector/pkg/bytequantity"
2324
"github.com/aws/amazon-ec2-instance-selector/pkg/selector"
2425
"github.com/spf13/cobra"
2526
"github.com/spf13/pflag"
@@ -42,6 +43,7 @@ func New(binaryName string, shortUsage string, longUsage, examples string, run r
4243
nilDefaults: map[string]bool{},
4344
rangeFlags: map[string]bool{},
4445
validators: map[string]validator{},
46+
processors: map[string]processor{},
4547
suiteFlags: pflag.NewFlagSet("suite", pflag.ExitOnError),
4648
}
4749
}
@@ -52,24 +54,23 @@ func (cl *CommandLineInterface) ParseFlags() (map[string]interface{}, error) {
5254
// Remove Suite Flags so that args only include Config and Filter Flags
5355
cl.Command.SetArgs(removeIntersectingArgs(cl.suiteFlags))
5456
// This parses Config and Filter flags only
55-
err := cl.Command.Execute()
56-
if err != nil {
57+
if err := cl.Command.Execute(); err != nil {
5758
return nil, err
5859
}
60+
5961
// Remove Config and Filter flags so that only suite flags are parsed
60-
err = cl.suiteFlags.Parse(removeIntersectingArgs(cl.Command.Flags()))
61-
if err != nil {
62+
if err := cl.suiteFlags.Parse(removeIntersectingArgs(cl.Command.Flags())); err != nil {
6263
return nil, err
6364
}
65+
6466
// Add suite flags to Command flagset so that other processing can occur
6567
// This has to be done after usage is printed so that the flagsets can be grouped properly when printed
6668
cl.Command.Flags().AddFlagSet(cl.suiteFlags)
67-
err = cl.SetUntouchedFlagValuesToNil()
68-
if err != nil {
69+
if err := cl.SetUntouchedFlagValuesToNil(); err != nil {
6970
return nil, err
7071
}
71-
err = cl.ProcessRangeFilterFlags()
72-
if err != nil {
72+
73+
if err := cl.ProcessFlags(); err != nil {
7374
return nil, err
7475
}
7576
return cl.Flags, nil
@@ -82,13 +83,29 @@ func (cl *CommandLineInterface) ParseAndValidateFlags() (map[string]interface{},
8283
if err != nil {
8384
return nil, err
8485
}
85-
err = cl.ValidateFlags()
86-
if err != nil {
86+
if err := cl.ValidateFlags(); err != nil {
8787
return nil, err
8888
}
8989
return flags, nil
9090
}
9191

92+
// ProcessFlags iterates through any registered processors and executes them
93+
// Processors are executed before validators
94+
func (cl *CommandLineInterface) ProcessFlags() error {
95+
for flagName, processorFn := range cl.processors {
96+
if processorFn == nil {
97+
continue
98+
}
99+
if err := processorFn(cl.Flags[flagName]); err != nil {
100+
return err
101+
}
102+
}
103+
if err := cl.ProcessRangeFilterFlags(); err != nil {
104+
return err
105+
}
106+
return nil
107+
}
108+
92109
// ValidateFlags iterates through any registered validators and executes them
93110
func (cl *CommandLineInterface) ValidateFlags() error {
94111
for flagName, validationFn := range cl.validators {
@@ -164,8 +181,8 @@ func (cl *CommandLineInterface) SetUntouchedFlagValuesToNil() error {
164181
if reflect.ValueOf(*v).IsZero() {
165182
cl.Flags[f.Name] = nil
166183
}
167-
case *float64:
168-
if reflect.ValueOf(*v).IsZero() {
184+
case *bytequantity.ByteQuantity:
185+
if v.Quantity == 0 {
169186
cl.Flags[f.Name] = nil
170187
}
171188
case *string:
@@ -212,15 +229,19 @@ func (cl *CommandLineInterface) ProcessRangeFilterFlags() error {
212229
switch cl.Flags[rangeHelperMax].(type) {
213230
case *int:
214231
cl.Flags[rangeHelperMin] = cl.IntMe(0)
215-
case *float64:
216-
cl.Flags[rangeHelperMin] = cl.Float64Me(0)
232+
case *bytequantity.ByteQuantity:
233+
cl.Flags[rangeHelperMin] = cl.ByteQuantityMe(bytequantity.ByteQuantity{Quantity: 0})
234+
default:
235+
return fmt.Errorf("Unable to set %s", rangeHelperMax)
217236
}
218237
} else if cl.Flags[rangeHelperMax] == nil {
219238
switch cl.Flags[rangeHelperMin].(type) {
220239
case *int:
221240
cl.Flags[rangeHelperMax] = cl.IntMe(maxInt)
222-
case *float64:
223-
cl.Flags[rangeHelperMax] = cl.Float64Me(maxFloat64)
241+
case *bytequantity.ByteQuantity:
242+
cl.Flags[rangeHelperMax] = cl.ByteQuantityMe(bytequantity.ByteQuantity{Quantity: maxUint64})
243+
default:
244+
return fmt.Errorf("Unable to set %s", rangeHelperMin)
224245
}
225246
}
226247

@@ -230,13 +251,12 @@ func (cl *CommandLineInterface) ProcessRangeFilterFlags() error {
230251
LowerBound: *cl.IntMe(cl.Flags[rangeHelperMin]),
231252
UpperBound: *cl.IntMe(cl.Flags[rangeHelperMax]),
232253
}
233-
case *float64:
234-
cl.Flags[flagName] = &selector.Float64RangeFilter{
235-
LowerBound: *cl.Float64Me(cl.Flags[rangeHelperMin]),
236-
UpperBound: *cl.Float64Me(cl.Flags[rangeHelperMax]),
254+
case *bytequantity.ByteQuantity:
255+
cl.Flags[flagName] = &selector.ByteQuantityRangeFilter{
256+
LowerBound: *cl.ByteQuantityMe(cl.Flags[rangeHelperMin]),
257+
UpperBound: *cl.ByteQuantityMe(cl.Flags[rangeHelperMax]),
237258
}
238259
}
239-
240260
}
241261
return nil
242262
}

0 commit comments

Comments
 (0)