Skip to content

Commit

Permalink
Selector Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Nov 11, 2023
1 parent f5e7e5a commit 6286881
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
5 changes: 4 additions & 1 deletion pkg/controllers/nodeclass/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ var _ = Describe("NodeClassController", func() {
It("Should resolve a valid selectors for Subnet by tags", func() {
nodeClass.Spec.SubnetSelectorTerms = []v1beta1.SubnetSelectorTerm{
{
Tags: map[string]string{`Name`: `test-subnet-1,test-subnet-2`},
Tags: map[string]string{`Name`: `test-subnet-1`},
},
{
Tags: map[string]string{`Name`: `test-subnet-2`},
},
}
ExpectApplied(ctx, env.Client, nodeClass)
Expand Down
5 changes: 4 additions & 1 deletion pkg/providers/launchtemplate/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,10 @@ var _ = Describe("LaunchTemplates", func() {
})
Context("Subnet-based Launch Template Configration", func() {
It("should explicitly set 'AssignPublicIPv4' to false in the Launch Template", func() {
nodeClass.Spec.SubnetSelectorTerms = []v1beta1.SubnetSelectorTerm{{Tags: map[string]string{"Name": "test-subnet-1,test-subnet-3"}}}
nodeClass.Spec.SubnetSelectorTerms = []v1beta1.SubnetSelectorTerm{
{Tags: map[string]string{"Name": "test-subnet-1"}},
{Tags: map[string]string{"Name": "test-subnet-3"}},
}
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod()
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
Expand Down
10 changes: 5 additions & 5 deletions pkg/utils/nodeclass/nodeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewSubnetSelectorTerms(subnetSelector map[string]string) (terms []v1beta1.S
for k, v := range subnetSelector {
switch k {
case "aws-ids", "aws::ids":
ids = strings.Split(strings.Trim(v, " "), ",")
ids = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
default:
tags[k] = v
}
Expand All @@ -97,7 +97,7 @@ func NewSecurityGroupSelectorTerms(securityGroupSelector map[string]string) (ter
for k, v := range securityGroupSelector {
switch k {
case "aws-ids", "aws::ids":
ids = strings.Split(strings.Trim(v, " "), ",")
ids = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
default:
tags[k] = v
}
Expand All @@ -124,11 +124,11 @@ func NewAMISelectorTerms(amiSelector map[string]string) (terms []v1beta1.AMISele
for k, v := range amiSelector {
switch k {
case "aws-ids", "aws::ids":
ids = strings.Split(strings.Trim(v, " "), ",")
ids = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
case "aws::name":
names = strings.Split(strings.Trim(v, " "), ",")
names = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
case "aws::owners":
owners = strings.Split(strings.Trim(v, " "), ",")
owners = lo.Map(strings.Split(v, ","), func(s string, _ int) string { return strings.Trim(s, " ") })
default:
tags[k] = v
}
Expand Down
98 changes: 98 additions & 0 deletions pkg/utils/nodeclass/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,104 @@ var _ = Describe("NodeClassUtils", func() {
ExpectSecurityGroupStatusEqual(nodeTemplate.Status.SecurityGroups, nodeClass.Status.SecurityGroups)
ExpectAMIStatusEqual(nodeTemplate.Status.AMIs, nodeClass.Status.AMIs)
})
It("should convert a AWSNodeTemplate to a EC2NodeClass (with AMISelector name and owner values set) with spaces", func() {
nodeTemplate.Spec.AMISelector = map[string]string{
"aws::name": "ami-name1, ami-name2, test name",
"aws::owners": "self, amazon, 123456789, test owner",
}
nodeClass := nodeclassutil.New(nodeTemplate)

for k, v := range nodeTemplate.Annotations {
Expect(nodeClass.Annotations).To(HaveKeyWithValue(k, v))
}
for k, v := range nodeTemplate.Labels {
Expect(nodeClass.Labels).To(HaveKeyWithValue(k, v))
}
Expect(nodeClass.Spec.SubnetSelectorTerms).To(HaveLen(1))
Expect(nodeClass.Spec.SubnetSelectorTerms[0].Tags).To(Equal(nodeTemplate.Spec.SubnetSelector))
Expect(nodeClass.Spec.SecurityGroupSelectorTerms).To(HaveLen(1))
Expect(nodeClass.Spec.SecurityGroupSelectorTerms[0].Tags).To(Equal(nodeTemplate.Spec.SecurityGroupSelector))

// Expect AMISelectorTerms to be exactly what we would expect from the filtering above
Expect(nodeClass.Spec.AMISelectorTerms).To(HaveLen(12))
Expect(nodeClass.Spec.AMISelectorTerms).To(ConsistOf(
v1beta1.AMISelectorTerm{
Name: "ami-name1",
Owner: "self",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name1",
Owner: "amazon",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name1",
Owner: "123456789",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name1",
Owner: "test owner",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name2",
Owner: "self",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name2",
Owner: "amazon",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name2",
Owner: "123456789",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "ami-name2",
Owner: "test owner",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "test name",
Owner: "self",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "test name",
Owner: "amazon",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "test name",
Owner: "123456789",
Tags: map[string]string{},
},
v1beta1.AMISelectorTerm{
Name: "test name",
Owner: "test owner",
Tags: map[string]string{},
},
))

Expect(nodeClass.Spec.AMIFamily).To(Equal(nodeTemplate.Spec.AMIFamily))
Expect(nodeClass.Spec.UserData).To(Equal(nodeTemplate.Spec.UserData))
Expect(nodeClass.Spec.Role).To(BeEmpty())
Expect(nodeClass.Spec.Tags).To(Equal(nodeTemplate.Spec.Tags))
ExpectBlockDeviceMappingsEqual(nodeTemplate.Spec.BlockDeviceMappings, nodeClass.Spec.BlockDeviceMappings)
Expect(nodeClass.Spec.DetailedMonitoring).To(Equal(nodeTemplate.Spec.DetailedMonitoring))
ExpectMetadataOptionsEqual(nodeTemplate.Spec.MetadataOptions, nodeClass.Spec.MetadataOptions)
Expect(nodeClass.Spec.Context).To(Equal(nodeTemplate.Spec.Context))
Expect(nodeClass.Spec.LaunchTemplateName).To(Equal(nodeTemplate.Spec.LaunchTemplateName))
Expect(nodeClass.Spec.InstanceProfile).To(Equal(nodeTemplate.Spec.InstanceProfile))

ExpectSubnetStatusEqual(nodeTemplate.Status.Subnets, nodeClass.Status.Subnets)
ExpectSecurityGroupStatusEqual(nodeTemplate.Status.SecurityGroups, nodeClass.Status.SecurityGroups)
ExpectAMIStatusEqual(nodeTemplate.Status.AMIs, nodeClass.Status.AMIs)
})
It("should convert a AWSNodeTemplate to a EC2NodeClass (with AMISelector id set)", func() {
nodeTemplate.Spec.AMISelector = map[string]string{
"aws::ids": "ami-1234,ami-5678,ami-custom-id",
Expand Down

0 comments on commit 6286881

Please sign in to comment.