Skip to content

Commit

Permalink
Merge branch 'feat/gcp-support-routers' into 'main'
Browse files Browse the repository at this point in the history
feat: GCP add Router support

See merge request qovery/backend/pleco!115
  • Loading branch information
benjaminch committed Mar 29, 2024
2 parents ddeb968 + af4ea7d commit d959477
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Check out our Blog announcement of Pleco: https://www.qovery.com/blog/announceme
- [X] Artifact Registry Repositories
- [X] Kubernetes clusters
- [X] Networks // via JSON tags in resource description because resource has no support for tags
- [X] Routers // via JSON tags in resource description because resource has no support for tags
- [X] Service accounts // via JSON tags in resource description because resource has no support for tags
- [ ] AZURE

Expand Down Expand Up @@ -257,6 +258,7 @@ Here are some of the resources you can check:
--enable-object-storage # Enable object storage watch
--enable-artifact-registry # Enable artifact registry watch
--enable-network # Enable network watch
--enable-router # Enable router watch
--enable-iam # Enable IAM watch (service accounts)
```

Expand All @@ -272,6 +274,7 @@ pleco start
--enable-artifact-registry
--enable-cluster
--enable-network
--enable-router
--enable-iam
--gcp-regions
europe-west9
Expand Down
1 change: 1 addition & 0 deletions charts/pleco/values-gcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ gcpFeatures:
- us-west4
cluster: true
network: true
router: true
iam: true
artifactRegistry: true
objectStorage: true
Expand Down
1 change: 1 addition & 0 deletions charts/pleco/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ gcpFeatures:
# - europe-west9
cluster: false
network: false
router: false
iam: false
objectStorage: false
artifactRegistry: false
Expand Down
1 change: 1 addition & 0 deletions pkg/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func startGCP(cmd *cobra.Command, interval int64, dryRun bool, disableTTLCheck b
EnableCluster: getCmdBool(cmd, "enable-cluster"),
EnableBucket: getCmdBool(cmd, "enable-object-storage"),
EnableNetwork: getCmdBool(cmd, "enable-network"),
EnableRouter: getCmdBool(cmd, "enable-router"),
EnableArtifactRegistry: getCmdBool(cmd, "enable-artifact-registry"),
EnableIAM: getCmdBool(cmd, "enable-iam"),
}
Expand Down
1 change: 1 addition & 0 deletions pkg/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ func initGCPFlags(startCmd *cobra.Command) {
startCmd.Flags().BoolP("enable-object-storage", "", false, "Enable object storage buckets watch")
startCmd.Flags().BoolP("enable-artifact-registry", "", false, "Enable security groups watch")
startCmd.Flags().BoolP("enable-network", "", false, "Enable Networks and its children watch")
startCmd.Flags().BoolP("enable-router", "", false, "Enable Routers and its children watch")
startCmd.Flags().BoolP("enable-iam", "", false, "Enable IAM (service accounts) watch")
}
89 changes: 89 additions & 0 deletions pkg/gcp/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package gcp

import (
"cloud.google.com/go/compute/apiv1/computepb"
"context"
"encoding/json"
"fmt"
"github.com/Qovery/pleco/pkg/common"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)

func DeleteExpiredRouters(sessions GCPSessions, options GCPOptions) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

routersIterator := sessions.Router.List(ctx, &computepb.ListRoutersRequest{
Project: options.ProjectID,
Region: options.Location,
})

for {
router, err := routersIterator.Next()
if err != nil {
break
}

routerName := ""
if router.Name != nil {
routerName = *router.Name
}
routerDescription := ""
if router.Description != nil {
routerDescription = *router.Description
}

resourceTags := common.ResourceTags{}
if err = json.Unmarshal([]byte(routerDescription), &resourceTags); err != nil {
log.Info(fmt.Sprintf("No resource tags found in description field, ignoring this router (`%s`)", routerName))
continue
}
ttlStr := ""
if resourceTags.TTL != nil {
ttlStr = resourceTags.TTL.String()
} else {
log.Info(fmt.Sprintf("No ttl value found, ignoring this router (`%s`)", routerName))
continue
}
ttl, err := strconv.ParseInt(ttlStr, 10, 64)
if err != nil {
log.Warn(fmt.Sprintf("ttl label value `%s` is not parsable to int64, ignoring this router (`%s`)", ttlStr, routerName))
continue
}
creationTimeStr := ""
if resourceTags.CreationUnixTimestamp != nil {
creationTimeStr = resourceTags.CreationUnixTimestamp.String()
} else {
log.Info(fmt.Sprintf("No creation time value found, ignoring this router (`%s`)", routerName))
continue
}
creationTimeInt64, err := strconv.ParseInt(creationTimeStr, 10, 64)
if err != nil {
log.Warn(fmt.Sprintf("creation_date label value `%s` is not parsable to int64, ignoring this router (`%s`)", creationTimeStr, routerName))
continue
}
creationTime := time.Unix(creationTimeInt64, 0).UTC()

// Router is not expired (or is protected TTL = 0)
if ttl == 0 || creationTimeInt64 == 0 || time.Now().UTC().Before(creationTime.Add(time.Second*time.Duration(ttl))) {
continue
}

if options.DryRun {
log.Info(fmt.Sprintf("Router `%s will be deleted`", routerName))
continue
}

log.Info(fmt.Sprintf("Deleting router `%s`", routerName))
_, err = sessions.Router.Delete(ctx, &computepb.DeleteRouterRequest{
Project: options.ProjectID,
Region: options.Location,
Router: routerName,
})
if err != nil {
log.Error(fmt.Sprintf("Error deleting router `%s`, error: %s", routerName, err))
}
}
}
14 changes: 14 additions & 0 deletions pkg/gcp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ type GCPOptions struct {
EnableNetwork bool
EnableArtifactRegistry bool
EnableIAM bool
EnableRouter bool
}

type GCPSessions struct {
Bucket *storage.Client
ArtifactRegistry *artifactregistry.Client
Cluster *container.ClusterManagerClient
Network *compute.NetworksClient
Router *compute.RoutersClient
IAM *iam.Service
}

Expand Down Expand Up @@ -102,6 +104,18 @@ func runPlecoInRegion(location string, interval int64, wg *sync.WaitGroup, optio
listServiceToCheckStatus = append(listServiceToCheckStatus, DeleteExpiredVPCs)
}

if options.EnableRouter {
routerClient, err := compute.NewRoutersRESTClient(ctx)
if err != nil {
logrus.Errorf("compute.NewRoutersRESTClient: %s", err)
return
}
defer routerClient.Close()
sessions.Router = routerClient

listServiceToCheckStatus = append(listServiceToCheckStatus, DeleteExpiredRouters)
}

if options.EnableIAM {
iamService, err := iam.NewService(ctx)
if err != nil {
Expand Down

0 comments on commit d959477

Please sign in to comment.