@@ -3,6 +3,7 @@ package scheduler
33import (
44 "context"
55 "fmt"
6+ "strings"
67 "time"
78
89 "github.com/go-logr/logr"
@@ -221,7 +222,15 @@ func (s *Scheduler) ScheduleOne(ctx context.Context) {
221222 return
222223 }
223224
224- result := framework .NewSchedulerResult (vmid , node )
225+ // select vm storage to be used for vm image
226+ // must be done after node selection as some storages may not be available on some nodes
227+ storage , err := s .SelectStorage (qemuCtx , * config , node )
228+ if err != nil {
229+ state .UpdateState (true , err , framework.SchedulerResult {})
230+ return
231+ }
232+
233+ result := framework .NewSchedulerResult (vmid , node , storage )
225234 state .UpdateState (true , nil , result )
226235}
227236
@@ -250,8 +259,8 @@ func (s *Scheduler) WaitStatus(ctx context.Context, config *api.VirtualMachineCr
250259
251260// create new qemu with given spec and context
252261func (s * Scheduler ) CreateQEMU (ctx context.Context , config * api.VirtualMachineCreateOptions ) (framework.SchedulerResult , error ) {
253- s . logger = s .logger .WithValues ("qemu" , config .Name )
254- s . logger .Info ("adding qemu to scheduler queue" )
262+ log : = s .logger .WithValues ("qemu" , config .Name )
263+ log .Info ("adding qemu to scheduler queue" )
255264 // add qemu spec into the queue
256265 s .schedulingQueue .Add (ctx , config )
257266
@@ -262,16 +271,16 @@ func (s *Scheduler) CreateQEMU(ctx context.Context, config *api.VirtualMachineCr
262271 return status .Result (), err
263272 }
264273 if status .Error () != nil {
265- s . logger .Error (status .Error (), fmt .Sprintf ("failed to create qemu: %v" , status .Messages ()))
274+ log .Error (status .Error (), fmt .Sprintf ("failed to create qemu: %v" , status .Messages ()))
266275 return status .Result (), status .Error ()
267276 }
268- s . logger .Info (fmt .Sprintf ("%v" , status .Messages ()))
277+ log .Info (fmt .Sprintf ("%v" , status .Messages ()))
269278 return status .Result (), nil
270279}
271280
272281func (s * Scheduler ) SelectNode (ctx context.Context , config api.VirtualMachineCreateOptions ) (string , error ) {
273282 s .logger .Info ("finding proxmox node matching qemu" )
274- nodes , err := s .client .Nodes (ctx )
283+ nodes , err := s .client .GetNodes (ctx )
275284 if err != nil {
276285 return "" , err
277286 }
@@ -316,6 +325,33 @@ func (s *Scheduler) SelectVMID(ctx context.Context, config api.VirtualMachineCre
316325 return s .RunVMIDPlugins (ctx , nil , config , nextid , * usedID )
317326}
318327
328+ func (s * Scheduler ) SelectStorage (ctx context.Context , config api.VirtualMachineCreateOptions , nodeName string ) (string , error ) {
329+ s .logger .Info ("finding proxmox storage to be used for qemu" )
330+ if config .Storage != "" {
331+ // to do: raise error if storage is not available on the node
332+ return config .Storage , nil
333+ }
334+
335+ node , err := s .client .Node (ctx , nodeName )
336+ if err != nil {
337+ return "" , err
338+ }
339+ storages , err := node .GetStorages (ctx )
340+ if err != nil {
341+ return "" , err
342+ }
343+
344+ // current logic is just selecting the first storage
345+ // that is active and supports "images" type of content
346+ for _ , storage := range storages {
347+ if strings .Contains (storage .Content , "images" ) && storage .Active == 1 {
348+ return storage .Storage , nil
349+ }
350+ }
351+
352+ return "" , fmt .Errorf ("no storage available for VM image on node %s" , nodeName )
353+ }
354+
319355func (s * Scheduler ) RunFilterPlugins (ctx context.Context , state * framework.CycleState , config api.VirtualMachineCreateOptions , nodes []* api.Node ) ([]* api.Node , error ) {
320356 s .logger .Info ("filtering proxmox node" )
321357 feasibleNodes := make ([]* api.Node , 0 , len (nodes ))
@@ -342,7 +378,7 @@ func (s *Scheduler) RunFilterPlugins(ctx context.Context, state *framework.Cycle
342378func (s * Scheduler ) RunScorePlugins (ctx context.Context , state * framework.CycleState , config api.VirtualMachineCreateOptions , nodes []* api.Node ) (framework.NodeScoreList , * framework.Status ) {
343379 s .logger .Info ("scoring proxmox node" )
344380 status := framework .NewStatus ()
345- var scoresMap map [string ](map [int ]framework.NodeScore )
381+ scoresMap := make ( map [string ](map [int ]framework.NodeScore ) )
346382 nodeInfos , err := framework .GetNodeInfoList (ctx , s .client )
347383 if err != nil {
348384 status .SetCode (1 )
0 commit comments