github.com/openshift/installer@v1.4.17/pkg/destroy/gcp/subnetwork.go (about) 1 package gcp 2 3 import ( 4 "context" 5 6 "github.com/pkg/errors" 7 "google.golang.org/api/compute/v1" 8 "google.golang.org/api/googleapi" 9 10 "github.com/openshift/installer/pkg/types/gcp" 11 ) 12 13 func (o *ClusterUninstaller) listSubnetworks(ctx context.Context) ([]cloudResource, error) { 14 return o.listSubnetworksWithFilter(ctx, "items(name,network),nextPageToken", o.clusterIDFilter(), nil) 15 } 16 17 // listSubnetworksWithFilter lists subnetworks in the project that satisfy the filter criteria. 18 // The fields parameter specifies which fields should be returned in the result, the filter string contains 19 // a filter string passed to the API to filter results. The filterFunc is a client-side filtering function 20 // that determines whether a particular result should be returned or not. 21 func (o *ClusterUninstaller) listSubnetworksWithFilter(ctx context.Context, fields string, filter string, filterFunc func(*compute.Subnetwork) bool) ([]cloudResource, error) { 22 o.Logger.Debugf("Listing subnetworks") 23 ctx, cancel := context.WithTimeout(ctx, defaultTimeout) 24 defer cancel() 25 result := []cloudResource{} 26 req := o.computeSvc.Subnetworks.List(o.ProjectID, o.Region).Fields(googleapi.Field(fields)) 27 if len(filter) > 0 { 28 req = req.Filter(filter) 29 } 30 err := req.Pages(ctx, func(list *compute.SubnetworkList) error { 31 for _, item := range list.Items { 32 if filterFunc == nil || filterFunc != nil && filterFunc(item) { 33 o.Logger.Debugf("Found subnetwork: %s", item.Name) 34 result = append(result, cloudResource{ 35 key: item.Name, 36 name: item.Name, 37 typeName: "subnetwork", 38 quota: []gcp.QuotaUsage{{ 39 Metric: &gcp.Metric{ 40 Service: gcp.ServiceComputeEngineAPI, 41 Limit: "subnetworks", 42 }, 43 Amount: 1, 44 }, { 45 Metric: &gcp.Metric{ 46 Service: gcp.ServiceComputeEngineAPI, 47 Limit: "subnet_ranges_per_vpc_network", 48 Dimensions: map[string]string{ 49 "network_id": getNameFromURL("networks", item.Network), 50 }, 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 subnetworks") 61 } 62 return result, nil 63 } 64 65 func (o *ClusterUninstaller) deleteSubnetwork(ctx context.Context, item cloudResource) error { 66 o.Logger.Debugf("Deleting subnetwork %s", item.name) 67 ctx, cancel := context.WithTimeout(ctx, defaultTimeout) 68 defer cancel() 69 op, err := o.computeSvc.Subnetworks.Delete(o.ProjectID, o.Region, 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 subnetwork %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 subnetwork %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 subnetwork %s", item.name) 82 } 83 return nil 84 } 85 86 // destroySubNetworks removes all subnetwork resources that have a name prefixed 87 // with the cluster's infra ID. 88 func (o *ClusterUninstaller) destroySubnetworks(ctx context.Context) error { 89 found, err := o.listSubnetworks(ctx) 90 if err != nil { 91 return err 92 } 93 items := o.insertPendingItems("subnetwork", found) 94 for _, item := range items { 95 err := o.deleteSubnetwork(ctx, item) 96 if err != nil { 97 o.errorTracker.suppressWarning(item.key, err, o.Logger) 98 } 99 } 100 if items = o.getPendingItems("subnetwork"); len(items) > 0 { 101 return errors.Errorf("%d items pending", len(items)) 102 } 103 return nil 104 }