Skip to content

Commit 3925504

Browse files
authored
CLOUDP-141796: Add network peer cloud cleaner (#750)
* Add cloud vpc cleaner * Fix cleaner * Update default cleaner input
1 parent 71becb7 commit 3925504

File tree

12 files changed

+627
-5
lines changed

12 files changed

+627
-5
lines changed

.github/actions/clean-vpc/azure.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
9+
"github.com/Azure/azure-sdk-for-go/profiles/latest/network/mgmt/network"
10+
"github.com/Azure/go-autorest/autorest/azure/auth"
11+
)
12+
13+
func deleteAzureVPCBySubstr(ctx context.Context, subID, resourceGroupName, substr string) (bool, error) {
14+
ok := true
15+
authorizer, err := auth.NewAuthorizerFromEnvironment()
16+
if err != nil {
17+
return false, fmt.Errorf("error creating authorizer: %v", err)
18+
}
19+
vnetClient := network.NewVirtualNetworksClient(subID)
20+
vnetClient.Authorizer = authorizer
21+
22+
vnets, err := vnetClient.List(ctx, resourceGroupName)
23+
if err != nil {
24+
return false, fmt.Errorf("error fetching vnets: %v", err)
25+
}
26+
for _, vnet := range vnets.Values() {
27+
if vnet.Name != nil && strings.HasPrefix(*vnet.Name, substr) {
28+
log.Printf("deleting vnet %s", *vnet.Name)
29+
_, err = vnetClient.Delete(ctx, resourceGroupName, *vnet.Name)
30+
if err != nil {
31+
log.Printf("error deleting vnet: %v", err)
32+
ok = false
33+
}
34+
}
35+
}
36+
37+
return ok, nil
38+
}

.github/actions/clean-vpc/gcp.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"strings"
9+
10+
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/config"
11+
"github.com/mongodb/mongodb-atlas-kubernetes/test/e2e/utils"
12+
13+
"google.golang.org/api/compute/v1"
14+
)
15+
16+
func deleteGCPVPCBySubstr(gcpProjectID, nameSubstr string) (bool, error) {
17+
ok := true
18+
computeService, err := compute.NewService(context.Background())
19+
if err != nil {
20+
return false, fmt.Errorf("failed to create compute service: %s", err)
21+
}
22+
networkService := compute.NewNetworksService(computeService)
23+
networks, err := networkService.List(gcpProjectID).Do()
24+
if err != nil {
25+
return false, fmt.Errorf("failed to list networks: %s", err)
26+
}
27+
for _, network := range networks.Items {
28+
if strings.HasPrefix(network.Name, nameSubstr) {
29+
log.Printf("deleting network %s", network.Name)
30+
_, err = networkService.Delete(gcpProjectID, network.Name).Do()
31+
if err != nil {
32+
log.Printf(fmt.Sprintf("failed to delete network %s: %s", network.Name, err))
33+
ok = false
34+
}
35+
}
36+
}
37+
return ok, nil
38+
}
39+
40+
func setGCPCredentials() error {
41+
err := utils.SaveToFile(config.FileNameSAGCP, []byte(os.Getenv("GCP_SA_CRED")))
42+
if err != nil {
43+
return fmt.Errorf("error saving gcp sa cred to file: %v", err)
44+
}
45+
err = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", config.FileNameSAGCP)
46+
if err != nil {
47+
return fmt.Errorf("error setting GOOGLE_APPLICATION_CREDENTIALS: %v", err)
48+
}
49+
return nil
50+
}

.github/actions/clean-vpc/go.mod

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
module actions/cleanup-vpc
2+
3+
go 1.19
4+
5+
require (
6+
github.com/mongodb/mongodb-atlas-kubernetes v1.4.1-0.20221031083205-66e624f6e882
7+
google.golang.org/api v0.101.0
8+
)
9+
10+
require (
11+
cloud.google.com/go/compute v1.10.0 // indirect
12+
github.com/Azure/azure-sdk-for-go v66.0.0+incompatible // indirect
13+
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
14+
github.com/Azure/go-autorest/autorest v0.11.28 // indirect
15+
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
16+
github.com/Azure/go-autorest/autorest/azure/auth v0.5.11 // indirect
17+
github.com/Azure/go-autorest/autorest/azure/cli v0.4.5 // indirect
18+
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
19+
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
20+
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
21+
github.com/Azure/go-autorest/logger v0.2.1 // indirect
22+
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
23+
github.com/PuerkitoBio/purell v1.1.1 // indirect
24+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
25+
github.com/aws/aws-sdk-go v1.44.121 // indirect
26+
github.com/davecgh/go-spew v1.1.1 // indirect
27+
github.com/dimchansky/utfbom v1.1.1 // indirect
28+
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
29+
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
30+
github.com/go-logr/logr v1.2.3 // indirect
31+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
32+
github.com/go-openapi/jsonreference v0.19.5 // indirect
33+
github.com/go-openapi/swag v0.19.14 // indirect
34+
github.com/gogo/protobuf v1.3.2 // indirect
35+
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
36+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
37+
github.com/golang/protobuf v1.5.2 // indirect
38+
github.com/google/gnostic v0.5.7-v3refs // indirect
39+
github.com/google/go-querystring v1.1.0 // indirect
40+
github.com/google/gofuzz v1.2.0 // indirect
41+
github.com/google/uuid v1.3.0 // indirect
42+
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
43+
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
44+
github.com/jmespath/go-jmespath v0.4.0 // indirect
45+
github.com/josharian/intern v1.0.0 // indirect
46+
github.com/json-iterator/go v1.1.12 // indirect
47+
github.com/mailru/easyjson v0.7.6 // indirect
48+
github.com/mitchellh/go-homedir v1.1.0 // indirect
49+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
50+
github.com/modern-go/reflect2 v1.0.2 // indirect
51+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
52+
github.com/openlyinc/pointy v1.1.2 // indirect
53+
github.com/pborman/uuid v1.2.1 // indirect
54+
github.com/pkg/errors v0.9.1 // indirect
55+
github.com/sethvargo/go-password v0.2.0 // indirect
56+
go.mongodb.org/atlas v0.18.0 // indirect
57+
go.opencensus.io v0.23.0 // indirect
58+
go.uber.org/atomic v1.9.0 // indirect
59+
go.uber.org/multierr v1.7.0 // indirect
60+
go.uber.org/zap v1.23.0 // indirect
61+
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
62+
golang.org/x/net v0.1.0 // indirect
63+
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
64+
golang.org/x/sys v0.1.0 // indirect
65+
golang.org/x/term v0.1.0 // indirect
66+
golang.org/x/text v0.4.0 // indirect
67+
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
68+
google.golang.org/appengine v1.6.7 // indirect
69+
google.golang.org/genproto v0.0.0-20221018160656-63c7b68cfc55 // indirect
70+
google.golang.org/grpc v1.50.1 // indirect
71+
google.golang.org/protobuf v1.28.1 // indirect
72+
gopkg.in/inf.v0 v0.9.1 // indirect
73+
gopkg.in/yaml.v2 v2.4.0 // indirect
74+
gopkg.in/yaml.v3 v3.0.1 // indirect
75+
k8s.io/api v0.25.3 // indirect
76+
k8s.io/apimachinery v0.25.3 // indirect
77+
k8s.io/client-go v0.25.3 // indirect
78+
k8s.io/klog/v2 v2.70.1 // indirect
79+
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
80+
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
81+
sigs.k8s.io/controller-runtime v0.13.0 // indirect
82+
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
83+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
84+
sigs.k8s.io/yaml v1.3.0 // indirect
85+
)

0 commit comments

Comments
 (0)