Allow creating VMs with implicit CPU and memory (#243)

* Allow creating VMs with implicit CPU and memory

* Clarify why cpu/memory can be 0 a bit better

* Controller(API): don't forget to update DefaultCPU and DefaultMemory

* Add an integration test for implicit CPU and memory
This commit is contained in:
Nikolay Edigaryev
2025-02-06 00:50:01 +04:00
committed by GitHub
parent 6b3d64be96
commit 581de320b9
10 changed files with 150 additions and 12 deletions
-6
View File
@@ -34,12 +34,6 @@ func (controller *Controller) createVM(ctx *gin.Context) responder.Responder {
if vm.Image == "" {
return responder.JSON(http.StatusPreconditionFailed, NewErrorResponse("VM image is empty"))
}
if vm.CPU == 0 {
return responder.JSON(http.StatusPreconditionFailed, NewErrorResponse("VM CPU is zero"))
}
if vm.Memory == 0 {
return responder.JSON(http.StatusPreconditionFailed, NewErrorResponse("VM memory is zero"))
}
vm.Status = v1.VMStatusPending
vm.CreatedAt = time.Now()
+2
View File
@@ -98,6 +98,8 @@ func (controller *Controller) createWorker(ctx *gin.Context) responder.Responder
dbWorker.LastSeen = worker.LastSeen
dbWorker.Resources = worker.Resources
dbWorker.DefaultCPU = worker.DefaultCPU
dbWorker.DefaultMemory = worker.DefaultMemory
if err := txn.SetWorker(*dbWorker); err != nil {
return responder.Error(err)
+27 -1
View File
@@ -134,7 +134,7 @@ func (scheduler *Scheduler) RequestScheduling() {
}
}
//nolint:gocognit // this logic could be said to be considered even more complex if split into multiple functions
//nolint:gocognit,gocyclo // this logic could be seen as even more complex if split into multiple functions
func (scheduler *Scheduler) schedulingLoopIteration() error {
affectedWorkers := mapset.NewSet[string]()
@@ -289,6 +289,30 @@ NextVM:
unscheduledVM.Worker = worker.Name
unscheduledVM.ScheduledAt = time.Now()
// Fill out the actual CPU allocation
if unscheduledVM.CPU == 0 {
// Provide defaults for VMs with implicit CPU specification
if worker.DefaultCPU != 0 {
unscheduledVM.AssignedCPU = worker.DefaultCPU
} else {
unscheduledVM.AssignedCPU = 4
}
} else {
unscheduledVM.AssignedCPU = unscheduledVM.CPU
}
// Fill out the actual memory allocation
if unscheduledVM.Memory == 0 {
// Provide defaults for VMs with implicit memory specification
if worker.DefaultMemory != 0 {
unscheduledVM.AssignedMemory = worker.DefaultMemory
} else {
unscheduledVM.AssignedMemory = 8192
}
} else {
unscheduledVM.AssignedMemory = unscheduledVM.Memory
}
if err := txn.SetVM(unscheduledVM); err != nil {
return err
}
@@ -434,6 +458,8 @@ func (scheduler *Scheduler) healthCheckVM(txn storepkg.Transaction, vm v1.VM) er
vm.Status = v1.VMStatusPending
vm.StatusMessage = ""
vm.Worker = ""
vm.AssignedCPU = 0
vm.AssignedMemory = 0
vm.RestartedAt = time.Now()
vm.RestartCount++
vm.ScheduledAt = time.Time{}