Skip to content

Commit

Permalink
Create Subnet controller
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Apr 25, 2024
1 parent f724f6e commit 1dd3fbe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
InstanceTypesAndZonesTTL = 5 * time.Minute
// InstanceProfileTTL is the time before we refresh checking instance profile existence at IAM
InstanceProfileTTL = 15 * time.Minute
// InstanceTypesAndZonesTTL is the time before we remove subnets that have been refreshed
SubnetTTL = 10 * time.Minute
)

const (
Expand Down
10 changes: 3 additions & 7 deletions pkg/cloudprovider/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,13 @@ func (c *CloudProvider) isAMIDrifted(ctx context.Context, nodeClaim *corev1beta1
// Checks if the security groups are drifted, by comparing the subnet returned from the subnetProvider
// to the ec2 instance subnets
func (c *CloudProvider) isSubnetDrifted(ctx context.Context, instance *instance.Instance, nodeClass *v1beta1.EC2NodeClass) (cloudprovider.DriftReason, error) {

Check failure on line 99 in pkg/cloudprovider/drift.go

View workflow job for this annotation

GitHub Actions / ci

`(*CloudProvider).isSubnetDrifted` - `ctx` is unused (unparam)
subnets, err := c.subnetProvider.List(ctx, nodeClass)
if err != nil {
return "", err
}
// subnets need to be found to check for drift
if len(subnets) == 0 {
if len(nodeClass.Status.Subnets) == 0 {
return "", fmt.Errorf("no subnets are discovered")
}

_, found := lo.Find(subnets, func(subnet *ec2.Subnet) bool {
return aws.StringValue(subnet.SubnetId) == instance.SubnetID
_, found := lo.Find(nodeClass.Status.Subnets, func(subnet v1beta1.Subnet) bool {
return subnet.ID == instance.SubnetID
})

if !found {
Expand Down
28 changes: 22 additions & 6 deletions pkg/providers/subnet/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,16 @@ var _ = Describe("SubnetProvider", func() {
})
Context("Provider Cache", func() {
It("should resolve subnets from cache that are filtered by id", func() {
expectedSubnets := awsEnv.EC2API.DescribeSubnetsOutput.Clone().Subnets
for _, subnet := range expectedSubnets {
expectedSubnets, err := awsEnv.EC2API.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: lo.ToPtr("tag-key"),
Values: []*string{lo.ToPtr("*")},
},
},
})
Expect(err).To(BeNil())
for _, subnet := range expectedSubnets.Subnets {
nodeClass.Spec.SubnetSelectorTerms = []v1beta1.SubnetSelectorTerm{
{
ID: *subnet.SubnetId,
Expand All @@ -257,12 +265,20 @@ var _ = Describe("SubnetProvider", func() {
for _, cachedObject := range awsEnv.SubnetCache.Items() {
cachedSubnet := cachedObject.Object.([]*ec2.Subnet)
Expect(cachedSubnet).To(HaveLen(1))
lo.Contains(expectedSubnets, cachedSubnet[0])
lo.Contains(expectedSubnets.Subnets, cachedSubnet[0])
}
})
It("should resolve subnets from cache that are filtered by tags", func() {
expectedSubnets := awsEnv.EC2API.DescribeSubnetsOutput.Clone().Subnets
tagSet := lo.Map(expectedSubnets, func(subnet *ec2.Subnet, _ int) map[string]string {
expectedSubnets, err := awsEnv.EC2API.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: lo.ToPtr("tag-key"),
Values: []*string{lo.ToPtr("*")},
},
},
})
Expect(err).To(BeNil())
tagSet := lo.Map(expectedSubnets.Subnets, func(subnet *ec2.Subnet, _ int) map[string]string {
tag, _ := lo.Find(subnet.Tags, func(tag *ec2.Tag) bool {
return lo.FromPtr(tag.Key) == "Name"
})
Expand All @@ -282,7 +298,7 @@ var _ = Describe("SubnetProvider", func() {
for _, cachedObject := range awsEnv.SubnetCache.Items() {
cachedSubnet := cachedObject.Object.([]*ec2.Subnet)
Expect(cachedSubnet).To(HaveLen(1))
lo.Contains(expectedSubnets, cachedSubnet[0])
lo.Contains(expectedSubnets.Subnets, cachedSubnet[0])
}
})
})
Expand Down

0 comments on commit 1dd3fbe

Please sign in to comment.