fix(region,host,scheduler): add check kvm cpu max on cpu predicate (#23538)

This commit is contained in:
wanyaoqi
2025-10-22 15:07:22 +08:00
committed by GitHub
parent b352d0cc92
commit 35e34f019a
8 changed files with 22 additions and 1 deletions
+2
View File
@@ -467,6 +467,8 @@ type HostSizeAttributes struct {
CpuMicrocode string `json:"cpu_microcode"`
// CPU架构
CpuArchitecture string `json:"cpu_architecture"`
// KVM 允许单台虚机最大 vcpu 个数
KvmCapMaxVcpu *int `json:"kvm_cap_max_vcpu"`
// 内存大小(单位MB)
MemSize string `json:"mem_size"`
+2
View File
@@ -146,6 +146,8 @@ type SHost struct {
CpuMicrocode string `width:"64" charset:"ascii" nullable:"true" get:"domain" update:"domain" create:"domain_optional"`
// CPU架构
CpuArchitecture string `width:"16" charset:"ascii" nullable:"true" get:"domain" list:"domain" update:"domain" create:"domain_optional"`
// KVM CAP VCPU MAX
KvmCapMaxVcpu int `nullable:"true" get:"domain" list:"domain" update:"domain" create:"domain_optional"`
// 内存大小,单位Mb
MemSize int `nullable:"true" list:"domain" update:"domain" create:"domain_optional"`
+2
View File
@@ -1617,6 +1617,8 @@ func (h *SHostInfo) updateOrCreateHost(hostId string) (*api.HostDetails, error)
input.CpuDesc = h.Cpu.cpuInfoProc.Model
input.CpuMicrocode = h.Cpu.cpuInfoProc.Microcode
input.CpuArchitecture = h.Cpu.CpuArchitecture
maxVcpu := int(h.GetKVMMaxCpus())
input.KvmCapMaxVcpu = &maxVcpu
if h.Cpu.cpuInfoProc.Freq > 0 {
input.CpuMhz = &h.Cpu.cpuInfoProc.Freq
@@ -45,6 +45,7 @@ const (
ErrBaremetalHasAlreadyBeenOccupied = `baremetal has already been occupied`
ErrPrepaidHostOccupied = `prepaid host occupied`
ErrHostCpuArchitectureNotMatch = `host cpu architecture not match`
ErrHostKvmVcpuMaxNotEnough = `host kvm vcpu max not enough`
ErrUnknown = `unknown error`
)
@@ -80,8 +80,13 @@ func (f *CPUPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candida
return h.GetResult()
}
freeCPUCount := getter.FreeCPUCount(useRsvd)
reqCPUCount := int64(d.Ncpu + d.ExtraCpuCount)
if getter.KvmCapMaxVcpuCount() > 0 && reqCPUCount > getter.KvmCapMaxVcpuCount() {
h.Exclude2(predicates.ErrHostKvmVcpuMaxNotEnough, getter.KvmCapMaxVcpuCount(), reqCPUCount)
return h.GetResult()
}
freeCPUCount := getter.FreeCPUCount(useRsvd)
if freeCPUCount < reqCPUCount {
totalCPUCount := getter.TotalCPUCount(useRsvd)
h.AppendInsufficientResourceError(reqCPUCount, totalCPUCount, freeCPUCount)
+4
View File
@@ -96,6 +96,10 @@ func (b baseHostGetter) CPUArch() string {
return b.h.CpuArchitecture
}
func (b baseHostGetter) KvmCapMaxVcpuCount() int64 {
return int64(b.h.KvmCapMaxVcpu)
}
func (b baseHostGetter) Cloudprovider() *computemodels.SCloudprovider {
return b.h.Cloudprovider
}
+1
View File
@@ -93,6 +93,7 @@ type CandidatePropertyGetter interface {
RunningCPUCount() int64
TotalCPUCount(useRsvd bool) int64
FreeCPUCount(useRsvd bool) int64
KvmCapMaxVcpuCount() int64
RunningMemorySize() int64
TotalMemorySize(useRsvd bool) int64
+4
View File
@@ -318,6 +318,10 @@ func (m *MockCandidatePropertyGetter) CPUArch() string {
return ""
}
func (m *MockCandidatePropertyGetter) KvmCapMaxVcpuCount() int64 {
return -1
}
// Host indicates an expected call of Host
func (mr *MockCandidatePropertyGetterMockRecorder) Host() *gomock.Call {
mr.mock.ctrl.T.Helper()