Skip to content

Commit

Permalink
OCM-10403 | test: make vpc total clean support instances and lb leak …
Browse files Browse the repository at this point in the history
…deletion
  • Loading branch information
xueli181114 committed Aug 14, 2024
1 parent 2080f91 commit 4191990
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 31 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.21

require (
github.com/apparentlymart/go-cidr v1.1.0
github.com/aws/aws-sdk-go-v2 v1.30.0
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.9
github.com/aws/aws-sdk-go-v2/credentials v1.17.9
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.48.0
github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.24.3
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0
github.com/aws/aws-sdk-go-v2/service/iam v1.27.1
github.com/aws/aws-sdk-go-v2/service/kms v1.30.0
github.com/aws/aws-sdk-go-v2/service/ram v1.26.1
Expand All @@ -28,15 +28,15 @@ require (
require github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect

require (
github.com/aws/smithy-go v1.20.2
github.com/aws/smithy-go v1.20.3
github.com/kr/pretty v0.1.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)

require (
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.35.1
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/aws/aws-sdk-go-v2 v1.30.0 h1:6qAwtzlfcTtcL8NHtbDQAqgM5s6NDipQTkPxyH/6kAA=
github.com/aws/aws-sdk-go-v2 v1.30.0/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg=
github.com/aws/aws-sdk-go-v2/config v1.27.9 h1:gRx/NwpNEFSk+yQlgmk1bmxxvQ5TyJ76CWXs9XScTqg=
Expand All @@ -10,10 +10,10 @@ github.com/aws/aws-sdk-go-v2/credentials v1.17.9 h1:N8s0/7yW+h8qR8WaRlPQeJ6czVMN
github.com/aws/aws-sdk-go-v2/credentials v1.17.9/go.mod h1:446YhIdmSV0Jf/SLafGZalQo+xr2iw7/fzXGDPTU1yQ=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 h1:af5YzcLf80tv4Em4jWVD75lpnOHSBkPUZxZfGkrI3HI=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0/go.mod h1:nQ3how7DMnFMWiU1SpECohgC82fpn4cKZ875NDMmwtA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 h1:SJ04WXGTwnHlWIODtC5kJzKbeuHt+OUNOgKg7nfnUGw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12/go.mod h1:FkpvXhA92gb3GE9LD6Og0pHHycTxW7xGpnEh5E7Opwo=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 h1:hb5KgeYfObi5MHkSSZMEudnIvX30iB+E21evI4r6BnQ=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12/go.mod h1:CroKe/eWJdyfy9Vx4rljP5wTUjNJfb+fPz1uMYUhEGM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 h1:SoNJ4RlFEQEbtDcCEt+QG56MY4fm4W8rYirAmq+/DdU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15/go.mod h1:U9ke74k1n2bf+RIgoX1SXFed1HLs51OgUSs+Ph0KJP8=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 h1:C6WHdGnTDIYETAm5iErQUiVNsclNx9qbJVPIt03B6bI=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15/go.mod h1:ZQLZqhcu+JhSrA9/NXRm8SkDvsycE+JkV3WGY41e+IM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.48.0 h1:uMlYsoHdd2Gr9sDGq2ieUR5jVu7F5AqPYz6UBJmdRhY=
Expand All @@ -22,8 +22,8 @@ github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.35.1 h1:suWu59CRsDNhw2YXP
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.35.1/go.mod h1:tZiRxrv5yBRgZ9Z4OOOxwscAZRFk5DgYhEcjX1QpvgI=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0 h1:ltCQObuImVYmIrMX65ikB9W83MEun3Ry2Sk11ecZ8Xw=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.152.0/go.mod h1:TeZ9dVQzGaLG+SBIgdLIDbJ6WmfFvksLeG3EHGnNfZM=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.24.3 h1:pjgSJEvgJzv+e0frrqspeYdHz2JSW1KAGMXRe1FuQ1M=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.24.3/go.mod h1:dhRVzB/bmggoMEBhYXKZrTE+jqN34O4+webZSjGi12c=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0 h1:8rDRtPOu3ax8jEctw7G926JQlnFdhZZA4KJzQ+4ks3Q=
github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.34.0/go.mod h1:L5bVuO4PeXuDuMYZfL3IW69E6mz6PDCYpp6IKDlcLMA=
github.com/aws/aws-sdk-go-v2/service/iam v1.27.1 h1:rPkEOnwPOVop34lpAlA4Dv6x67Ys3moXkPDvBfjgSSo=
github.com/aws/aws-sdk-go-v2/service/iam v1.27.1/go.mod h1:qdQ8NUrhmXE80S54w+LrtHUY+1Fp7cQSRZbJUZKrAcU=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 h1:EyBZibRTVAs6ECHZOw5/wlylS9OcTzwyjeQMudmREjE=
Expand All @@ -42,8 +42,8 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3 h1:uLq0BKatTmDzWa/Nu4WO0M1A
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3/go.mod h1:b+qdhjnxj8GSR6t5YfphOffeoQSQ1KmpoVVuBn+PWxs=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5 h1:J/PpTf/hllOjx8Xu9DMflff3FajfLxqM5+tepvVXmxg=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5/go.mod h1:0ih0Z83YDH/QeQ6Ori2yGE2XvWYv/Xm+cZc01LC6oK0=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
2 changes: 1 addition & 1 deletion pkg/aws/aws_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/openshift-online/ocm-common/pkg/log"

elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/route53"

CON "github.com/openshift-online/ocm-common/pkg/aws/consts"
Expand Down
21 changes: 9 additions & 12 deletions pkg/aws/aws_client/elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ package aws_client
import (
"context"

elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"

elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"github.com/openshift-online/ocm-common/pkg/log"
)

func (client *AWSClient) DescribeLoadBalancers(vpcID string) ([]elbtypes.LoadBalancerDescription, error) {
func (client *AWSClient) DescribeLoadBalancers(vpcID string) ([]elbtypes.LoadBalancer, error) {

listenedELB := []elbtypes.LoadBalancerDescription{}
listenedELB := []elbtypes.LoadBalancer{}
input := &elb.DescribeLoadBalancersInput{}
resp, err := client.ElbClient.DescribeLoadBalancers(context.TODO(), input)
if err != nil {
return nil, err
}
// for _, lb := range resp.LoadBalancers {
for _, lb := range resp.LoadBalancerDescriptions {

// if *lb.VpcId == vpcID {
if *lb.VPCId == vpcID {
for _, lb := range resp.LoadBalancers {
if *lb.VpcId == vpcID {
log.LogInfo("Got load balancer %s", *lb.LoadBalancerName)
listenedELB = append(listenedELB, lb)
}
Expand All @@ -30,12 +27,12 @@ func (client *AWSClient) DescribeLoadBalancers(vpcID string) ([]elbtypes.LoadBal
return listenedELB, err
}

func (client *AWSClient) DeleteELB(ELB elbtypes.LoadBalancerDescription) error {
log.LogInfo("Goint to delete ELB %s", *ELB.LoadBalancerName)
func (client *AWSClient) DeleteELB(ELB elbtypes.LoadBalancer) error {
log.LogInfo("Going to delete ELB %s", *ELB.LoadBalancerName)

deleteELBInput := &elb.DeleteLoadBalancerInput{
// LoadBalancerArn: ELB.LoadBalancerArn,
LoadBalancerName: ELB.LoadBalancerName,
LoadBalancerArn: ELB.LoadBalancerArn,
}
_, err := client.ElbClient.DeleteLoadBalancer(context.TODO(), deleteELBInput)
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/test/vpc_client/elb.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package vpc_client

import "time"

func (vpc *VPC) DeleteVPCELBs() error {
elbs, err := vpc.AWSClient.DescribeLoadBalancers(vpc.VpcID)
if err != nil {
Expand All @@ -12,5 +14,8 @@ func (vpc *VPC) DeleteVPCELBs() error {
return err
}
}
if len(elbs) != 0 {
time.Sleep(time.Minute) // sleep 1 minute to wait for the LBs totally deleted
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/test/vpc_client/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func (vpc *VPC) TerminateVPCInstances(nonClusterOnly bool) error {
keyPairNames := []string{}
for _, inst := range insts {
needTermination = append(needTermination, *inst.InstanceId)
keyPairNames = append(keyPairNames, *inst.KeyName)
if inst.KeyName != nil {
keyPairNames = append(keyPairNames, *inst.KeyName)
}
}
err = vpc.AWSClient.TerminateInstances(needTermination, true, 20)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/test/vpc_client/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func (vpc *VPC) DeleteVPCSecurityGroups(customizedOnly bool) error {
if customizedOnly {
for _, sg := range securityGroups {
for _, tag := range sg.Tags {
if *tag.Key == "Name" && (*tag.Value == con.ProxySecurityGroupName || *tag.Value == con.AdditionalSecurityGroupName) {
if *tag.Key == "Name" && (*tag.Value == con.ProxySecurityGroupName ||
*tag.Value == con.AdditionalSecurityGroupName) {
needCleanGroups = append(needCleanGroups, sg)
}
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/test/vpc_client/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (vpc *VPC) DeleteVPCChain(totalClean ...bool) error {
return fmt.Errorf("got empty vpc ID to clean. Make sure you loaded it from AWS")
}
log.LogInfo("Going to delete the vpc and follow resources by ID: %s", vpcID)
log.LogInfo("Going to terminate proxy instances if existing")
log.LogInfo("Going to terminate proxy instances if exists")
err := vpc.TerminateVPCInstances(true)
if err != nil {
log.LogError("Delete vpc instances meets error: %s", err.Error())
Expand Down Expand Up @@ -105,11 +105,22 @@ func (vpc *VPC) DeleteVPCChain(totalClean ...bool) error {
}
if len(totalClean) == 1 && totalClean[0] {
log.LogInfo("Got total clean set, going to delete other possible resource leak")
// Delete leak instances
log.LogInfo("Going to terminate the leak instances if exist")
err = vpc.TerminateVPCInstances(false)
if err != nil {
log.LogError("Terminate vpc instances meets error: %s", err.Error())
return err
}

// Delete leak ELBs
err = vpc.DeleteVPCELBs()
if err != nil {
log.LogError("Delete vpc load balancers meets error: %s", err.Error())
return err
}

// Delete leak security groups
err = vpc.DeleteVPCSecurityGroups(false)
if err != nil {
log.LogError("Delete vpc security groups meets error: %s", err.Error())
Expand Down

0 comments on commit 4191990

Please sign in to comment.