@@ -116,9 +116,15 @@ func (gcp *GCP) DeletePrivateEndpoint(ctx context.Context, groupName, sAttachmen
116116 return nil
117117}
118118
119- func (gcp * GCP ) DeleteOrphanVPCs (ctx context.Context , lifetimeHours int , vpcNamePrefix string ) ([]string , []string , []error ) {
119+ func (gcp * GCP ) DeleteOrphanVPCs (ctx context.Context , vpcNamePrefixes , regions []string , lifetimeHours int ) ([]string , []string , []error ) {
120+ var filterParts []string
121+ for _ , prefix := range vpcNamePrefixes {
122+ filterParts = append (filterParts , fmt .Sprintf (`name = "%s*"` , prefix ))
123+ }
124+ filterString := strings .Join (filterParts , " OR " )
120125 vpcs := gcp .networkClient .List (ctx , & computepb.ListNetworksRequest {
121126 Project : gcp .projectID ,
127+ Filter : & filterString ,
122128 })
123129
124130 var done , skipped []string
@@ -134,12 +140,7 @@ func (gcp *GCP) DeleteOrphanVPCs(ctx context.Context, lifetimeHours int, vpcName
134140 )
135141 continue
136142 }
137- if ! strings .HasPrefix (vpc .GetName (), vpcNamePrefix ) {
138- skipped = append (skipped ,
139- fmt .Sprintf ("VPC %s skipped\n " , vpc .GetName ()),
140- )
141- continue
142- }
143+
143144 createdAt , err := asTime (vpc .GetCreationTimestamp ())
144145 if err != nil {
145146 errs = append (errs ,
@@ -154,6 +155,24 @@ func (gcp *GCP) DeleteOrphanVPCs(ctx context.Context, lifetimeHours int, vpcName
154155 continue
155156 }
156157
158+ if len (vpc .GetSubnetworks ()) > 0 {
159+ for _ , subnet := range vpc .GetSubnetworks () {
160+ for _ , region := range regions {
161+ _ , err := gcp .subnetworksClient .Delete (ctx , & computepb.DeleteSubnetworkRequest {
162+ Project : gcp .projectID ,
163+ Region : region ,
164+ Subnetwork : path .Base (subnet ),
165+ })
166+ if err != nil {
167+ errs = append (errs ,
168+ fmt .Errorf ("error deleting subnetwork %s: %w" , subnet , err ),
169+ )
170+ continue
171+ }
172+ }
173+ }
174+ }
175+
157176 op , err := gcp .networkClient .Delete (ctx , & computepb.DeleteNetworkRequest {
158177 Project : gcp .projectID ,
159178 Network : vpc .GetName (),
@@ -180,7 +199,7 @@ func (gcp *GCP) DeleteOrphanVPCs(ctx context.Context, lifetimeHours int, vpcName
180199 return done , skipped , errs
181200}
182201
183- func (gcp * GCP ) DeleteOrphanPrivateEndpoints (ctx context.Context , lifetimeHours int , region string , subnet string ) ([]string , []string , []error ) {
202+ func (gcp * GCP ) DeleteOrphanPrivateEndpoints (ctx context.Context , region string , lifetimeHours int ) ([]string , []string , []error ) {
184203 addresses := gcp .addressClient .List (ctx , & computepb.ListAddressesRequest {
185204 Project : gcp .projectID ,
186205 Region : region ,
@@ -193,21 +212,17 @@ func (gcp *GCP) DeleteOrphanPrivateEndpoints(ctx context.Context, lifetimeHours
193212 break
194213 }
195214 if err != nil {
196- errs = append (errs ,
215+ errs = append (
216+ errs ,
197217 fmt .Errorf ("failed iterating addresses in project %v region %v: %w" , gcp .projectID , region , err ),
198218 )
199219 continue
200220 }
201- suffix := fmt .Sprintf ("subnetworks/%s" , subnet )
202- if ! strings .HasSuffix (addr .GetSubnetwork (), suffix ) {
203- skipped = append (skipped ,
204- fmt .Sprintf ("Address %s(%s) skipped, not in %s\n " , addr .GetName (), addr .GetAddress (), subnet ),
205- )
206- continue
207- }
221+
208222 createdAt , err := asTime (addr .GetCreationTimestamp ())
209223 if err != nil {
210- errs = append (errs ,
224+ errs = append (
225+ errs ,
211226 fmt .Errorf (
212227 "failed parsing Address %s(%s) creation timestamp %q: %w" ,
213228 addr .GetCreationTimestamp (), addr .GetName (), addr .GetAddress (), err ,
@@ -224,27 +239,26 @@ func (gcp *GCP) DeleteOrphanPrivateEndpoints(ctx context.Context, lifetimeHours
224239 )
225240 continue
226241 }
227- frName , err := expectForwardingRule (addr .GetUsers ())
228- if err != nil {
229- errs = append (errs , err )
230- continue
231- }
232242
233- if frName != "" {
234- err := gcp .deleteForwardingRule (ctx , frName , region )
235- if err != nil {
236- errs = append (errs ,
237- fmt .Errorf ("failed deleting Forwarding Rule %q in region %q: %w" , region , frName , err ),
243+ if addr .GetStatus () == "IN_USE" {
244+ for _ , user := range addr .GetUsers () {
245+ fr := path .Base (user )
246+ err = gcp .deleteForwardingRule (ctx , fr , region )
247+ if err != nil {
248+ errs = append (errs ,
249+ fmt .Errorf ("failed deleting Forwarding Rule %q in region %q: %w" , region , fr , err ),
250+ )
251+ continue
252+ }
253+ done = append (done ,
254+ fmt .Sprintf ("Deleted Forwarding Rule %s for %s\n " , fr , addr .GetAddress ()),
238255 )
239- continue
240256 }
241- done = append (done ,
242- fmt .Sprintf ("Deleted Forwarding Rule %s for %s\n " , frName , addr .GetAddress ()),
243- )
244257 } else {
245258 skipped = append (skipped ,
246259 fmt .Sprintf ("No forwarding rule using Address %s(%s)" , addr .GetName (), addr .GetAddress ()))
247260 }
261+
248262 if err := gcp .deleteIPAddress (ctx , addr .GetName (), region ); err != nil {
249263 errs = append (errs ,
250264 fmt .Errorf ("error deleting Address %s(%s) in region %q: %w" ,
@@ -262,19 +276,6 @@ func asTime(rfc3339time string) (time.Time, error) {
262276 return time .Parse (time .RFC3339 , rfc3339time )
263277}
264278
265- func expectForwardingRule (usersOfEndpointAddress []string ) (string , error ) {
266- if len (usersOfEndpointAddress ) == 0 {
267- return "" , nil
268- }
269- if len (usersOfEndpointAddress ) > 1 {
270- return "" , fmt .Errorf ("expected a single user of an Endpoint Address, but got %v" , usersOfEndpointAddress )
271- }
272- if strings .Contains (usersOfEndpointAddress [0 ], "/forwardingRules/" ) {
273- return path .Base (usersOfEndpointAddress [0 ]), nil
274- }
275- return "" , fmt .Errorf ("expected a Forwarding Rule user for Endpoint Address but got %s" , usersOfEndpointAddress [0 ])
276- }
277-
278279func (gcp * GCP ) DeleteCryptoKey (ctx context.Context , keyName string ) error {
279280 _ , err := gcp .keyManagementClient .GetCryptoKeyVersion (ctx , & kmspb.GetCryptoKeyVersionRequest {
280281 Name : keyName ,
0 commit comments