Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0900359

Browse files
committedFeb 1, 2025
select storage for vm image after node selection so to not pick unavailable storage
1 parent 469028b commit 0900359

File tree

9 files changed

+69
-72
lines changed

9 files changed

+69
-72
lines changed
 

‎cloud/scheduler/framework/cycle_state.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ type CycleState struct {
88
}
99

1010
type SchedulerResult struct {
11-
vmid int
12-
node string
11+
vmid int
12+
node string
13+
storage string
1314
}
1415

1516
func NewCycleState() CycleState {
@@ -46,8 +47,8 @@ func (c *CycleState) UpdateState(completed bool, err error, result SchedulerResu
4647
c.result = result
4748
}
4849

49-
func NewSchedulerResult(vmid int, node string) SchedulerResult {
50-
return SchedulerResult{vmid: vmid, node: node}
50+
func NewSchedulerResult(vmid int, node string, storage string) SchedulerResult {
51+
return SchedulerResult{vmid: vmid, node: node, storage: storage}
5152
}
5253

5354
func (c *CycleState) Result() SchedulerResult {
@@ -61,3 +62,7 @@ func (r *SchedulerResult) Node() string {
6162
func (r *SchedulerResult) VMID() int {
6263
return r.vmid
6364
}
65+
66+
func (r *SchedulerResult) Storage() string {
67+
return r.storage
68+
}

‎cloud/scheduler/framework/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type NodeInfo struct {
5858
}
5959

6060
func GetNodeInfoList(ctx context.Context, client *proxmox.Service) ([]*NodeInfo, error) {
61-
nodes, err := client.Nodes(ctx)
61+
nodes, err := client.GetNodes(ctx)
6262
if err != nil {
6363
return nil, err
6464
}

‎cloud/scheduler/scheduler.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scheduler
33
import (
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

@@ -271,7 +280,7 @@ func (s *Scheduler) CreateQEMU(ctx context.Context, config *api.VirtualMachineCr
271280

272281
func (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+
319355
func (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))

‎cloud/services/compute/instance/cloudinit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (s *Service) deleteCloudConfig(ctx context.Context) error {
3737
path := userSnippetPath(s.scope.Name())
3838
volumeID := fmt.Sprintf("%s:%s", storageName, path)
3939

40-
node, err := s.client.Node(ctx, s.scope.NodeName())
40+
node, err := s.client.GetNode(ctx, s.scope.NodeName())
4141
if err != nil {
4242
return err
4343
}

‎cloud/services/compute/instance/qemu.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, erro
6767
log := log.FromContext(ctx)
6868
log.Info("creating qemu")
6969

70-
if err := s.ensureStorageAvailable(ctx); err != nil {
71-
return nil, err
72-
}
73-
7470
// create qemu
7571
log.Info("making qemu spec")
7672
vmoption := s.generateVMOptions()
@@ -81,10 +77,14 @@ func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, erro
8177
log.Error(err, "failed to schedule qemu instance")
8278
return nil, err
8379
}
84-
node, vmid := result.Node(), result.VMID()
80+
node, vmid, storage := result.Node(), result.VMID(), result.Storage()
8581
s.scope.SetNodeName(node)
8682
s.scope.SetVMID(vmid)
8783

84+
// inject storage
85+
s.injectVMOption(&vmoption, storage)
86+
s.scope.SetStorage(storage)
87+
8888
// os image
8989
if err := s.setCloudImage(ctx); err != nil {
9090
return nil, err
@@ -164,3 +164,14 @@ func boolToInt8(b bool) int8 {
164164
}
165165
return 0
166166
}
167+
168+
func (s *Service) injectVMOption(vmOption *api.VirtualMachineCreateOptions, storage string) *api.VirtualMachineCreateOptions {
169+
// storage is finalized after node scheduling so we need to inject storage name here
170+
ide2 := fmt.Sprintf("file=%s:cloudinit,media=cdrom", storage)
171+
scsi0 := fmt.Sprintf("%s:0,import-from=%s", storage, rawImageFilePath(s.scope.GetImage()))
172+
vmOption.Scsi.Scsi0 = scsi0
173+
vmOption.Ide.Ide2 = ide2
174+
vmOption.Storage = storage
175+
176+
return vmOption
177+
}

‎cloud/services/compute/instance/storage.go

-55
This file was deleted.

‎cloud/services/compute/storage/reconcile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *Service) deleteStorage(ctx context.Context) error {
7676
return err
7777
}
7878

79-
nodes, err := s.client.Nodes(ctx)
79+
nodes, err := s.client.GetNodes(ctx)
8080
if err != nil {
8181
return err
8282
}

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require (
66
github.com/go-logr/logr v1.3.0
77
github.com/imdario/mergo v0.3.13
8-
github.com/k8s-proxmox/proxmox-go v0.0.0-alpha28
8+
github.com/k8s-proxmox/proxmox-go v0.0.0-alpha30
99
github.com/onsi/ginkgo/v2 v2.13.2
1010
github.com/onsi/gomega v1.30.0
1111
github.com/pkg/errors v0.9.1

‎go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
301301
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
302302
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
303303
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
304-
github.com/k8s-proxmox/proxmox-go v0.0.0-alpha28 h1:h0PwVITcljicpXCmMcOyXeXWhkVeYBiK4F2A/Ch5dxg=
305-
github.com/k8s-proxmox/proxmox-go v0.0.0-alpha28/go.mod h1:ZSAdc9vVAEcIhbNkZxURWTY+k59cXUy9mswp5ofMM40=
304+
github.com/k8s-proxmox/proxmox-go v0.0.0-alpha30 h1:xwA4cEZVjaShetPErsN/z+CHUA4jE8HhIRQ9d345WsM=
305+
github.com/k8s-proxmox/proxmox-go v0.0.0-alpha30/go.mod h1:ZSAdc9vVAEcIhbNkZxURWTY+k59cXUy9mswp5ofMM40=
306306
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
307307
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
308308
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

0 commit comments

Comments
 (0)
Please sign in to comment.