Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS EKS cleanup fix. #58

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ module c7n-helper
go 1.23

require (
github.com/aws/aws-sdk-go-v2 v1.32.0
github.com/aws/aws-sdk-go-v2 v1.32.4
github.com/aws/aws-sdk-go-v2/config v1.27.41
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.45.0
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.55.0
github.com/aws/aws-sdk-go-v2/service/ec2 v1.181.0
github.com/aws/aws-sdk-go-v2/service/eks v1.50.0
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.28.0
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.28.4
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.41.1
github.com/aws/smithy-go v1.22.0
github.com/hashicorp/go-multierror v1.1.1
github.com/lensesio/tableprinter v0.0.0-20201125135848-89e81fc956e7
Expand All @@ -23,8 +24,8 @@ require (
require (
github.com/aws/aws-sdk-go-v2/credentials v1.17.39 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.19 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.19 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.23 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.23 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.0 // indirect
Expand Down
54 changes: 10 additions & 44 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,71 +1,39 @@
github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=
github.com/aws/aws-sdk-go-v2 v1.31.0/go.mod h1:ztolYtaEUtdpf9Wftr31CJfLVjOnD/CVRkKOOYgF8hA=
github.com/aws/aws-sdk-go-v2 v1.32.0 h1:GuHp7GvMN74PXD5C97KT5D87UhIy4bQPkflQKbfkndg=
github.com/aws/aws-sdk-go-v2 v1.32.0/go.mod h1:2SK5n0a2karNTv5tbP1SjsX0uhttou00v/HpXKM1ZUo=
github.com/aws/aws-sdk-go-v2/config v1.27.39 h1:FCylu78eTGzW1ynHcongXK9YHtoXD5AiiUqq3YfJYjU=
github.com/aws/aws-sdk-go-v2/config v1.27.39/go.mod h1:wczj2hbyskP4LjMKBEZwPRO1shXY+GsQleab+ZXT2ik=
github.com/aws/aws-sdk-go-v2 v1.32.4 h1:S13INUiTxgrPueTmrm5DZ+MiAo99zYzHEFh1UNkOxNE=
github.com/aws/aws-sdk-go-v2 v1.32.4/go.mod h1:2SK5n0a2karNTv5tbP1SjsX0uhttou00v/HpXKM1ZUo=
github.com/aws/aws-sdk-go-v2/config v1.27.41 h1:esG3WpmEuNJ6F4kVFLumN8nCfA5VBav1KKb3JPx83O4=
github.com/aws/aws-sdk-go-v2/config v1.27.41/go.mod h1:haUg09ebP+ClvPjU3EB/xe0HF9PguO19PD2fdjM2X14=
github.com/aws/aws-sdk-go-v2/credentials v1.17.37 h1:G2aOH01yW8X373JK419THj5QVqu9vKEwxSEsGxihoW0=
github.com/aws/aws-sdk-go-v2/credentials v1.17.37/go.mod h1:0ecCjlb7htYCptRD45lXJ6aJDQac6D2NlKGpZqyTG6A=
github.com/aws/aws-sdk-go-v2/credentials v1.17.39 h1:tmVexAhoGqJxNE2oc4/SJqL+Jz1x1iCPt5ts9XcqZCU=
github.com/aws/aws-sdk-go-v2/credentials v1.17.39/go.mod h1:zgOdbDI9epE608PdboJ87CYvPIejAgFevazeJW6iauQ=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 h1:C/d03NAmh8C4BZXhuRNboF/DqhBkBCeDiJDcaqIT5pA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14/go.mod h1:7I0Ju7p9mCIdlrfS+JCgqcYD0VXz/N4yozsox+0o078=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.15 h1:kGjlNc2IXXcxPDcfMyCshNCjVgxUhC/vTJv7NvC9wKk=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.15/go.mod h1:rk/HmqPo+dX0Uv0Q1+4w3QKFdICEGSsTYz1hRWvH8UI=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18 h1:kYQ3H1u0ANr9KEKlGs/jTLrBFPo8P8NaH/w7A01NeeM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18/go.mod h1:r506HmK5JDUh9+Mw4CfGJGSSoqIiLCndAuqXuhbv67Y=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.19 h1:Q/k5wCeJkSWs+62kDfOillkNIJ5NqmE3iOfm48g/W8c=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.19/go.mod h1:Wns1C66VvtA2Bv/cUBuKZKQKdjo7EVMhp90aAa+8oTI=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18 h1:Z7IdFUONvTcvS7YuhtVxN99v2cCoHRXOS4mTr0B/pUc=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18/go.mod h1:DkKMmksZVVyat+Y+r1dEOgJEfUeA7UngIHWeKsi0yNc=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.19 h1:AYLE0lUfKvN6icFTR/p+NmD1amYKTbqHQ1Nm+jwE6BM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.19/go.mod h1:1giLakj64GjuH1NBzF/DXqly5DWHtMTaOzRZ53nFX0I=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.23 h1:A2w6m6Tmr+BNXjDsr7M90zkWjsu4JXHwrzPg235STs4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.23/go.mod h1:35EVp9wyeANdujZruvHiQUAo9E3vbhnIO1mTCAxMlY0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.23 h1:pgYW9FCabt2M25MoHYCfMrVY2ghiiBKYWUVXfwZs+sU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.23/go.mod h1:c48kLgzO19wAu3CPkDWC28JbaJ+hfQlsdl7I2+oqIbk=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 h1:VaRN3TlFdd6KxX1x3ILT5ynH6HvKgqdiXoTxAF4HQcQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1/go.mod h1:FbtygfRFze9usAadmnGJNc8KsP346kEe+y2/oyhGAGc=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.44.3 h1:uW81sdnq9hfg2hSnVqAFp+mMmu4Y86dU/bE9ET2LCIg=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.44.3/go.mod h1:Gmv7s//GGvs3nj9aqltFYnLStW8vDIwch0USkE67G4E=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.45.0 h1:wf5tH7iaOBAy5UkhwnNhx+7BdbedEqTHzGTd7toh5RQ=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.45.0/go.mod h1:TVp3het8D/Zqq7Wl8UfQe8OQtEUxZMu2Z4vw6/46ud0=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.3 h1:kVbtKOK6sNCqPsXE/7xN93pD090XETITuBNHrrPQsvk=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.54.3/go.mod h1:85xWVAzH8I6dCauQy7j1nt8CbSELPzGQj45chIZ/qMA=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.55.0 h1:Pf3hi7g1jDoD31qetA/HuY94jga2pH4W5W3+5uVaxKY=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.55.0/go.mod h1:WEfYjobS0jTq12V8UgB+wfHcq/tTV4mZXIc+a8OjL2A=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.179.2 h1:rGBv2N0zWvNTKnxOfbBH4mNM8WMdDNkaxdqtz152G40=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.179.2/go.mod h1:W6sNzs5T4VpZn1Vy+FMKw8s24vt5k6zPJXcNOK0asBo=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.181.0 h1:YzSOMQYRZQKuLz/bD6illIGwJfa1WFfeFAZM5Zr5LB8=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.181.0/go.mod h1:CudaKF0Yu5+ZfKMiiPdtJ/kOOBty7CIEJUhESP52e9M=
github.com/aws/aws-sdk-go-v2/service/eks v1.49.3 h1:4Aq01bwq1RnyMLAgx/6kB8cqvfLlQet5cWY3MVhlsqU=
github.com/aws/aws-sdk-go-v2/service/eks v1.49.3/go.mod h1:QUjwO93Ri00egMAeWw75dviZBM5pECLx0KNeNaBtTIM=
github.com/aws/aws-sdk-go-v2/service/eks v1.50.0 h1:eL4AEDwVx29t+B7dkcuL/3W+RQKR64PPbfQVQTs8FEs=
github.com/aws/aws-sdk-go-v2/service/eks v1.50.0/go.mod h1:0C9DxOpj1d8GioesPAKXMob9X2lyFepeL6C5z9oA4HM=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.27.3 h1:6GwGMmJD+KxJ11vqvKX2jtcyfvurkp33SVp6OxYrIJg=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.27.3/go.mod h1:A6rhNF3Qz6pn97WX3DcIK7g6ODOCYR7t698ptify9eM=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.28.0 h1:7DILBd1OhZDxLj1AmxE694olYcsHa1QFqXh43MnhvAU=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.28.0/go.mod h1:eFAY1hh0RSoo6QM2QFgbFPIiE6b/W7VeoN/DBoOOkAg=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5 h1:QFASJGfT8wMXtuP3D5CRmMjARHv9ZmzFUMJznHDOY3w=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5/go.mod h1:QdZ3OmoIjSX+8D1OPAzPxDfjXASbBMDsz9qvtyIhtik=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.28.4 h1:Rdrd35iVHabYS45yIrm0NVYpq/hNhdAhB2FiXYCOZyw=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.28.4/go.mod h1:OBFqCwiJoYtdhDdH0S7bKMk7PbM6JYsD7psjAVZ+tVY=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.41.1 h1:EfkdYBfEgJJREyk0fm7C9OrcS+cq9KK7lYvabo4nEMM=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.41.1/go.mod h1:ffdKles8aLKN0GJkZ2LdFKFD1wGs6ZFuu/+Hftv4Xu0=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 h1:TToQNkvGguu209puTojY/ozlqy2d/SFNcoLIqTFi42g=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0/go.mod h1:0jp+ltwkf+SwG2fm/PKo8t4y8pJSgOCO4D8Lz3k0aHQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20 h1:Xbwbmk44URTiHNx6PNo0ujDE6ERlsCKJD3u1zfnzAPg=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20/go.mod h1:oAfOFzUB14ltPZj1rWwRc3d/6OgD76R8KlvU3EqM9Fg=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.0 h1:AdbiDUgQZmM28rDIZbiSwFxz8+3B94aOXxzs6oH+EA0=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.0/go.mod h1:uV476Bd80tiDTX4X2redMtagQUg65aU/gzPojSJ4kSI=
github.com/aws/aws-sdk-go-v2/service/sso v1.23.3 h1:rs4JCczF805+FDv2tRhZ1NU0RB2H6ryAvsWPanAr72Y=
github.com/aws/aws-sdk-go-v2/service/sso v1.23.3/go.mod h1:XRlMvmad0ZNL+75C5FYdMvbbLkd6qiqz6foR1nA1PXY=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.0 h1:71FvP6XFj53NK+YiAEGVzeiccLVeFnHOCvMig0zOHsE=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.0/go.mod h1:UVJqtKXSd9YppRKgdBIkyv7qgbSGv5DchM3yX0BN2mU=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.3 h1:S7EPdMVZod8BGKQQPTBK+FcX9g7bKR7c4+HxWqHP7Vg=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.3/go.mod h1:FnvDM4sfa+isJ3kDXIzAB9GAwVSzFzSy97uZ3IsHo4E=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.0 h1:Uco4o19bi3AmBapImNzuMk+rfzlui52BDyVK1UfJeRA=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.0/go.mod h1:+HLFhCpnG08hBee8bUdfd1mBK+rFKPt4O5igR9lXDfk=
github.com/aws/aws-sdk-go-v2/service/sts v1.31.3 h1:VzudTFrDCIDakXtemR7l6Qzt2+JYsVqo2MxBPt5k8T8=
github.com/aws/aws-sdk-go-v2/service/sts v1.31.3/go.mod h1:yMWe0F+XG0DkRZK5ODZhG7BEFYhLXi2dqGsv6tX0cgI=
github.com/aws/aws-sdk-go-v2/service/sts v1.32.0 h1:GiQUjZM2KUZX68o/LpZ1xqxYMuvoxpRrOwYARYog3vc=
github.com/aws/aws-sdk-go-v2/service/sts v1.32.0/go.mod h1:dKnu7M4MAS2SDlng1ytxd03H+y0LoUfEQ5E2VaaSw/4=
github.com/aws/smithy-go v1.21.0 h1:H7L8dtDRk0P1Qm6y0ji7MCYMQObJ5R9CRpyPhRUkLYA=
github.com/aws/smithy-go v1.21.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM=
github.com/aws/smithy-go v1.22.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -101,8 +69,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/slack-go/slack v0.12.1 h1:X97b9g2hnITDtNsNe5GkGx6O2/Sz/uC20ejRZN6QxOw=
github.com/slack-go/slack v0.12.1/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/slack-go/slack v0.14.0 h1:6c0UTfbRnvRssZUsZ2qe0Iu07VAMPjRqOa6oX8ewF4k=
github.com/slack-go/slack v0.14.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
Expand Down
50 changes: 32 additions & 18 deletions pkg/aws/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func DeleteResources(ctx context.Context, accounts []dto.Account, tries int, ret
break
}
}
if err == nil {
logger.Info("deleting cluster")
err = deleteEKS(ctx, cls.EKS, clusterName)
}
return err
})
}
Expand All @@ -55,10 +51,16 @@ func DeleteResources(ctx context.Context, accounts []dto.Account, tries int, ret
func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName string) error {
logger := log.FromContext(ctx)
var errs error

logger.Info("deleting cluster")
if err := deleteEKS(ctx, clients.EKS, clusterName); err != nil {
errs = multierr.Append(errs, err)
}

logger.Info("listing vpc peering connections")
connections, err := listVpcPeeringConnections(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting vpc peering connections: %d", len(connections))
if err := deleteVpcPeeringConnections(ctx, clients.EC2, vpcID, connections); err != nil {
Expand All @@ -68,17 +70,29 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing load balancers")
balancers, err := listLoadBalancers(ctx, clients.ELB, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting load balancers: %d", len(balancers))

if err := deleteLoadBalancers(ctx, clients.ELB, balancers); err != nil {
errs = multierr.Append(errs, err)
}

logger.Info("listing load balancers V2")
balancersV2, err := listLoadBalancersV2(ctx, clients.ELBv2, vpcID)
if err != nil {
return multierr.Append(errs, err)
}
logger.Infof("deleting load balancers V2: %d", len(balancersV2))

if err := deleteLoadBalancersV2(ctx, clients.ELBv2, balancersV2); err != nil {
errs = multierr.Append(errs, err)
}

logger.Info("listing autoscaling groups")
scalingGroups, err := listAutoScalingGroups(ctx, clients.ASG, clusterName)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting autoscaling groups: %d", len(scalingGroups))
if err := deleteAutoScalingGroups(ctx, clients.ASG, clients.EC2, scalingGroups); err != nil {
Expand All @@ -88,7 +102,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing reservation")
reservations, err := listReservations(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting reservation: %d", len(reservations))
if err := terminateInstancesInReservations(ctx, clients.EC2, reservations); err != nil {
Expand All @@ -98,7 +112,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing network acl")
acls, err := listNonDefaultNetworkAcls(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting network acl: %d", len(acls))
if err := deleteNetworkAcls(ctx, clients.EC2, vpcID, acls); err != nil {
Expand All @@ -108,7 +122,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing elastic ips")
ips, err := listElasticIps(ctx, clients.EC2, clusterName)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting elastic ips: %d", len(ips))
if err := releaseElasticIps(ctx, clients.EC2, ips); err != nil {
Expand All @@ -118,7 +132,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing nat gateways")
nats, err := listNatGateways(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting nat gateways: %d", len(nats))
if err := deleteNatGateways(ctx, clients.EC2, nats); err != nil {
Expand All @@ -128,7 +142,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing internet gateways")
gws, err := listInternetGateways(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting internet gateways: %d", len(gws))
if err := deleteInternetGateways(ctx, clients.EC2, vpcID, gws); err != nil {
Expand All @@ -138,7 +152,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing network interfaces")
interfaces, err := listNetworkInterfaces(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting network interfaces: %d", len(interfaces))
if err := deleteNetworkInterfaces(ctx, clients.EC2, interfaces); err != nil {
Expand All @@ -148,7 +162,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing subnets")
subnets, err := listSubnets(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting subnets: %d", len(subnets))
if err := deleteSubnets(ctx, clients.EC2, vpcID, subnets); err != nil {
Expand All @@ -158,7 +172,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing security groups")
secGroups, err := listNonDefaultSecurityGroups(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting security groups: %d", len(secGroups))
if err := deleteSecurityGroups(ctx, clients.EC2, vpcID, secGroups); err != nil {
Expand All @@ -168,7 +182,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing vpn gateways")
vpns, err := listVpnGateways(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting vpn gateways: %d", len(vpns))
if err := deleteVpnGateways(ctx, clients.EC2, vpcID, vpns); err != nil {
Expand All @@ -178,7 +192,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s
logger.Info("listing route tables")
routes, err := listRouteTables(ctx, clients.EC2, vpcID)
if err != nil {
return err
return multierr.Append(errs, err)
}
logger.Infof("deleting route tables: %d", len(routes))
if err := deleteRouteTables(ctx, clients.EC2, vpcID, routes); err != nil {
Expand All @@ -197,7 +211,7 @@ func deleteVpcAndEks(ctx context.Context, clients *clients, vpcID, clusterName s

logger.Info("deleting cloud formation")
if _, err := listCloudFormationStacks(ctx, clients.CF, clusterName); err != nil {
return err
return multierr.Append(errs, err)
}
if err := deleteCloudFormation(ctx, clients.CF, clusterName); err != nil {
errs = multierr.Append(errs, err)
Expand Down
23 changes: 13 additions & 10 deletions pkg/aws/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
)

type clients struct {
ASG *autoscaling.Client
EC2 *ec2.Client
ELB *elasticloadbalancing.Client
EKS *eks.Client
CF *cloudformation.Client
ASG *autoscaling.Client
EC2 *ec2.Client
ELB *elasticloadbalancing.Client
ELBv2 *elasticloadbalancingv2.Client
EKS *eks.Client
CF *cloudformation.Client
}

var clientsMap = map[string]*clients{}
Expand All @@ -35,11 +37,12 @@ func InitClientsMap(ctx context.Context, accounts []dto.Account) error {
return err
}
clientsMap[key] = &clients{
ASG: autoscaling.NewFromConfig(cfg),
CF: cloudformation.NewFromConfig(cfg),
EC2: ec2.NewFromConfig(cfg),
ELB: elasticloadbalancing.NewFromConfig(cfg),
EKS: eks.NewFromConfig(cfg),
ASG: autoscaling.NewFromConfig(cfg),
CF: cloudformation.NewFromConfig(cfg),
EC2: ec2.NewFromConfig(cfg),
ELB: elasticloadbalancing.NewFromConfig(cfg),
ELBv2: elasticloadbalancingv2.NewFromConfig(cfg),
EKS: eks.NewFromConfig(cfg),
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/aws/loadbalancerv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aws

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"go.uber.org/multierr"
)

func deleteLoadBalancersV2(ctx context.Context, client *elasticloadbalancingv2.Client, loadBalancers []types.LoadBalancer) (errs error) {
for _, loadBalancer := range loadBalancers {
if loadBalancer.LoadBalancerArn == nil {
continue
}
_, err := client.DeleteLoadBalancer(ctx, &elasticloadbalancingv2.DeleteLoadBalancerInput{
LoadBalancerArn: loadBalancer.LoadBalancerArn,
})
errs = multierr.Append(errs, err)
}
return
}

func listLoadBalancersV2(ctx context.Context, client *elasticloadbalancingv2.Client, vpcId string) ([]types.LoadBalancer, error) {
input := elasticloadbalancingv2.DescribeLoadBalancersInput{}
var loadBalancers []types.LoadBalancer
for {
output, err := client.DescribeLoadBalancers(ctx, &input)
if err != nil {
return nil, err
}
for _, loadBalancer := range output.LoadBalancers {
if loadBalancer.VpcId == nil || *loadBalancer.VpcId != vpcId {
continue
}
fmt.Printf("\nfound LB: %s\n", *loadBalancer.LoadBalancerName)
loadBalancers = append(loadBalancers, loadBalancer)
}
if output.NextMarker == nil {
return loadBalancers, nil
}
input.Marker = output.NextMarker
}
}