fix(region): raise GetNamedNetworkConfiguration error

This commit is contained in:
Zexi Li
2021-07-28 17:23:29 +08:00
parent 705d7a0076
commit cc5bc646bf
5 changed files with 26 additions and 19 deletions
+8 -6
View File
@@ -135,8 +135,11 @@ func (self *SBaremetalGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk
return httperrors.NewUnsupportOperationError("Cannot resize disk for baremtal")
}
func (self *SBaremetalGuestDriver) GetNamedNetworkConfiguration(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, netConfig *api.NetworkConfig) (*models.SNetwork, []models.SNicConfig, api.IPAllocationDirection, bool) {
netifs, net := host.GetNetinterfacesWithIdAndCredential(netConfig.Network, userCred, netConfig.Reserved)
func (self *SBaremetalGuestDriver) GetNamedNetworkConfiguration(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, netConfig *api.NetworkConfig) (*models.SNetwork, []models.SNicConfig, api.IPAllocationDirection, bool, error) {
netifs, net, err := host.GetNetinterfacesWithIdAndCredential(netConfig.Network, userCred, netConfig.Reserved)
if err != nil {
return nil, nil, "", false, errors.Wrap(err, "get host netinterfaces")
}
if netifs != nil {
nicCnt := 1
if netConfig.RequireTeaming || netConfig.TryTeaming {
@@ -144,8 +147,7 @@ func (self *SBaremetalGuestDriver) GetNamedNetworkConfiguration(guest *models.SG
}
if len(netifs) < nicCnt {
if netConfig.RequireTeaming {
log.Errorf("not enough network interfaces, want %d got %d", nicCnt, len(netifs))
return net, nil, "", false
return net, nil, "", false, errors.Errorf("not enough network interfaces, want %d got %d", nicCnt, len(netifs))
}
nicCnt = len(netifs)
}
@@ -166,9 +168,9 @@ func (self *SBaremetalGuestDriver) GetNamedNetworkConfiguration(guest *models.SG
reuseAddr = true
}
return net, nicConfs, api.IPAllocationStepup, reuseAddr
return net, nicConfs, api.IPAllocationStepup, reuseAddr, nil
}
return net, nil, "", false
return net, nil, "", false, nil
}
func (self *SBaremetalGuestDriver) GetRandomNetworkTypes() []string {
+6 -3
View File
@@ -55,8 +55,11 @@ func (self *SVirtualizedGuestDriver) PrepareDiskRaidConfig(userCred mcclient.Tok
return nil, nil
}
func (self *SVirtualizedGuestDriver) GetNamedNetworkConfiguration(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, netConfig *api.NetworkConfig) (*models.SNetwork, []models.SNicConfig, api.IPAllocationDirection, bool) {
net, _ := host.GetNetworkWithId(netConfig.Network, netConfig.Reserved)
func (self *SVirtualizedGuestDriver) GetNamedNetworkConfiguration(guest *models.SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *models.SHost, netConfig *api.NetworkConfig) (*models.SNetwork, []models.SNicConfig, api.IPAllocationDirection, bool, error) {
net, err := host.GetNetworkWithId(netConfig.Network, netConfig.Reserved)
if err != nil {
return nil, nil, "", false, errors.Wrapf(err, "get network with id %q, reserverd %v", netConfig.Network, netConfig.Reserved)
}
nicConfs := []models.SNicConfig{
{
Mac: netConfig.Mac,
@@ -75,7 +78,7 @@ func (self *SVirtualizedGuestDriver) GetNamedNetworkConfiguration(guest *models.
if len(netConfig.Address) > 0 && !options.Options.EnablePreAllocateIpAddr && !utils.IsInStringArray(host.GetProviderName(), []string{api.CLOUD_PROVIDER_ONECLOUD, api.CLOUD_PROVIDER_VMWARE}) {
reUse = true
}
return net, nicConfs, api.IPAllocationStepdown, reUse
return net, nicConfs, api.IPAllocationStepdown, reUse, nil
}
func (self *SVirtualizedGuestDriver) GetRandomNetworkTypes() []string {
+1 -1
View File
@@ -69,7 +69,7 @@ type IGuestDriver interface {
PrepareDiskRaidConfig(userCred mcclient.TokenCredential, host *SHost, params []*api.BaremetalDiskConfig, disks []*api.DiskConfig) ([]*api.DiskConfig, error)
GetNamedNetworkConfiguration(guest *SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *SHost, netConfig *api.NetworkConfig) (*SNetwork, []SNicConfig, api.IPAllocationDirection, bool)
GetNamedNetworkConfiguration(guest *SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *SHost, netConfig *api.NetworkConfig) (*SNetwork, []SNicConfig, api.IPAllocationDirection, bool, error)
Attach2RandomNetwork(guest *SGuest, ctx context.Context, userCred mcclient.TokenCredential, host *SHost, netConfig *api.NetworkConfig, pendingUsage quotas.IQuota) ([]SGuestnetwork, error)
GetRandomNetworkTypes() []string
+5 -3
View File
@@ -3481,7 +3481,10 @@ func (self *SGuest) attach2NetworkDesc(
func (self *SGuest) attach2NamedNetworkDesc(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, netConfig *api.NetworkConfig, pendingUsage quotas.IQuota) ([]SGuestnetwork, error) {
driver := self.GetDriver()
net, nicConfs, allocDir, reuseAddr := driver.GetNamedNetworkConfiguration(self, ctx, userCred, host, netConfig)
net, nicConfs, allocDir, reuseAddr, err := driver.GetNamedNetworkConfiguration(self, ctx, userCred, host, netConfig)
if err != nil {
return nil, errors.Wrapf(err, "GetNamedNetworkConfiguration on host %q", host.GetName())
}
if net != nil {
if len(nicConfs) == 0 {
return nil, fmt.Errorf("no avaialble network interface?")
@@ -3500,8 +3503,7 @@ func (self *SGuest) attach2NamedNetworkDesc(ctx context.Context, userCred mcclie
NicConfs: nicConfs,
})
if err != nil {
log.Errorf("Attach2Network fail %s", err)
return nil, err
return nil, errors.Wrap(err, "Attach2Network fail")
} else {
return gn, nil
}
+6 -6
View File
@@ -2453,18 +2453,18 @@ func (self *SHost) getNetworkOfIPOnHost(ipAddr string) (*SNetwork, error) {
return nil, fmt.Errorf("IP %s not reachable on this host", ipAddr)
}
func (self *SHost) GetNetinterfacesWithIdAndCredential(netId string, userCred mcclient.TokenCredential, reserved bool) ([]SNetInterface, *SNetwork) {
func (self *SHost) GetNetinterfacesWithIdAndCredential(netId string, userCred mcclient.TokenCredential, reserved bool) ([]SNetInterface, *SNetwork, error) {
netObj, err := NetworkManager.FetchById(netId)
if err != nil {
return nil, nil
return nil, nil, errors.Wrapf(err, "fetch by id %q", netId)
}
net := netObj.(*SNetwork)
used, err := net.getFreeAddressCount()
if err != nil {
return nil, nil
return nil, nil, errors.Wrapf(err, "get network %q free address count", net.GetName())
}
if used == 0 && !reserved && !options.Options.BaremetalServerReuseHostIp {
return nil, nil
return nil, nil, errors.Errorf("network %q out of usage", net.GetName())
}
matchNetIfs := make([]SNetInterface, 0)
netifs := self.GetNetInterfaces()
@@ -2478,9 +2478,9 @@ func (self *SHost) GetNetinterfacesWithIdAndCredential(netId string, userCred mc
}
}
if len(matchNetIfs) > 0 {
return matchNetIfs, net
return matchNetIfs, net, nil
}
return nil, nil
return nil, nil, errors.Errorf("not found matched netinterface by net %q wire %q", net.GetName(), net.WireId)
}
func (self *SHost) GetNetworkWithId(netId string, reserved bool) (*SNetwork, error) {