Skip to content

Commit

Permalink
fix(region): fix default network on get desc (#21396)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi authored Oct 14, 2024
1 parent 151c178 commit 09e95bd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2863,33 +2863,47 @@ 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
}
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
}

// 挂载网卡
Expand Down
5 changes: 5 additions & 0 deletions pkg/compute/models/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions pkg/hostman/guestfs/fsdriver/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 09e95bd

Please sign in to comment.