Skip to content

Commit

Permalink
Merge pull request #150 from k8s-proxmox/fix/147
Browse files Browse the repository at this point in the history
stop creating qemu if there is already same name qemu
  • Loading branch information
sp-yduck authored Dec 6, 2023
2 parents 2fcb0de + 98badc9 commit 808d372
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions cloud/services/compute/instance/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,35 @@ func (s *Service) reconcileQEMU(ctx context.Context) (*proxmox.VirtualMachine, e
log.Info("Reconciling QEMU")

qemu, err := s.getQEMU(ctx)
if err == nil { // if qemu is found, return it
return qemu, nil
if err != nil {
if !rest.IsNotFound(err) {
log.Error(err, "failed to get qemu")
return nil, err
}

// no qemu found, try to create new one
log.V(3).Info("qemu wasn't found. new qemu will be created")
if exist, err := s.client.VirtualMachineExistsWithName(ctx, s.scope.Name()); exist || err != nil {
if exist {
// there should no qemu with same name. occuring an error
err = fmt.Errorf("qemu %s already exists", s.scope.Name())
}
log.Error(err, "stop creating new qemu to avoid replicating same qemu")
return nil, err
}
qemu, err = s.createQEMU(ctx)
if err != nil {
log.Error(err, "failed to create qemu")
return nil, err
}
}
if !rest.IsNotFound(err) {
log.Error(err, "failed to get qemu")

s.scope.SetVMID(qemu.VM.VMID)
s.scope.SetNodeName(qemu.Node)
if err := s.scope.PatchObject(); err != nil {
return nil, err
}

// no qemu found, create new one
return s.createQEMU(ctx)
return qemu, nil
}

// get QEMU gets proxmox vm from vmid
Expand Down Expand Up @@ -62,12 +81,6 @@ func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, erro
log.Error(err, "failed to create qemu instance")
return nil, err
}

s.scope.SetNodeName(result.Node())
s.scope.SetVMID(result.VMID())
if err := s.scope.PatchObject(); err != nil {
return nil, err
}
return result.Instance(), nil
}

Expand Down

0 comments on commit 808d372

Please sign in to comment.