mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix: force assign ip address when sync vm vnics (#18852)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -28,7 +28,6 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
@@ -73,11 +72,11 @@ func (self *SVirtualizedGuestDriver) GetNamedNetworkConfiguration(guest *models.
|
||||
Ifname: "",
|
||||
})
|
||||
}
|
||||
reUse := false
|
||||
if len(netConfig.Address) > 0 && !options.Options.EnablePreAllocateIpAddr && !utils.IsInStringArray(host.GetProviderName(), []string{api.CLOUD_PROVIDER_ONECLOUD, api.CLOUD_PROVIDER_VMWARE, api.CLOUD_PROVIDER_CLOUDPODS}) {
|
||||
reUse = true
|
||||
}
|
||||
return net, nicConfs, api.IPAllocationStepdown, reUse, nil
|
||||
// reUse := false
|
||||
// if len(netConfig.Address) > 0 && !options.Options.EnablePreAllocateIpAddr && !utils.IsInStringArray(host.GetProviderName(), []string{api.CLOUD_PROVIDER_ONECLOUD, api.CLOUD_PROVIDER_VMWARE, api.CLOUD_PROVIDER_CLOUDPODS}) {
|
||||
// reUse = true
|
||||
// }
|
||||
return net, nicConfs, api.IPAllocationStepdown, false, nil
|
||||
}
|
||||
|
||||
func (self *SVirtualizedGuestDriver) GetRandomNetworkTypes() []string {
|
||||
|
||||
@@ -314,17 +314,24 @@ func (manager *SGuestnetworkManager) newGuestNetwork(
|
||||
// if reuse Ip address, no need to check address availability
|
||||
// assign it anyway
|
||||
gn.IpAddr = address
|
||||
} else if provider == api.CLOUD_PROVIDER_ONECLOUD || options.Options.EnablePreAllocateIpAddr {
|
||||
addrTable := network.GetUsedAddresses()
|
||||
recentAddrTable := manager.getRecentlyReleasedIPAddresses(network.Id, network.getAllocTimoutDuration())
|
||||
ipAddr, err := network.GetFreeIP(ctx, userCred, addrTable, recentAddrTable, address, allocDir, reserved)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
// 如果是不具备IPAM能力的平台(主要是OneCloud和VMware,也就是VPC为ONECLOUD的平台 provider == api.CLOUD_PROVIDER_ONECLOUD ),则需要分配IP地址
|
||||
// 如果是其他云平台(具体IPAM能力的平台),则
|
||||
// * 开启options.Options.EnablePreAllocateIpAddr,也就是把IPAM的任务交给平台的,则需要分配IP地址
|
||||
// * IP地址为空并且 !options.Options.EnablePreAllocateIpAddr 时,不需要分配IP,等创建后自动同步过来
|
||||
// * 否则,还是需要先分配了
|
||||
if provider == api.CLOUD_PROVIDER_ONECLOUD || options.Options.EnablePreAllocateIpAddr || (!options.Options.EnablePreAllocateIpAddr && len(address) > 0) {
|
||||
addrTable := network.GetUsedAddresses()
|
||||
recentAddrTable := manager.getRecentlyReleasedIPAddresses(network.Id, network.getAllocTimoutDuration())
|
||||
ipAddr, err := network.GetFreeIP(ctx, userCred, addrTable, recentAddrTable, address, allocDir, reserved)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(address) > 0 && ipAddr != address && requiredDesignatedIp {
|
||||
return nil, fmt.Errorf("candidate ip %s is occupied!", address)
|
||||
}
|
||||
gn.IpAddr = ipAddr
|
||||
}
|
||||
if len(address) > 0 && ipAddr != address && requiredDesignatedIp {
|
||||
return nil, fmt.Errorf("candidate ip %s is occupied!", address)
|
||||
}
|
||||
gn.IpAddr = ipAddr
|
||||
}
|
||||
|
||||
if vpc.Id != api.DEFAULT_VPC_ID && provider == api.CLOUD_PROVIDER_ONECLOUD {
|
||||
|
||||
@@ -3663,8 +3663,8 @@ func (self *SGuest) SyncVMNics(
|
||||
TryReserved: true,
|
||||
AllocDir: api.IPAllocationDefault,
|
||||
RequireDesignatedIP: true,
|
||||
UseDesignatedIP: false,
|
||||
NicConfs: []SNicConfig{nicConf},
|
||||
// UseDesignatedIP: true,
|
||||
NicConfs: []SNicConfig{nicConf},
|
||||
})
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
|
||||
@@ -125,6 +125,7 @@ type ComputeOptions struct {
|
||||
// cloud image sync
|
||||
CloudImagesSyncIntervalHours int `default:"3" help:"Interval to sync public cloud image, defualt is 3 hour"`
|
||||
|
||||
// 由云管(Cloudpods)负责分配IP地址,默认为false。默认是由对应具备IPAM能力的云平台自主分配IP地址
|
||||
EnablePreAllocateIpAddr bool `help:"Enable private and public cloud private ip pre allocate, default false" default:"false"`
|
||||
|
||||
// 创建虚拟机失败后, 自动使用其他相同配置套餐
|
||||
|
||||
Reference in New Issue
Block a user