github.com/openshift/installer@v1.4.17/pkg/destroy/gcp/route.go (about) 1 package gcp 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/pkg/errors" 9 "google.golang.org/api/compute/v1" 10 "google.golang.org/api/googleapi" 11 12 "github.com/openshift/installer/pkg/types/gcp" 13 ) 14 15 func (o *ClusterUninstaller) listNetworkRoutes(ctx context.Context, networkURL string) ([]cloudResource, error) { 16 return o.listRoutesWithFilter(ctx, "items(name),nextPageToken", fmt.Sprintf("network eq %q", networkURL), nil) 17 } 18 19 func (o *ClusterUninstaller) listRoutes(ctx context.Context) ([]cloudResource, error) { 20 return o.listRoutesWithFilter(ctx, "items(name),nextPageToken", o.clusterIDFilter(), nil) 21 } 22 23 // listRoutesWithFilter lists routes in the project that satisfy the filter criteria. 24 // The fields parameter specifies which fields should be returned in the result, the filter string contains 25 // a filter string passed to the API to filter results. The filterFunc is a client-side filtering function 26 // that determines whether a particular result should be returned or not. 27 func (o *ClusterUninstaller) listRoutesWithFilter(ctx context.Context, fields string, filter string, filterFunc func(*compute.Route) bool) ([]cloudResource, error) { 28 o.Logger.Debugf("Listing routes") 29 ctx, cancel := context.WithTimeout(ctx, defaultTimeout) 30 defer cancel() 31 result := []cloudResource{} 32 req := o.computeSvc.Routes.List(o.ProjectID).Fields(googleapi.Field(fields)) 33 if len(filter) > 0 { 34 req = req.Filter(filter) 35 } 36 err := req.Pages(ctx, func(list *compute.RouteList) error { 37 for _, item := range list.Items { 38 if filterFunc == nil || filterFunc != nil && filterFunc(item) { 39 if strings.HasPrefix(item.Name, "default-route-") { 40 continue 41 } 42 o.Logger.Debugf("Found route: %s", item.Name) 43 result = append(result, cloudResource{ 44 key: item.Name, 45 name: item.Name, 46 typeName: "route", 47 quota: []gcp.QuotaUsage{{ 48 Metric: &gcp.Metric{ 49 Service: gcp.ServiceComputeEngineAPI, 50 Limit: "routes", 51 }, 52 Amount: 1, 53 }}, 54 }) 55 } 56 } 57 return nil 58 }) 59 if err != nil { 60 return nil, errors.Wrapf(err, "failed to list routes") 61 } 62 return result, nil 63 } 64 65 func (o *ClusterUninstaller) deleteRoute(ctx context.Context, item cloudResource) error { 66 o.Logger.Debugf("Deleting route %s", item.name) 67 ctx, cancel := context.WithTimeout(ctx, defaultTimeout) 68 defer cancel() 69 op, err := o.computeSvc.Routes.Delete(o.ProjectID, item.name).RequestId(o.requestID(item.typeName, item.name)).Context(ctx).Do() 70 if err != nil && !isNoOp(err) { 71 o.resetRequestID(item.typeName, item.name) 72 return errors.Wrapf(err, "failed to delete route %s", item.name) 73 } 74 if op != nil && op.Status == "DONE" && isErrorStatus(op.HttpErrorStatusCode) { 75 o.resetRequestID(item.typeName, item.name) 76 return errors.Errorf("failed to delete route %s with error: %s", item.name, operationErrorMessage(op)) 77 } 78 if (err != nil && isNoOp(err)) || (op != nil && op.Status == "DONE") { 79 o.resetRequestID(item.typeName, item.name) 80 o.deletePendingItems(item.typeName, []cloudResource{item}) 81 o.Logger.Infof("Deleted route %s", item.name) 82 } 83 return nil 84 } 85 86 // destroyRutes removes all route resources that have a name prefixed 87 // with the cluster's infra ID. 88 func (o *ClusterUninstaller) destroyRoutes(ctx context.Context) error { 89 found, err := o.listRoutes(ctx) 90 if err != nil { 91 return err 92 } 93 items := o.insertPendingItems("route", found) 94 for _, item := range items { 95 err := o.deleteRoute(ctx, item) 96 if err != nil { 97 o.errorTracker.suppressWarning(item.key, err, o.Logger) 98 } 99 } 100 if items = o.getPendingItems("route"); len(items) > 0 { 101 return errors.Errorf("%d items pending", len(items)) 102 } 103 return nil 104 }