Skip to content

Commit

Permalink
refactor elbv2 (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 authored Aug 2, 2023
1 parent 1b0e359 commit c60a2fc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 234 deletions.
2 changes: 1 addition & 1 deletion aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func GetAllResources(targetRegions []string, excludeAfter time.Time, resourceTyp
}
if IsNukeable(loadBalancersV2.ResourceName(), resourceTypes) {
start := time.Now()
elbv2Arns, err := getAllElbv2Instances(cloudNukeSession, region, excludeAfter, configObj)
elbv2Arns, err := loadBalancersV2.getAll(configObj)
if err != nil {
ge := report.GeneralError{
Error: err,
Expand Down
46 changes: 46 additions & 0 deletions aws/eks_utils_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"github.com/gruntwork-io/go-commons/collections"
"testing"
"time"

Expand Down Expand Up @@ -298,3 +299,48 @@ const eksNodeGroupAssumeRolePolicy = `{
}
]
}`

func getSubnetsInDifferentAZs(t *testing.T, session *session.Session) (*ec2.Subnet, *ec2.Subnet) {
subnetOutput, err := ec2.New(session).DescribeSubnets(&ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: awsgo.String("default-for-az"),
Values: []*string{awsgo.String("true")},
},
},
})
require.NoError(t, err)
require.True(t, len(subnetOutput.Subnets) >= 2)

subnet1Idx := -1
for idx, subnet := range subnetOutput.Subnets {
if !collections.ListContainsElement(AvailabilityZoneBlackList, awsgo.StringValue(subnet.AvailabilityZone)) {
subnet1Idx = idx
break
}
}
if subnet1Idx == -1 {
require.Fail(t, "Unable to find a subnet in an availability zone that is not blacklisted.")
}
subnet1 := subnetOutput.Subnets[subnet1Idx]
az1 := awsgo.StringValue(subnet1.AvailabilityZone)
subnet1Id := awsgo.StringValue(subnet1.SubnetId)
subnet1VpcId := awsgo.StringValue(subnet1.VpcId)

for i := subnet1Idx + 1; i < len(subnetOutput.Subnets); i++ {
subnet2 := subnetOutput.Subnets[i]
az2 := awsgo.StringValue(subnet2.AvailabilityZone)
if collections.ListContainsElement(AvailabilityZoneBlackList, az2) {
// Skip because subnet is in a blacklisted AZ
continue
}
subnet2Id := awsgo.StringValue(subnet2.SubnetId)
subnet2VpcId := awsgo.StringValue(subnet2.VpcId)
if az1 != az2 && subnet1Id != subnet2Id && subnet1VpcId == subnet2VpcId {
return subnet1, subnet2
}
}

require.Fail(t, "Unable to find 2 subnets in different Availability Zones")
return nil, nil
}
50 changes: 15 additions & 35 deletions aws/elbv2.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,52 @@
package aws

import (
"github.com/gruntwork-io/cloud-nuke/telemetry"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
"time"

"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/telemetry"
"github.com/gruntwork-io/go-commons/errors"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
)

// Returns a formatted string of ELBv2 Arns
func getAllElbv2Instances(session *session.Session, region string, excludeAfter time.Time, configObj config.Config) ([]*string, error) {
svc := elbv2.New(session)
result, err := svc.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{})
func (balancer LoadBalancersV2) getAll(configObj config.Config) ([]*string, error) {
result, err := balancer.Client.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}

var arns []*string
for _, balancer := range result.LoadBalancers {
if shouldIncludeELBv2(balancer, excludeAfter, configObj) {
if configObj.ELBv2.ShouldInclude(config.ResourceValue{
Name: balancer.LoadBalancerName,
Time: balancer.CreatedTime,
}) {
arns = append(arns, balancer.LoadBalancerArn)
}
}

return arns, nil
}

func shouldIncludeELBv2(balancer *elbv2.LoadBalancer, excludeAfter time.Time, configObj config.Config) bool {
if balancer == nil {
return false
}

if balancer.CreatedTime != nil && excludeAfter.Before(*balancer.CreatedTime) {
return false
}

return config.ShouldInclude(
awsgo.StringValue(balancer.LoadBalancerName),
configObj.ELBv2.IncludeRule.NamesRegExp,
configObj.ELBv2.ExcludeRule.NamesRegExp,
)
}

// Deletes all Elastic Load Balancers
func nukeAllElbv2Instances(session *session.Session, arns []*string) error {
svc := elbv2.New(session)

func (balancer LoadBalancersV2) nukeAll(arns []*string) error {
if len(arns) == 0 {
logging.Logger.Debugf("No V2 Elastic Load Balancers to nuke in region %s", *session.Config.Region)
logging.Logger.Debugf("No V2 Elastic Load Balancers to nuke in region %s", balancer.Region)
return nil
}

logging.Logger.Debugf("Deleting all V2 Elastic Load Balancers in region %s", *session.Config.Region)
logging.Logger.Debugf("Deleting all V2 Elastic Load Balancers in region %s", balancer.Region)
var deletedArns []*string

for _, arn := range arns {
params := &elbv2.DeleteLoadBalancerInput{
LoadBalancerArn: arn,
}

_, err := svc.DeleteLoadBalancer(params)
_, err := balancer.Client.DeleteLoadBalancer(params)

// Record status of this resource
e := report.Entry{
Expand All @@ -81,7 +61,7 @@ func nukeAllElbv2Instances(session *session.Session, arns []*string) error {
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking Load Balancer V2",
}, map[string]interface{}{
"region": *session.Config.Region,
"region": balancer.Region,
})
} else {
deletedArns = append(deletedArns, arn)
Expand All @@ -90,7 +70,7 @@ func nukeAllElbv2Instances(session *session.Session, arns []*string) error {
}

if len(deletedArns) > 0 {
err := svc.WaitUntilLoadBalancersDeleted(&elbv2.DescribeLoadBalancersInput{
err := balancer.Client.WaitUntilLoadBalancersDeleted(&elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: deletedArns,
})
if err != nil {
Expand All @@ -99,6 +79,6 @@ func nukeAllElbv2Instances(session *session.Session, arns []*string) error {
}
}

logging.Logger.Debugf("[OK] %d V2 Elastic Load Balancer(s) deleted in %s", len(deletedArns), *session.Config.Region)
logging.Logger.Debugf("[OK] %d V2 Elastic Load Balancer(s) deleted in %s", len(deletedArns), balancer.Region)
return nil
}
Loading

0 comments on commit c60a2fc

Please sign in to comment.