Skip to content

Commit

Permalink
fix(region): cpu socket (#18731)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Nov 21, 2023
1 parent 1d9a291 commit 2e1c5fa
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 52 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ require (
k8s.io/client-go v0.19.3
k8s.io/cluster-bootstrap v0.19.3
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231117073513-e4f90b4cabe4
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231120065010-32185e2427ba
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
yunion.io/x/jsonutils v1.0.1-0.20230613121553-0f3b41e2ef19
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1192,8 +1192,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231117073513-e4f90b4cabe4 h1:cMkp0GGx770vEryaMBZLWdcR3lhmScM+FbE6XWmqEZU=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231117073513-e4f90b4cabe4/go.mod h1:aj1gR9PPb6eqqKOwvANe26CoZFY8ydmXy0fuvgKYXH0=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231120065010-32185e2427ba h1:+q78oFUtoFfoIwP6KRSBE3EGX4QRgZNjwIqNnfXCQs8=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231120065010-32185e2427ba/go.mod h1:aj1gR9PPb6eqqKOwvANe26CoZFY8ydmXy0fuvgKYXH0=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32 h1:v7POYkQwo1XzOxBoIoRVr/k0V9Y5JyjpshlIFa9raug=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/compute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ type ServerCreateInput struct {
// default: 1
VcpuCount int `json:"vcpu_count"`

// cpu卡槽数
// 目前仅vmware支持此参数
// default: 1
CpuSockets int `json:"cpu_sockets"`

// 用户自定义启动脚本
// 支持 #cloud-config yaml 格式及shell脚本
// 支持特殊user data平台: Aliyun, Qcloud, Azure, Apsara, Ucloud
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/compute/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,8 @@ type ServerChangeConfigInput struct {
// swagger: ignore
Flavor string `json:"flavor" yunion-deprecated-by:"instance_type"`

// cpu卡槽数
CpuSockets *int `json:"cpu_sockets"`
// cpu大小
VcpuCount int `json:"vcpu_count"`
// 内存大小, 1024M, 1G
Expand Down Expand Up @@ -809,6 +811,7 @@ type GuestJsonDesc struct {
Description string `json:"description"`
UUID string `json:"uuid"`
Mem int `json:"mem"`
CpuSockets int `json:"cpu_sockets"`
Cpu int `json:"cpu"`
Vga string `json:"vga"`
Vdi string `json:"vdi"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/compute/guestdrivers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ func (drv *SBaseGuestDriver) DoGuestCreateDisksTask(ctx context.Context, guest *
return fmt.Errorf("Not Implement")
}

func (drv *SBaseGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, vmemSize int64) error {
return fmt.Errorf("Not Implement")
func (drv *SBaseGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, cpuSockets, vmemSize int64) error {
return errors.Wrapf(cloudprovider.ErrNotImplemented, "RequestChangeVmConfig")
}

func (drv *SBaseGuestDriver) NeedRequestGuestHotAddIso(ctx context.Context, guest *models.SGuest) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (self *SContainerDriver) DoGuestCreateDisksTask(ctx context.Context, guest
return self.newUnsupportOperationError("create disk")
}

func (self *SContainerDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, vmemSize int64) error {
func (self *SContainerDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, cpuSockets, vmemSize int64) error {
return self.newUnsupportOperationError("change config")
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/compute/guestdrivers/esxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ func (self *SESXiGuestDriver) ValidateCreateData(ctx context.Context, userCred m
data.Disks[i].Format = "vmdk"
}

if data.CpuSockets > data.VcpuCount {
return nil, httperrors.NewInputParameterError("The number of cpu sockets cannot be greater than the number of cpus")
}

// check disk config
if len(data.Disks) == 0 {
return data, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (self *SKVMGuestDriver) NeedStopForChangeSpec(ctx context.Context, guest *m
// apis.IsARM(guest.OsArch)
}

func (self *SKVMGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, vmemSize int64) error {
func (self *SKVMGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, cpuSockets, vmemSize int64) error {
if jsonutils.QueryBoolean(task.GetParams(), "guest_online", false) {
addCpu := vcpuCount - int64(guest.VcpuCount)
addMem := vmemSize - int64(guest.VmemSize)
Expand Down
20 changes: 11 additions & 9 deletions pkg/compute/guestdrivers/managedvirtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,8 +989,11 @@ func (drv *SManagedVirtualizedGuestDriver) DoGuestCreateDisksTask(ctx context.Co
return nil
}

func (drv *SManagedVirtualizedGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, vmemSize int64) error {
host, _ := guest.GetHost()
func (drv *SManagedVirtualizedGuestDriver) RequestChangeVmConfig(ctx context.Context, guest *models.SGuest, task taskman.ITask, instanceType string, vcpuCount, cpuSockets, vmemSize int64) error {
host, err := guest.GetHost()
if err != nil {
return errors.Wrapf(err, "GetHost")
}
ihost, err := host.GetIHost(ctx)
if err != nil {
return err
Expand All @@ -1002,22 +1005,21 @@ func (drv *SManagedVirtualizedGuestDriver) RequestChangeVmConfig(ctx context.Con
}

if len(instanceType) == 0 {
region, _ := host.GetRegion()
sku, err := models.ServerSkuManager.GetMatchedSku(region.GetId(), vcpuCount, vmemSize)
region, err := host.GetRegion()
if err != nil {
return errors.Wrap(err, "ManagedVirtualizedGuestDriver.RequestChangeVmConfig.GetMatchedSku")
return err
}

if sku == nil {
return errors.Wrap(errors.ErrNotFound, "ManagedVirtualizedGuestDriver.RequestChangeVmConfig.GetMatchedSku")
sku, err := models.ServerSkuManager.GetMatchedSku(region.GetId(), vcpuCount, vmemSize)
if err != nil {
return errors.Wrapf(err, "GetMatchedSku %s %dC%dM", region.GetId(), vcpuCount, vmemSize)
}

instanceType = sku.Name
}

taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) {
config := &cloudprovider.SManagedVMChangeConfig{
Cpu: int(vcpuCount),
CpuSocket: int(cpuSockets),
MemoryMB: int(vmemSize),
InstanceType: instanceType,
}
Expand Down
38 changes: 24 additions & 14 deletions pkg/compute/models/guest_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2697,13 +2697,14 @@ func (self *SGuest) PerformChangeConfig(ctx context.Context, userCred mcclient.T
}

var addCpu, addMem int
var cpuChanged, memChanged bool
var cpuChanged, cpuSocketsChanged, memChanged bool

confs := jsonutils.NewDict()
confs.Add(jsonutils.Marshal(map[string]interface{}{
"instance_type": self.InstanceType,
"vcpu_count": self.VcpuCount,
"vmem_size": self.VmemSize,
"cpu_sockets": self.CpuSockets,
}), "old")
if len(input.InstanceType) > 0 {
sku, err := ServerSkuManager.FetchSkuByNameAndProvider(input.InstanceType, self.GetDriver().GetProvider(), true)
Expand Down Expand Up @@ -2735,24 +2736,33 @@ func (self *SGuest) PerformChangeConfig(ctx context.Context, userCred mcclient.T
addCpu = input.VcpuCount - self.VcpuCount
confs.Add(jsonutils.NewInt(int64(input.VcpuCount)), "vcpu_count")
}
if !regutils.MatchSize(input.VmemSize) {
return nil, httperrors.NewBadRequestError("Memory size %q must be number[+unit], like 256M, 1G or 256", input.VmemSize)
}
nVmem, err := fileutils.GetSizeMb(input.VmemSize, 'M', 1024)
if err != nil {
httperrors.NewBadRequestError("Params vmem_size parse error")
}
if nVmem != self.VmemSize {
memChanged = true
addMem = nVmem - self.VmemSize
err = confs.Add(jsonutils.NewInt(int64(nVmem)), "vmem_size")
if len(input.VmemSize) > 0 {
if !regutils.MatchSize(input.VmemSize) {
return nil, httperrors.NewBadRequestError("Memory size %q must be number[+unit], like 256M, 1G or 256", input.VmemSize)
}
nVmem, err := fileutils.GetSizeMb(input.VmemSize, 'M', 1024)
if err != nil {
return nil, httperrors.NewBadRequestError("Params vmem_size parse error")
httperrors.NewBadRequestError("Params vmem_size parse error")
}
if nVmem != self.VmemSize {
memChanged = true
addMem = nVmem - self.VmemSize
err = confs.Add(jsonutils.NewInt(int64(nVmem)), "vmem_size")
if err != nil {
return nil, httperrors.NewBadRequestError("Params vmem_size parse error")
}
}
}
}
if input.CpuSockets != nil && *input.CpuSockets != self.CpuSockets {
if *input.CpuSockets > self.VcpuCount+addCpu {
return nil, httperrors.NewInputParameterError("The number of cpu sockets cannot be greater than the number of cpus")
}
cpuSocketsChanged = true
confs.Set("cpu_sockets", jsonutils.NewInt(int64(*input.CpuSockets)))
}

if self.Status == api.VM_RUNNING && (cpuChanged || memChanged) && self.GetDriver().NeedStopForChangeSpec(ctx, self, cpuChanged, memChanged) {
if self.Status == api.VM_RUNNING && (cpuChanged || memChanged || cpuSocketsChanged) && self.GetDriver().NeedStopForChangeSpec(ctx, self, cpuChanged, memChanged) {
return nil, httperrors.NewInvalidStatusError("cannot change CPU/Memory spec in status %s", self.Status)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/guestdrivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type IGuestDriver interface {

AllowReconfigGuest() bool
DoGuestCreateDisksTask(ctx context.Context, guest *SGuest, task taskman.ITask) error
RequestChangeVmConfig(ctx context.Context, guest *SGuest, task taskman.ITask, instanceType string, vcpuCount, vmemSize int64) error
RequestChangeVmConfig(ctx context.Context, guest *SGuest, task taskman.ITask, instanceType string, vcpuCount, cpuSockets, vmemSize int64) error

NeedRequestGuestHotAddIso(ctx context.Context, guest *SGuest) bool
RequestGuestHotAddIso(ctx context.Context, guest *SGuest, path string, boot bool, task taskman.ITask) error
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 @@ -121,6 +121,8 @@ type SGuest struct {

db.SEncryptedResource

// CPU大小
CpuSockets int `nullable:"false" default:"1" list:"user" create:"optional"`
// CPU大小
VcpuCount int `nullable:"false" default:"1" list:"user" create:"optional"`
// 内存大小, 单位MB
Expand Down Expand Up @@ -3015,6 +3017,7 @@ func (g *SGuest) syncWithCloudVM(ctx context.Context, userCred mcclient.TokenCre
}

g.VcpuCount = extVM.GetVcpuCount()
g.CpuSockets = extVM.GetCpuSockets()
g.BootOrder = extVM.GetBootOrder()
g.Vga = extVM.GetVga()
g.Vdi = extVM.GetVdi()
Expand Down Expand Up @@ -3108,6 +3111,7 @@ func (manager *SGuestManager) newCloudVM(ctx context.Context, userCred mcclient.
guest.InferPowerStates()
guest.ExternalId = extVM.GetGlobalId()
guest.VcpuCount = extVM.GetVcpuCount()
guest.CpuSockets = extVM.GetCpuSockets()
guest.BootOrder = extVM.GetBootOrder()
guest.Vga = extVM.GetVga()
guest.Vdi = extVM.GetVdi()
Expand Down Expand Up @@ -4911,6 +4915,7 @@ func (self *SGuest) GetJsonDescAtHypervisor(ctx context.Context, host *SHost) *a
UUID: self.Id,
Mem: self.VmemSize,
Cpu: self.VcpuCount,
CpuSockets: self.CpuSockets,
Vga: self.getVga(),
Vdi: self.GetVdi(),
Machine: self.getMachine(),
Expand Down
13 changes: 9 additions & 4 deletions pkg/compute/tasks/guest_change_config_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ func (task *GuestChangeConfigTask) OnCreateDisksCompleteFailed(ctx context.Conte
func (task *GuestChangeConfigTask) OnCreateDisksComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
guest := obj.(*models.SGuest)

if task.Params.Contains("instance_type") || task.Params.Contains("vcpu_count") || task.Params.Contains("vmem_size") {
if task.Params.Contains("instance_type") || task.Params.Contains("vcpu_count") || task.Params.Contains("vmem_size") || task.Params.Contains("cpu_sockets") {
task.SetStage("OnGuestChangeCpuMemSpecComplete", nil)
instanceType, _ := task.Params.GetString("instance_type")
vcpuCount, _ := task.Params.Int("vcpu_count")
vmemSize, _ := task.Params.Int("vmem_size")
cpuSockets, _ := task.Params.Int("cpu_sockets")
if len(instanceType) > 0 {
provider := guest.GetDriver().GetProvider()
sku, err := models.ServerSkuManager.FetchSkuByNameAndProvider(instanceType, provider, false)
Expand All @@ -216,14 +217,14 @@ func (task *GuestChangeConfigTask) OnCreateDisksComplete(ctx context.Context, ob
vmemSize = int64(guest.VmemSize)
}
}
task.startGuestChangeCpuMemSpec(ctx, guest, instanceType, vcpuCount, vmemSize)
task.startGuestChangeCpuMemSpec(ctx, guest, instanceType, vcpuCount, cpuSockets, vmemSize)
} else {
task.OnGuestChangeCpuMemSpecComplete(ctx, obj, data)
}
}

func (task *GuestChangeConfigTask) startGuestChangeCpuMemSpec(ctx context.Context, guest *models.SGuest, instanceType string, vcpuCount int64, vmemSize int64) {
err := guest.GetDriver().RequestChangeVmConfig(ctx, guest, task, instanceType, vcpuCount, vmemSize)
func (task *GuestChangeConfigTask) startGuestChangeCpuMemSpec(ctx context.Context, guest *models.SGuest, instanceType string, vcpuCount, cpuSockets int64, vmemSize int64) {
err := guest.GetDriver().RequestChangeVmConfig(ctx, guest, task, instanceType, vcpuCount, cpuSockets, vmemSize)
if err != nil {
task.markStageFailed(ctx, guest, jsonutils.NewString(err.Error()))
return
Expand All @@ -235,6 +236,7 @@ func (task *GuestChangeConfigTask) OnGuestChangeCpuMemSpecComplete(ctx context.C

instanceType, _ := task.Params.GetString("instance_type")
vcpuCount, _ := task.Params.Int("vcpu_count")
cpuSockets, _ := task.Params.Int("cpu_sockets")
vmemSize, _ := task.Params.Int("vmem_size")

if len(instanceType) == 0 {
Expand All @@ -251,6 +253,9 @@ func (task *GuestChangeConfigTask) OnGuestChangeCpuMemSpecComplete(ctx context.C
if vcpuCount > 0 {
guest.VcpuCount = int(vcpuCount)
}
if cpuSockets > 0 {
guest.CpuSockets = int(cpuSockets)
}
if vmemSize > 0 {
guest.VmemSize = int(vmemSize)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/mcclient/options/compute/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ type ServerCreateOptionalOptions struct {
ServerConfigs

MemSpec string `help:"Memory size Or Instance Type" metavar:"MEMSPEC" json:"-"`
CpuSockets int `help:"Cpu sockets"`
EnableMemclean bool `help:"clean guest memory after guest exit" json:"enable_memclean"`

Keypair string `help:"SSH Keypair"`
Expand Down Expand Up @@ -996,9 +997,10 @@ func (o *ServerRebuildRootOptions) Description() string {

type ServerChangeConfigOptions struct {
ServerIdOptions
VcpuCount *int `help:"New number of Virtual CPU cores" json:"vcpu_count" token:"ncpu"`
VmemSize string `help:"New memory size" json:"vmem_size" token:"vmem"`
Disk []string `help:"Data disk description, from the 1st data disk to the last one, empty string if no change for this data disk"`
VcpuCount *int `help:"New number of Virtual CPU cores" json:"vcpu_count" token:"ncpu"`
CpuSockets *int `help:"Cpu sockets"`
VmemSize string `help:"New memory size" json:"vmem_size" token:"vmem"`
Disk []string `help:"Data disk description, from the 1st data disk to the last one, empty string if no change for this data disk"`

InstanceType string `help:"Instance Type, e.g. S2.SMALL2 for qcloud"`

Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
## explicit; go 1.12
sigs.k8s.io/yaml
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231117073513-e4f90b4cabe4
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20231120065010-32185e2427ba
## explicit; go 1.18
yunion.io/x/cloudmux/pkg/apis
yunion.io/x/cloudmux/pkg/apis/billing
Expand Down
1 change: 1 addition & 0 deletions vendor/yunion.io/x/cloudmux/pkg/cloudprovider/instance.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/yunion.io/x/cloudmux/pkg/cloudprovider/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions vendor/yunion.io/x/cloudmux/pkg/multicloud/esxi/host.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e1c5fa

Please sign in to comment.