@@ -36,12 +36,13 @@ import (
3636
3737const (
3838 // assume total ownership of these networks by dranet
39- wellKnownPrefix = "dranet "
39+ wellKnownPrefix = "dranetctl "
4040)
4141
4242var (
4343 // extract region and subnet name from URL
44- reSubnets = regexp .MustCompile (`/regions/([^/]+)/subnetworks/([^/]+)$` )
44+ reSubnets = regexp .MustCompile (`/regions/([^/]+)/subnetworks/([^/]+)$` )
45+ acceleratorPodNameFlag string
4546)
4647
4748// getRegion get the region part from a location
@@ -92,7 +93,7 @@ func createAcceleratorNetworks(ctx context.Context, acceleratorpodName string, n
9293 }
9394
9495 // Create Subnetwork
95- // get a non overllaping range from the Class E
96+ // get a non overlaping range from the Class E
9697 // TODO: this needs to be handled better
9798 networkURL := fmt .Sprintf ("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s" , projectID , networkName )
9899 cidr := fmt .Sprintf ("255.255.%d.0/24" , 20 + i )
@@ -311,7 +312,7 @@ func deleteNetwork(ctx context.Context, networkName string) error {
311312}
312313
313314// listNetworks list all dranet networks
314- func listNetworks (ctx context.Context ) []string {
315+ func listNetworks (ctx context.Context , acceleratorPodName string ) []string {
315316 output := []string {}
316317 // Prepare the List request.
317318 req := & computepb.ListNetworksRequest {
@@ -330,9 +331,18 @@ func listNetworks(ctx context.Context) []string {
330331 return output
331332 }
332333
333- if strings .HasPrefix (* network .Name , wellKnownPrefix ) {
334- output = append (output , * network .Name )
334+ // it assumes ownership via the well known prefix
335+ if ! strings .HasPrefix (* network .Name , wellKnownPrefix ) {
336+ continue
337+ }
338+ // filter by accelerator pod name if exist
339+ if acceleratorPodName != "" &&
340+ ! strings .Contains (* network .Name , obtainHexHash (acceleratorPodName )) {
341+ continue
335342 }
343+
344+ output = append (output , * network .Name )
345+
336346 klog .V (2 ).Infof ("Name: %s\n " , * network .Name )
337347 klog .V (2 ).Infof (" ID: %d\n " , network .Id )
338348 klog .V (2 ).Infof (" SelfLink: %s\n " , * network .SelfLink )
@@ -358,9 +368,10 @@ var cleanupNetworksCmd = &cobra.Command{
358368 Short : "Deletes all Google Cloud networks labeled as managed by DRA-Net" ,
359369 Long : `This command lists all Google Cloud networks in the specified project and deletes those created by dranetctl.
360370Use with caution, as this action is irreversible.` ,
371+ Args : cobra .MaximumNArgs (0 ),
361372 Run : func (cmd * cobra.Command , args []string ) {
362373 ctx := cmd .Context ()
363- networks := listNetworks (ctx )
374+ networks := listNetworks (ctx , acceleratorPodNameFlag )
364375 for _ , network := range networks {
365376 klog .Infof ("deleting network %s\n " , network )
366377 err := deleteNetwork (ctx , network )
@@ -374,9 +385,10 @@ Use with caution, as this action is irreversible.`,
374385var listNetworksCmd = & cobra.Command {
375386 Use : "list" ,
376387 Short : "Lists all Google Cloud networks in a project" ,
388+ Args : cobra .MaximumNArgs (0 ), // optional the acceleratorpod name as an argument
377389 Run : func (cmd * cobra.Command , args []string ) {
378390 ctx := cmd .Context ()
379- networks := listNetworks (ctx )
391+ networks := listNetworks (ctx , acceleratorPodNameFlag )
380392 fmt .Printf ("There are %d dranet networks\n " , len (networks ))
381393 fmt .Println ("---" )
382394 for _ , network := range networks {
@@ -388,4 +400,5 @@ var listNetworksCmd = &cobra.Command{
388400func init () {
389401 networksCmd .AddCommand (cleanupNetworksCmd )
390402 networksCmd .AddCommand (listNetworksCmd )
403+ networksCmd .PersistentFlags ().StringVar (& acceleratorPodNameFlag , "acceleratorpod" , "" , "Name of the accelerator pod to filter networks" )
391404}
0 commit comments