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

Cleaner: continue on error for PE and peering connections #1054

Merged
merged 5 commits into from
Aug 1, 2023
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
6 changes: 4 additions & 2 deletions .github/actions/clean-vpc/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"strings"
Expand All @@ -23,16 +24,17 @@ func deleteAzureVPCBySubstr(ctx context.Context, subID, resourceGroupName, subst
if err != nil {
return false, fmt.Errorf("error fetching vnets: %v", err)
}
var allErr error
for _, vnet := range vnets.Values() {
if vnet.Name != nil && strings.HasPrefix(*vnet.Name, substr) {
log.Printf("deleting vnet %s", *vnet.Name)
_, err = vnetClient.Delete(ctx, resourceGroupName, *vnet.Name)
if err != nil {
log.Printf("error deleting vnet: %v", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting vnet: %v", err))
ok = false
}
}
}

return ok, nil
return ok, allErr
}
6 changes: 4 additions & 2 deletions .github/actions/clean-vpc/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand All @@ -26,17 +27,18 @@ func deleteGCPVPCBySubstr(gcpProjectID, nameSubstr string) (bool, error) {
if err != nil {
return false, fmt.Errorf("failed to list networks: %s", err)
}
var allErr error
for _, network := range networks.Items {
if strings.HasPrefix(network.Name, nameSubstr) {
log.Printf("deleting network %s", network.Name)
_, err = networkService.Delete(gcpProjectID, network.Name).Do()
if err != nil {
log.Printf(fmt.Sprintf("failed to delete network %s: %s", network.Name, err))
allErr = errors.Join(allErr, fmt.Errorf("failed to delete network %s: %s", network.Name, err))
ok = false
}
}
}
return ok, nil
return ok, allErr
}

func setGCPCredentials() error {
Expand Down
12 changes: 10 additions & 2 deletions .github/actions/clean-vpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"os"
)
Expand All @@ -18,9 +20,11 @@ func main() {
if err != nil {
log.Fatal(err)
}
var allErr error
gcpOk, err := deleteGCPVPCBySubstr(googleProjectID, gcpVPCName)
if err != nil {
log.Fatal(err)
allErr = errors.Join(allErr, err)
log.Println(err)
}
if !gcpOk {
log.Println("Not all GCP VPC was deleted")
Expand All @@ -32,11 +36,15 @@ func main() {
ctx := context.Background()
azureOk, err := deleteAzureVPCBySubstr(ctx, subID, resourceGroupName, azureVPCName)
if err != nil {
log.Fatal(err)
allErr = errors.Join(allErr, err)
log.Println(err)
}
if !azureOk {
log.Println("Not all Azure VPC was deleted")
}
if allErr != nil {
fmt.Println("ERRORS: ", allErr)
}
if !azureOk || !gcpOk {
os.Exit(1)
}
Expand Down
19 changes: 13 additions & 6 deletions .github/actions/cleanup-pe/cleanall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"strings"
Expand All @@ -25,6 +26,7 @@ func cleanAllAWSPE(region string, subnets []string) error {

svc := ec2.New(awsSession)

var allErr error
for _, subnet := range subnets {
subnetInput := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{{
Expand Down Expand Up @@ -61,7 +63,8 @@ func cleanAllAWSPE(region string, subnets []string) error {

err = deleteAWSPEsByID(svc, endpointIDs)
if err != nil {
return err
allErr = errors.Join(allErr, err)
continue
}

log.Printf("deleted %d AWS PEs in region %s", len(endpointIDs), region)
Expand Down Expand Up @@ -132,6 +135,7 @@ func cleanAllGCPPE(ctx context.Context, projectID, vpc, region string, subnets [

networkURL := formNetworkURL(vpc, projectID)

var allErr error
for _, subnet := range subnets {
subnetURL := formSubnetURL(region, subnet, projectID)

Expand All @@ -147,7 +151,8 @@ func cleanAllGCPPE(ctx context.Context, projectID, vpc, region string, subnets [
if forwardRule.Network == networkURL {
_, err = computeService.ForwardingRules.Delete(projectID, region, forwardRule.Name).Do()
if err != nil {
return fmt.Errorf("error while deleting forwarding rule: %v", err)
allErr = errors.Join(allErr, fmt.Errorf("error while deleting forwarding rule: %v", err))
continue
}

counter++
Expand All @@ -160,11 +165,11 @@ func cleanAllGCPPE(ctx context.Context, projectID, vpc, region string, subnets [
time.Sleep(time.Second * 20) // need to wait for GCP to delete the forwarding rule
err = deleteGCPAddressBySubnet(computeService, projectID, region, subnetURL)
if err != nil {
return fmt.Errorf("error while deleting GCP address: %v", err)
allErr = errors.Join(allErr, fmt.Errorf("error while deleting GCP address: %v", err))
}
}

return nil
return allErr
}

func deleteGCPAddressBySubnet(service *compute.Service, projectID, region, subnetURL string) error {
Expand All @@ -175,19 +180,21 @@ func deleteGCPAddressBySubnet(service *compute.Service, projectID, region, subne
}

counter := 0
var allErr error
for _, address := range addressList.Items {
if address.Subnetwork == subnetURL {
_, err = service.Addresses.Delete(projectID, region, address.Name).Do()
if err != nil {
return fmt.Errorf("error while deleting address: %v", err)
allErr = errors.Join(allErr, fmt.Errorf("error while deleting address: %v", err))
continue
}
counter++
log.Printf("successfully deleted GCP address: %s. subnet: %s", address.Name, address.Subnetwork)
}
}

log.Printf("deleted %d GCP addresses", counter)
return nil
return allErr
}

func formNetworkURL(network, projectID string) string {
Expand Down
10 changes: 6 additions & 4 deletions .github/actions/cleanup-pe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -58,22 +59,23 @@ func CleanAllPE() error {
gcpRegion := "europe-west1"
subscriptionID := os.Getenv("AZURE_SUBSCRIPTION_ID")

var allErr error
err := cleanAllAzurePE(ctx, groupNameAzure, subscriptionID, []string{subnet1Name, subnet2Name})
if err != nil {
return fmt.Errorf("error while cleaning all azure pe: %v", err)
allErr = errors.Join(allErr, fmt.Errorf("error while cleaning all azure pe: %v", err))
}

for _, awsRegion := range awsRegions {
errClean := cleanAllAWSPE(awsRegion, []string{subnet1Name, subnet2Name})
if errClean != nil {
return fmt.Errorf("error cleaning all aws PE. region %s. error: %v", awsRegion, errClean)
allErr = errors.Join(allErr, fmt.Errorf("error cleaning all aws PE. region %s. error: %v", awsRegion, errClean))
}
}

err = cleanAllGCPPE(ctx, googleProjectID, gcpVPCName, gcpRegion, []string{subnet1Name, subnet2Name})
if err != nil {
return fmt.Errorf("error while cleaning all gcp pe: %v", err)
allErr = errors.Join(allErr, fmt.Errorf("error while cleaning all gcp pe: %v", err))
}

return nil
return allErr
}
6 changes: 4 additions & 2 deletions .github/actions/cleanup/deployment/advanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployment

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -21,11 +22,12 @@ func DeleteAllAdvancedClusters(ctx context.Context, client mongodbatlas.Advanced
if err != nil {
return fmt.Errorf("error getting advanced clusters: %w", err)
}
var allErr error
for _, cluster := range advancedClusters {
log.Printf("Deleting advanced cluster %s", cluster.Name)
if _, err = client.Delete(ctx, projectID, cluster.Name); err != nil {
return fmt.Errorf("error deleting advanced cluster: %w", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting advanced cluster: %w", err))
}
}
return nil
return allErr
}
6 changes: 4 additions & 2 deletions .github/actions/cleanup/deployment/datafederation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployment

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -14,11 +15,12 @@ func DeleteAllDataFederationInstances(ctx context.Context, client mongodbatlas.D
return fmt.Errorf("error listing datafederation instances: %w", err)
}

var allErr error
for _, df := range dfInstances {
log.Printf("Removing DataFederation instance: %s", df.Name)
if _, err := client.Delete(ctx, projectID, df.Name); err != nil {
return fmt.Errorf("unable to remove DataFederation instance: %w", err)
allErr = errors.Join(allErr, fmt.Errorf("unable to remove DataFederation instance: %w", err))
}
}
return nil
return allErr
}
6 changes: 4 additions & 2 deletions .github/actions/cleanup/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployment

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -13,13 +14,14 @@ func DeleteAllDeployments(ctx context.Context, client mongodbatlas.ClustersServi
if err != nil {
return fmt.Errorf("error getting deployments: %s", err)
}
var allErr error
for _, deployment := range deployments {
log.Printf("Deleting deployment %s", deployment.Name)
if _, err = client.Delete(ctx, projectID, deployment.Name); err != nil {
return fmt.Errorf("error deleting deployment: %s", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting deployment: %s", err))
}
}
return nil
return allErr
}

func GetAllDeployments(ctx context.Context, client mongodbatlas.ClustersService, projectID string) ([]mongodbatlas.Cluster, error) {
Expand Down
6 changes: 4 additions & 2 deletions .github/actions/cleanup/deployment/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deployment

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -13,13 +14,14 @@ func DeleteAllServerless(ctx context.Context, client mongodbatlas.ServerlessInst
if err != nil {
return fmt.Errorf("error getting serverless: %s", err)
}
var allErr error
for _, s := range serverless {
log.Printf("Deleting serverless %s", s.Name)
if _, err = client.Delete(ctx, projectID, s.Name); err != nil {
return fmt.Errorf("error deleting serverless: %s", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting serverless: %s", err))
}
}
return nil
return allErr
}

func GetAllServerless(ctx context.Context, client mongodbatlas.ServerlessInstancesService, projectID string) ([]*mongodbatlas.Cluster, error) {
Expand Down
1 change: 1 addition & 0 deletions .github/actions/cleanup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
log.Fatal(err)
}

log.Println("Total projects selected for deletion: ", len(projectList))
ctx := context.Background()
ok := project.DeleteProjects(ctx, client, projectList)
if !ok {
Expand Down
7 changes: 5 additions & 2 deletions .github/actions/cleanup/project/networkpeering.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package project

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -20,15 +21,17 @@ func DeleteAllNetworkPeers(ctx context.Context, peerService mongodbatlas.PeersSe
return fmt.Errorf("error getting network peers: %w", err)
}

var allErr error
for _, peer := range peerList {
_, err = peerService.Delete(ctx, projectID, peer.ID)
log.Printf("Deleting network peer %s", peer.ID)
if err != nil {
return fmt.Errorf("error deleting network peer: %w", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting network peer: %w", err))
continue
}
log.Printf("Deleted network peer %s", peer.ID)
}
return nil
return allErr
}

func GetAllNetworkPeers(ctx context.Context, peerService mongodbatlas.PeersService, projectID string) ([]mongodbatlas.Peer, error) {
Expand Down
9 changes: 5 additions & 4 deletions .github/actions/cleanup/project/pe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package project

import (
"context"
"errors"
"fmt"
"log"

Expand All @@ -21,22 +22,22 @@ func deleteAllPE(ctx context.Context, client mongodbatlas.PrivateEndpointsServic
}

func deletePrivateEndpointsFromAtlas(ctx context.Context, client mongodbatlas.PrivateEndpointsService, projectID string, listsToRemove []mongodbatlas.PrivateEndpointConnection) error {
var allErr error
for _, peService := range listsToRemove {
if firstInterfaceEndpointID(peService) != "" {
log.Printf("Deleting private endpoint %s", firstInterfaceEndpointID(peService))
if _, err := client.DeleteOnePrivateEndpoint(ctx, projectID, peService.ProviderName, peService.ID, firstInterfaceEndpointID(peService)); err != nil {
return fmt.Errorf("error deleting private endpoint interface: %s", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting private endpoint interface: %s", err))
}

continue
}

log.Printf("Deleting private endpoint %s", peService.EndpointServiceName)
if _, err := client.Delete(ctx, projectID, peService.ProviderName, peService.ID); err != nil {
return fmt.Errorf("error deleting private endpoint service: %s", err)
allErr = errors.Join(allErr, fmt.Errorf("error deleting private endpoint service: %s", err))
}
}
return nil
return allErr
}

func firstInterfaceEndpointID(connection mongodbatlas.PrivateEndpointConnection) string {
Expand Down
5 changes: 4 additions & 1 deletion .github/actions/cleanup/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ func DeleteProjects(ctx context.Context, client mongodbatlas.Client, projectList
log.Printf("error deleting DataFederation: %s", err)
}
err = deployment.DeleteAllAdvancedClusters(ctx, client.AdvancedClusters, project.ID)
if err != nil {
log.Printf("error deleting advanced clusters: %s", err)
}
_, err = client.Projects.Delete(context.Background(), project.ID)
if err != nil {
ok = false
log.Printf("error deleting project: %s", err)
} else {
log.Printf("Succesfully delete")
log.Printf("Project successufully deleted")
}
}
return ok
Expand Down
Loading
Loading