Skip to content

Commit 5144d53

Browse files
committed
case insensitive selector evaluation for strings
1 parent 12196cb commit 5144d53

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ Filter Flags:
395395
-z, --availability-zones strings Availability zones or zone ids to check EC2 capacity offered in specific AZs
396396
--baremetal Bare Metal instance types (.metal instances)
397397
-b, --burst-support Burstable instance types
398-
-a, --cpu-architecture string CPU architecture [x86_64, amd64, x86_64_mac, i386, or arm64]
399-
--cpu-manufacturer string CPU manufacturer [amd, intel, aws]
398+
-a, --cpu-architecture string CPU architecture [x86_64, amd64, x86_64_mac, i386, arm64, or arm64_mac]
399+
--cpu-manufacturer string CPU manufacturer [amd, intel, aws, apple]
400400
--current-generation Current generation instance types (explicitly set this to false to not return current generation instance types)
401401
--dedicated-hosts Dedicated Hosts supported
402402
--deny-list string List of instance types which should be excluded w/ regex syntax (Example: m[1-2]\.*)

cmd/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ Full docs can be found at github.com/aws/amazon-` + binName
168168
cli.Int32MinMaxRangeFlags(vcpus, cli.StringMe("c"), nil, "Number of vcpus available to the instance type.")
169169
cli.ByteQuantityMinMaxRangeFlags(memory, cli.StringMe("m"), nil, "Amount of Memory available (Example: 4 GiB)")
170170
cli.RatioFlag(vcpusToMemoryRatio, nil, nil, "The ratio of vcpus to GiBs of memory. (Example: 1:2)")
171-
cli.StringOptionsFlag(cpuArchitecture, cli.StringMe("a"), nil, "CPU architecture [x86_64, amd64, x86_64_mac, i386, or arm64]", []string{"x86_64", "x86_64_mac", "amd64", "i386", "arm64"})
172-
cli.StringOptionsFlag(cpuManufacturer, nil, nil, "CPU manufacturer [amd, intel, aws]", []string{"amd", "intel", "aws"})
171+
cli.StringOptionsFlag(cpuArchitecture, cli.StringMe("a"), nil, "CPU architecture [x86_64, amd64, x86_64_mac, i386, arm64, or arm64_mac]", []string{"x86_64", "x86_64_mac", "amd64", "i386", "arm64", "arm64_mac"})
172+
cli.StringOptionsFlag(cpuManufacturer, nil, nil, "CPU manufacturer [amd, intel, aws, apple]", []string{"amd", "intel", "aws", "apple"})
173173
cli.Int32MinMaxRangeFlags(gpus, cli.StringMe("g"), nil, "Total Number of GPUs (Example: 4)")
174174
cli.ByteQuantityMinMaxRangeFlags(gpuMemoryTotal, nil, nil, "Number of GPUs' total memory (Example: 4 GiB)")
175175
cli.StringFlag(gpuManufacturer, nil, nil, "GPU Manufacturer name (Example: NVIDIA)", nil)

pkg/cli/flags.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func (cl *CommandLineInterface) StringOptionsFlagOnFlagSet(flagSet *pflag.FlagSe
386386
return nil
387387
}
388388
for _, v := range validOpts {
389-
if v == *val.(*string) {
389+
if strings.EqualFold(v, *val.(*string)) {
390390
return nil
391391
}
392392
}

pkg/selector/comparators.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const (
2929
)
3030

3131
var (
32-
amdRegex = regexp.MustCompile(`[a-zA-Z0-9]+a\\.[a-zA-Z0-9]`)
3332
networkPerfRE = regexp.MustCompile(`[0-9]+ Gigabit`)
3433
generationRE = regexp.MustCompile(`[a-zA-Z]+([0-9]+)`)
3534
)
@@ -41,7 +40,7 @@ func isSupportedFromString(instanceTypeValue *string, target *string) bool {
4140
if instanceTypeValue == nil {
4241
return false
4342
}
44-
return *instanceTypeValue == *target
43+
return strings.EqualFold(*instanceTypeValue, *target)
4544
}
4645

4746
func isSupportedFromStrings(instanceTypeValues []*string, target *string) bool {
@@ -83,7 +82,7 @@ func isSupportedUsageClassType(instanceTypeValue []ec2types.UsageClassType, targ
8382
}
8483

8584
for _, potentialType := range instanceTypeValue {
86-
if potentialType == *target {
85+
if strings.EqualFold(string(potentialType), string(*target)) {
8786
return true
8887
}
8988
}
@@ -102,7 +101,7 @@ func isSupportedArchitectureType(instanceTypeValue []ec2types.ArchitectureType,
102101
}
103102

104103
for _, potentialType := range instanceTypeValue {
105-
if potentialType == *target {
104+
if strings.EqualFold(string(potentialType), string(*target)) {
106105
return true
107106
}
108107
}
@@ -120,7 +119,7 @@ func isSupportedVirtualizationType(instanceTypeValue []ec2types.VirtualizationTy
120119
return true
121120
}
122121
for _, potentialType := range instanceTypeValue {
123-
if potentialType == *target {
122+
if strings.EqualFold(string(potentialType), string(*target)) {
124123
return true
125124
}
126125
}
@@ -134,7 +133,7 @@ func isSupportedInstanceTypeHypervisorType(instanceTypeValue ec2types.InstanceTy
134133
if reflect.ValueOf(*target).IsZero() {
135134
return true
136135
}
137-
if instanceTypeValue == *target {
136+
if strings.EqualFold(string(instanceTypeValue), string(*target)) {
138137
return true
139138
}
140139
return false
@@ -151,7 +150,7 @@ func isSupportedRootDeviceType(instanceTypeValue []ec2types.RootDeviceType, targ
151150
return true
152151
}
153152
for _, potentialType := range instanceTypeValue {
154-
if potentialType == *target {
153+
if strings.EqualFold(string(potentialType), string(*target)) {
155154
return true
156155
}
157156
}
@@ -165,7 +164,7 @@ func isMatchingCpuArchitecture(instanceTypeValue CPUManufacturer, target *CPUMan
165164
if reflect.ValueOf(*target).IsZero() {
166165
return true
167166
}
168-
if instanceTypeValue == *target {
167+
if strings.EqualFold(string(instanceTypeValue), string(*target)) {
169168
return true
170169
}
171170
return false
@@ -376,19 +375,6 @@ func getEBSOptimizedBaselineIOPS(ebsInfo *ec2types.EbsInfo) *int32 {
376375
return ebsInfo.EbsOptimizedInfo.BaselineIops
377376
}
378377

379-
func getCPUManufacturer(instanceTypeInfo *ec2types.InstanceTypeInfo) CPUManufacturer {
380-
for _, it := range instanceTypeInfo.ProcessorInfo.SupportedArchitectures {
381-
if it == ec2types.ArchitectureTypeArm64 {
382-
return CPUManufacturerAWS
383-
}
384-
}
385-
386-
if amdRegex.Match([]byte(instanceTypeInfo.InstanceType)) {
387-
return CPUManufacturerAMD
388-
}
389-
return CPUManufacturerIntel
390-
}
391-
392378
// getInstanceTypeGeneration returns the generation from an instance type name
393379
// i.e. c7i.xlarge -> 7
394380
// if any error occurs, 0 will be returned.

pkg/selector/selector.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,16 @@ func (s Selector) prepareFilter(ctx context.Context, filters Filters, instanceTy
309309
filterInstanceTypes = nil
310310
}
311311

312+
var cpuManufacturerFilter *string
313+
if filters.CPUManufacturer != nil {
314+
cpuManufacturerFilter = aws.String(string(*filters.CPUManufacturer))
315+
}
316+
312317
// filterToInstanceSpecMappingPairs is a map of filter name [key] to filter pair [value].
313318
// A filter pair includes user input filter value and instance spec value retrieved from DescribeInstanceTypes
314319
filterToInstanceSpecMappingPairs := map[string]filterPair{
315320
cpuArchitecture: {filters.CPUArchitecture, instanceTypeInfo.ProcessorInfo.SupportedArchitectures},
316-
cpuManufacturer: {filters.CPUManufacturer, getCPUManufacturer(&instanceTypeInfo.InstanceTypeInfo)},
321+
cpuManufacturer: {cpuManufacturerFilter, instanceTypeInfo.ProcessorInfo.Manufacturer},
317322
usageClass: {filters.UsageClass, instanceTypeInfo.SupportedUsageClasses},
318323
rootDeviceType: {filters.RootDeviceType, instanceTypeInfo.SupportedRootDeviceTypes},
319324
hibernationSupported: {filters.HibernationSupported, instanceTypeInfo.HibernationSupported},

0 commit comments

Comments
 (0)