@@ -25,7 +25,6 @@ import (
2525 "github.com/GoogleCloudPlatform/galog"
2626 "github.com/GoogleCloudPlatform/google-guest-agent/internal/cfg"
2727 "github.com/GoogleCloudPlatform/google-guest-agent/internal/network/address"
28- "github.com/GoogleCloudPlatform/google-guest-agent/internal/network/service"
2928 "github.com/GoogleCloudPlatform/google-guest-agent/internal/run"
3029 "golang.org/x/exp/maps"
3130)
@@ -398,16 +397,40 @@ func (lc *linuxClient) RemoveRoutes(ctx context.Context, iface string) error {
398397
399398// SetupRoutes sets up the routes for the network interfaces. This uses ip route
400399// commands to delete extra routes and add missing routes.
401- func (lc * linuxClient ) Setup (ctx context.Context , opts * service.Options ) error {
402- nicConfigs := opts .FilteredNICConfigs ()
400+ func (lc * linuxClient ) Setup (ctx context.Context , opts * SetupOptions ) error {
401+ if opts == nil {
402+ galog .Debugf ("No setup options provided." )
403+ return nil
404+ }
405+ galog .Debugf ("Running ip route setup." )
406+
407+ if opts .AlreadyChecked {
408+ if len (opts .MissingRoutes ) == 0 && len (opts .ExtraRoutes ) == 0 {
409+ galog .Debugf ("No routes to setup." )
410+ return nil
411+ }
412+
413+ for iface , routes := range opts .ExtraRoutes {
414+ lc .deleteExtraRoutes (ctx , iface , routes )
415+ }
416+ for iface , routes := range opts .MissingRoutes {
417+ lc .addMissingRoutes (ctx , iface , routes )
418+ }
419+ return nil
420+ }
421+
422+ if opts .ServiceOptions == nil {
423+ galog .Debugf ("No service options provided." )
424+ return nil
425+ }
426+ nicConfigs := opts .ServiceOptions .FilteredNICConfigs ()
403427 if len (nicConfigs ) == 0 {
404428 galog .Debugf ("No NICs to setup routes for." )
405429 return nil
406430 }
407- galog .Debugf ("Running ip route setup." )
408431
409432 // Fallback route setup uses ip commands.
410- for _ , nic := range opts . FilteredNICConfigs () {
433+ for _ , nic := range nicConfigs {
411434 if nic .Interface == nil {
412435 galog .Debugf ("Skipping route setup for interface index %d: interface is nil" , nic .Index )
413436 continue
@@ -418,44 +441,60 @@ func (lc *linuxClient) Setup(ctx context.Context, opts *service.Options) error {
418441 }
419442
420443 // Find extra routes to delete.
421- extraAddrs := nic .ExtraAddresses .MergedMap ()
422- extraRoutes , err := lc .ExtraRoutes (ctx , nic .Interface .Name (), extraAddrs )
423- if err != nil {
424- return fmt .Errorf ("failed to get extra routes for interface %q: %w" , nic .Interface .Name (), err )
425- }
426-
427- if len (extraRoutes ) == 0 {
428- galog .Debugf ("No extra routes to delete for interface %q" , nic .Interface .Name ())
444+ var extraAddrs address.IPAddressMap
445+ var extraRoutes []Handle
446+ var err error
447+ if opts .AlreadyChecked {
448+ extraRoutes = opts .ExtraRoutes [nic .Interface .Name ()]
429449 } else {
430- galog .Infof ("Deleting extra routes %v for interface %q" , extraRoutes , nic .Interface .Name ())
431- for _ , r := range extraRoutes {
432- if err = lc .Delete (ctx , r ); err != nil {
433- // Continue to delete the rest of the routes, and only log the error.
434- galog .Errorf ("Failed to delete route %q for interface %q: %v" , r .Destination .String (), nic .Interface .Name (), err )
435- }
450+ extraAddrs = nic .ExtraAddresses .MergedMap ()
451+ extraRoutes , err = lc .ExtraRoutes (ctx , nic .Interface .Name (), extraAddrs )
452+ if err != nil {
453+ return fmt .Errorf ("failed to get extra routes for interface %q: %w" , nic .Interface .Name (), err )
436454 }
437455 }
456+ lc .deleteExtraRoutes (ctx , nic .Interface .Name (), extraRoutes )
438457
439458 // Find missing routes for the given interface.
440- missingRoutes , err := lc .MissingRoutes (ctx , nic .Interface .Name (), extraAddrs )
441- if err != nil {
442- return fmt .Errorf ("failed to get missing routes for interface %q: %w" , nic .Interface .Name (), err )
459+ var missingRoutes []Handle
460+ if opts .AlreadyChecked {
461+ missingRoutes = opts .MissingRoutes [nic .Interface .Name ()]
462+ } else {
463+ missingRoutes , err = lc .MissingRoutes (ctx , nic .Interface .Name (), extraAddrs )
464+ if err != nil {
465+ return fmt .Errorf ("failed to get missing routes for interface %q: %w" , nic .Interface .Name (), err )
466+ }
443467 }
468+ lc .addMissingRoutes (ctx , nic .Interface .Name (), missingRoutes )
469+ }
470+ galog .Debugf ("Finished ip route setup." )
471+ return nil
472+ }
444473
445- if len (missingRoutes ) == 0 {
446- galog .Debugf ("No missing routes to add for interface %q" , nic .Interface .Name ())
447- continue
474+ func (lc * linuxClient ) deleteExtraRoutes (ctx context.Context , iface string , extraRoutes []Handle ) {
475+ if len (extraRoutes ) == 0 {
476+ galog .Debugf ("No extra routes to delete for interface %q" , iface )
477+ return
478+ }
479+ galog .Infof ("Deleting extra routes %v for interface %q" , extraRoutes , iface )
480+ for _ , r := range extraRoutes {
481+ if err := lc .Delete (ctx , r ); err != nil {
482+ // Continue to delete the rest of the routes, and only log the error.
483+ galog .Errorf ("Failed to delete route %q for interface %q: %v" , r .Destination .String (), iface , err )
448484 }
449- galog .Infof ("Adding routes %v for interface %q" , missingRoutes , nic .Interface .Name ())
485+ }
486+ }
450487
451- // Add the missing routes.
452- for _ , r := range missingRoutes {
453- if err = lc .Add (ctx , r ); err != nil {
454- // Continue to add the rest of the routes, and only log the error.
455- galog .Errorf ("Failed to add route %q for interface %q: %v" , r .Destination .String (), nic .Interface .Name (), err )
456- }
488+ func (lc * linuxClient ) addMissingRoutes (ctx context.Context , iface string , missingRoutes []Handle ) {
489+ if len (missingRoutes ) == 0 {
490+ galog .Debugf ("No missing routes to add for interface %q" , iface )
491+ return
492+ }
493+ galog .Infof ("Adding routes %v for interface %q" , missingRoutes , iface )
494+ for _ , r := range missingRoutes {
495+ if err := lc .Add (ctx , r ); err != nil {
496+ // Continue to add the rest of the routes, and only log the error.
497+ galog .Errorf ("Failed to add route %q for interface %q: %v" , r .Destination .String (), iface , err )
457498 }
458499 }
459- galog .Debugf ("Finished ip route setup." )
460- return nil
461500}
0 commit comments