Skip to content

Commit

Permalink
Add support for allow/deny regexp(s) in instanceSelector (#8247)
Browse files Browse the repository at this point in the history
Signed-off-by: Davanum Srinivas <davanum@gmail.com>
  • Loading branch information
dims authored Feb 24, 2025
1 parent 791898f commit 270bd7a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/apis/eksctl.io/v1alpha5/assets/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,11 @@
},
"InstanceSelector": {
"properties": {
"allow": {
"type": "string",
"description": "List of allowed instance types to select from w/ regex syntax (Example: m[3-5]\\\\.*)",
"x-intellij-html-description": "List of allowed instance types to select from w/ regex syntax (Example: m[3-5]\\.*)"
},
"cpuArchitecture": {
"type": "string",
"description": "CPU Architecture of the EC2 instance type. Valid variants are: `\"x86_64\"` `\"amd64\"` `\"arm64\"`",
Expand All @@ -1221,6 +1226,11 @@
"arm64"
]
},
"deny": {
"type": "string",
"description": "List of instance types which should be excluded w/ regex syntax (Example: m[1-2]\\\\.*)",
"x-intellij-html-description": "List of instance types which should be excluded w/ regex syntax (Example: m[1-2]\\.*)"
},
"gpus": {
"type": "integer",
"description": "specifies the number of GPUs. It can be set to 0 to select non-GPU instance types.",
Expand All @@ -1241,7 +1251,9 @@
"vCPUs",
"memory",
"gpus",
"cpuArchitecture"
"cpuArchitecture",
"allow",
"deny"
],
"additionalProperties": false,
"description": "holds EC2 instance selector options",
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/eksctl.io/v1alpha5/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,11 @@ type InstanceSelector struct {
// `"amd64"`
// `"arm64"`
CPUArchitecture string `json:"cpuArchitecture,omitempty"`
// List of allowed instance types to select from w/ regex syntax (Example: m[3-5]\\.*)
Allow *string `json:"allow,omitempty"`

// List of instance types which should be excluded w/ regex syntax (Example: m[1-2]\\.*)
Deny *string `json:"deny,omitempty"`
}

// IsZero returns true if all fields hold a zero value
Expand Down
46 changes: 46 additions & 0 deletions pkg/eks/instance_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,52 @@ var _ = Describe("Instance Selector", func() {
createFakeInstanceSelector: makeInstanceSelector("c3.large", "c4.large", "c5.large"),
expectedInstanceTypes: []string{"c3.large", "c4.large", "c5.large"},
}),

Entry("valid instance selector criteria with Allow field", instanceSelectorCase{
nodeGroups: []api.NodePool{
&api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{},
},
&api.ManagedNodeGroup{
NodeGroupBase: &api.NodeGroupBase{},
},
},
instanceSelectorValue: &api.InstanceSelector{
CPUArchitecture: "x86_64",
Allow: aws.String(`c5[a-d]+\..*`),
},
createFakeInstanceSelector: makeInstanceSelector(
"c5a.2xlarge",
"c5d.2xlarge",
),
expectedInstanceTypes: []string{"c5a.2xlarge",
"c5d.2xlarge"},
clusterAZs: []string{"az1", "az2"},
expectedAZs: []string{"az1", "az2"},
}),

Entry("valid instance selector criteria with Deny field", instanceSelectorCase{
nodeGroups: []api.NodePool{
&api.NodeGroup{
NodeGroupBase: &api.NodeGroupBase{},
},
&api.ManagedNodeGroup{
NodeGroupBase: &api.NodeGroupBase{},
},
},
instanceSelectorValue: &api.InstanceSelector{
CPUArchitecture: "x86_64",
Deny: aws.String(`c4.*`),
},
createFakeInstanceSelector: makeInstanceSelector(
"c5a.2xlarge",
"c5d.2xlarge",
),
expectedInstanceTypes: []string{"c5a.2xlarge",
"c5d.2xlarge"},
clusterAZs: []string{"az1", "az2"},
expectedAZs: []string{"az1", "az2"},
}),
)
})

Expand Down
17 changes: 17 additions & 0 deletions pkg/eks/nodegroup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"regexp"
"strings"

"github.com/aws/amazon-ec2-instance-selector/v2/pkg/bytequantity"
Expand Down Expand Up @@ -209,6 +210,22 @@ func (n *NodeGroupService) expandInstanceSelector(ins *api.InstanceSelector, azs

filters.CPUArchitecture = (*ec2types.ArchitectureType)(aws.String(cpuArch))

if ins.Allow != nil {
regexVal, err := regexp.Compile(*ins.Allow)
if err != nil {
return nil, errors.Wrapf(err, "invalid value %q for instanceSelector.allow", *ins.Allow)
}
filters.AllowList = regexVal
}

if ins.Deny != nil {
regexVal, err := regexp.Compile(*ins.Deny)
if err != nil {
return nil, errors.Wrapf(err, "invalid value %q for instanceSelector.deny", *ins.Deny)
}
filters.DenyList = regexVal
}

instanceTypes, err := n.instanceSelector.Filter(context.TODO(), filters)
if err != nil {
return nil, errors.Wrap(err, "error querying instance types for the specified instance selector criteria")
Expand Down

0 comments on commit 270bd7a

Please sign in to comment.