From 09e95bda7a9f600ef5374dbcc6721d54bcff5656 Mon Sep 17 00:00:00 2001 From: wanyaoqi <18528551+wanyaoqi@users.noreply.github.com> Date: Mon, 14 Oct 2024 12:36:09 +0800 Subject: [PATCH] fix(region): fix default network on get desc (#21396) --- pkg/compute/models/guest_actions.go | 42 ++++++++++++++++++--------- pkg/compute/models/guests.go | 5 ++++ pkg/hostman/guestfs/fsdriver/linux.go | 5 ++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index 454b0b37418..68c29b76e6e 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -2863,10 +2863,22 @@ func (self *SGuest) PerformDetachnetwork( return nil, nil } -func (guest *SGuest) fixDefaultGateway(ctx context.Context, userCred mcclient.TokenCredential) error { +func (guest *SGuest) fixDefaultGatewayByNics(ctx context.Context, userCred mcclient.TokenCredential, nics []SGuestnetwork) (bool, error) { defaultGwCnt := 0 + for i := range nics { + if nics[i].Virtual || len(nics[i].TeamWith) > 0 { + continue + } + if nics[i].IsDefault { + defaultGwCnt++ + } + } + + if defaultGwCnt == 1 { + return false, nil + } + nicList := netutils2.SNicInfoList{} - nics, _ := guest.GetNetworks("") for i := range nics { if nics[i].Virtual || len(nics[i].TeamWith) > 0 { continue @@ -2874,22 +2886,24 @@ func (guest *SGuest) fixDefaultGateway(ctx context.Context, userCred mcclient.To net, _ := nics[i].GetNetwork() if net != nil { nicList = nicList.Add(nics[i].IpAddr, nics[i].MacAddr, net.GuestGateway) - if nics[i].IsDefault { - defaultGwCnt++ - } } } - if defaultGwCnt != 1 { - gwMac, _ := nicList.FindDefaultNicMac() - if gwMac != "" { - err := guest.setDefaultGateway(ctx, userCred, gwMac) - if err != nil { - log.Errorf("setDefaultGateway fail %s", err) - return errors.Wrap(err, "setDefaultGateway") - } + + gwMac, _ := nicList.FindDefaultNicMac() + if gwMac != "" { + err := guest.setDefaultGateway(ctx, userCred, gwMac) + if err != nil { + log.Errorf("setDefaultGateway fail %s", err) + return true, errors.Wrap(err, "setDefaultGateway") } } - return nil + return true, nil +} + +func (guest *SGuest) fixDefaultGateway(ctx context.Context, userCred mcclient.TokenCredential) error { + nics, _ := guest.GetNetworks("") + _, err := guest.fixDefaultGatewayByNics(ctx, userCred, nics) + return err } // 挂载网卡 diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 9186ef2b666..35c81cbb8e0 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -5222,6 +5222,10 @@ func (self *SGuest) GetJsonDescAtHypervisor(ctx context.Context, host *SHost) *a // nics, domain desc.Domain = options.Options.DNSDomain nics, _ := self.GetNetworks("") + changed, _ := self.fixDefaultGatewayByNics(ctx, auth.AdminCredential(), nics) + if changed { + nics, _ = self.GetNetworks("") + } for _, nic := range nics { nicDesc := nic.getJsonDescAtHost(ctx, host) desc.Nics = append(desc.Nics, nicDesc) @@ -5331,6 +5335,7 @@ func (self *SGuest) GetJsonDescAtBaremetal(ctx context.Context, host *SHost) *ap desc.DiskConfig = host.getDiskConfig() + self.fixDefaultGateway(ctx, auth.AdminCredential()) netifs := host.GetAllNetInterfaces() desc.Domain = options.Options.DNSDomain diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 5afca73b3be..cbfd87993da 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -1317,6 +1317,11 @@ func getMainNic(nics []*types.SServerNic) *types.SServerNic { return nics[i] } } + for i := range nics { + if len(nics[i].Gateway) > 0 { + return nics[i] + } + } return nil }