Skip to content

Commit

Permalink
OCM-12269 | ci: fix with reducing the number of sg in id:73068
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwang-RH committed Nov 5, 2024
1 parent e54d44d commit 861e2f3
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/hcp_machine_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ var _ = Describe("HCP MachinePool", ci.Day2, ci.FeatureMachinepool, func() {
Replicas: helper.IntPointer(replicas),
MachineType: helper.StringPointer(machineType),
Name: helper.StringPointer(name),
AdditionalSecurityGroups: helper.StringSlicePointer(output.SGIDs),
AdditionalSecurityGroups: helper.StringSlicePointer(sgIDs),
AutoscalingEnabled: helper.BoolPointer(false),
AutoRepair: helper.BoolPointer(false),
SubnetID: helper.StringPointer(vpcOutput.PrivateSubnets[0]),
Expand Down
91 changes: 91 additions & 0 deletions tests/utils/helper/vpc_dependency_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package helper

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
. "github.com/terraform-redhat/terraform-provider-rhcs/tests/utils/log"
)

func DescribeInstances(vpcID string, region string) ([]string, error) {
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
ec := ec2.New(sess)
var instanceIDs []string
instances, err := ec.DescribeInstances(&ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcID)},
},
{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("pending"), aws.String("running"), aws.String("shutting-down"), aws.String("stopped"), aws.String("stopping")},
},
},
})
if err != nil {
return instanceIDs, err
}

for _, reservation := range instances.Reservations {
for _, instance := range reservation.Instances {
instanceIDs = append(instanceIDs, *instance.InstanceId)
Logger.Warnf("Instance ID(Reservation Id %s) %s may leak!", *reservation.ReservationId, *instance.InstanceId)
}
}
return instanceIDs, nil
}

func DescribeNatGateways(vpcID string, region string) ([]string, error) {
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
ec := ec2.New(sess)
var natGatewayIDs []string
natGateways, err := ec.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{
Filter: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcID)},
},
{
Name: aws.String("state"),
Values: []*string{aws.String("available"), aws.String("deleting"), aws.String("pending")},
},
},
})
if err != nil {
return natGatewayIDs, err
}
for _, ngw := range natGateways.NatGateways {
natGatewayIDs = append(natGatewayIDs, *ngw.NatGatewayId)
Logger.Warnf("NatGateway Id %s may leak!", *ngw.NatGatewayId)
}
return natGatewayIDs, nil
}

func DescribeNetworkInterfaces(vpcID string, region string) ([]string, error) {
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
ec := ec2.New(sess)
var networkInterfaceIDs []string
networkInterfaces, err := ec.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcID)},
},
},
})
if err != nil {
return networkInterfaceIDs, err
}
for _, ni := range networkInterfaces.NetworkInterfaces {
networkInterfaceIDs = append(networkInterfaceIDs, *ni.NetworkInterfaceId)
Logger.Warnf("NetworkInterface Id %s may leak!", *ni.NetworkInterfaceId)
}
return networkInterfaceIDs, nil
}
35 changes: 34 additions & 1 deletion tests/utils/profilehandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,8 +1056,41 @@ func (ctx *profileContext) DestroyRHCSClusterResources(token string) error {
} else {
_, err = vpcService.Destroy()
if err != nil {
errs = append(errs, err)
if ctx.profile.PrivateLink {
if strings.Contains(err.Error(), "DependencyViolation") {
Logger.Warn("DependencyViolation, waiting for 5 minutes before retrying...")
time.Sleep(5 * time.Minute)
_, err = vpcService.Destroy()
if err != nil {
errs = append(errs, err)
if strings.Contains(err.Error(), "DependencyViolation") {
vpcOutPut, err := vpcService.Output()
if err != nil {
Logger.Errorf("It met error when try to get the vpc output: %s", err.Error())
} else {
instances, err := helper.DescribeInstances(vpcOutPut.VPCID, ctx.profile.Region)
if err != nil {
Logger.Errorf("It met error when try to get the instances: %s", err.Error())
}
natGateways, err := helper.DescribeNatGateways(vpcOutPut.VPCID, ctx.profile.Region)
if err != nil {
Logger.Errorf("It met error when try to get natGateways: %s", err.Error())
}
networkInterfaces, err := helper.DescribeNetworkInterfaces(vpcOutPut.VPCID, ctx.profile.Region)
if err != nil {
Logger.Errorf("It met error when try to get networkInterfaces: %s", err.Error())
}
Logger.Warnf("Instance %v and natGateways %v and networkInterfaces %v may leak!", instances, natGateways, networkInterfaces)
}
}

}
}
} else {
errs = append(errs, err)
}
}

}
}
if ctx.profile.STS {
Expand Down

0 comments on commit 861e2f3

Please sign in to comment.