From 8237695a89b60e890863933584601aea0aebc3f8 Mon Sep 17 00:00:00 2001 From: sp-yduck Date: Thu, 25 Apr 2024 12:44:47 +0000 Subject: [PATCH] fix kvm image not found err: set os image before actual qemu creation --- cloud/scheduler/framework/cycle_state.go | 22 ++++------------------ cloud/scheduler/scheduler.go | 9 +-------- cloud/scheduler/scheduler_test.go | 4 ++-- cloud/services/compute/instance/image.go | 5 ----- cloud/services/compute/instance/qemu.go | 19 +++++++++++++++++-- 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/cloud/scheduler/framework/cycle_state.go b/cloud/scheduler/framework/cycle_state.go index f3d6d86..859e6d9 100644 --- a/cloud/scheduler/framework/cycle_state.go +++ b/cloud/scheduler/framework/cycle_state.go @@ -1,10 +1,5 @@ package framework -import ( - "github.com/k8s-proxmox/proxmox-go/api" - "github.com/k8s-proxmox/proxmox-go/proxmox" -) - type CycleState struct { completed bool err error @@ -13,9 +8,8 @@ type CycleState struct { } type SchedulerResult struct { - vmid int - node string - instance *proxmox.VirtualMachine + vmid int + node string } func NewCycleState() CycleState { @@ -46,18 +40,14 @@ func (c *CycleState) Messages() map[string]string { return c.messages } -func (c *CycleState) QEMU() *api.VirtualMachine { - return c.result.instance.VM -} - func (c *CycleState) UpdateState(completed bool, err error, result SchedulerResult) { c.completed = completed c.err = err c.result = result } -func NewSchedulerResult(vmid int, node string, instance *proxmox.VirtualMachine) SchedulerResult { - return SchedulerResult{vmid: vmid, node: node, instance: instance} +func NewSchedulerResult(vmid int, node string) SchedulerResult { + return SchedulerResult{vmid: vmid, node: node} } func (c *CycleState) Result() SchedulerResult { @@ -71,7 +61,3 @@ func (r *SchedulerResult) Node() string { func (r *SchedulerResult) VMID() int { return r.vmid } - -func (r *SchedulerResult) Instance() *proxmox.VirtualMachine { - return r.instance -} diff --git a/cloud/scheduler/scheduler.go b/cloud/scheduler/scheduler.go index 5cf9747..ba35b2a 100644 --- a/cloud/scheduler/scheduler.go +++ b/cloud/scheduler/scheduler.go @@ -221,14 +221,7 @@ func (s *Scheduler) ScheduleOne(ctx context.Context) { return } - // actually create qemu - vm, err := s.client.CreateVirtualMachine(ctx, node, vmid, *config) - if err != nil { - state.UpdateState(true, err, framework.SchedulerResult{}) - return - } - - result := framework.NewSchedulerResult(vmid, node, vm) + result := framework.NewSchedulerResult(vmid, node) state.UpdateState(true, nil, result) } diff --git a/cloud/scheduler/scheduler_test.go b/cloud/scheduler/scheduler_test.go index ae5004e..0b8a324 100644 --- a/cloud/scheduler/scheduler_test.go +++ b/cloud/scheduler/scheduler_test.go @@ -115,8 +115,8 @@ var _ = Describe("CreateQEMU", Label("integration"), func() { }) Expect(err).ToNot(HaveOccurred()) Expect(result).ToNot(BeNil()) - Expect(result.Node()).To(Equal(result.Instance().Node)) - Expect(result.VMID()).To(Equal(result.Instance().VM.VMID)) + Expect(result.Node()).ToNot(BeNil()) + Expect(result.VMID()).ToNot(BeNil()) }) }) }) diff --git a/cloud/services/compute/instance/image.go b/cloud/services/compute/instance/image.go index 0b33d8d..d5d9b32 100644 --- a/cloud/services/compute/instance/image.go +++ b/cloud/services/compute/instance/image.go @@ -22,11 +22,6 @@ func (s *Service) reconcileBootDevice(ctx context.Context, vm *proxmox.VirtualMa log := log.FromContext(ctx) log.Info("reconciling boot device") - // os image - if err := s.setCloudImage(ctx); err != nil { - return err - } - // boot disk log.Info("resizing boot disk") if err := vm.ResizeVolume(ctx, bootDvice, s.scope.GetHardware().Disk); err != nil { diff --git a/cloud/services/compute/instance/qemu.go b/cloud/services/compute/instance/qemu.go index 7d5d9db..037a1bc 100644 --- a/cloud/services/compute/instance/qemu.go +++ b/cloud/services/compute/instance/qemu.go @@ -78,10 +78,25 @@ func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, erro schedCtx := framework.ContextWithMap(ctx, s.scope.Annotations()) result, err := s.scheduler.CreateQEMU(schedCtx, &vmoption) if err != nil { - log.Error(err, "failed to create qemu instance") + log.Error(err, "failed to schedule qemu instance") return nil, err } - return result.Instance(), nil + node, vmid := result.Node(), result.VMID() + s.scope.SetNodeName(node) + s.scope.SetVMID(vmid) + + // os image + if err := s.setCloudImage(ctx); err != nil { + return nil, err + } + + // actually create qemu + vm, err := s.client.CreateVirtualMachine(ctx, node, vmid, vmoption) + if err != nil { + return nil, err + } + + return vm, nil } func (s *Service) generateVMOptions() api.VirtualMachineCreateOptions {