Skip to content

Commit 10747e6

Browse files
committed
stop creating qemu if there is already same name qemu
1 parent 6a24f57 commit 10747e6

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

cloud/services/compute/instance/qemu.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,35 @@ func (s *Service) reconcileQEMU(ctx context.Context) (*proxmox.VirtualMachine, e
2121
log.Info("Reconciling QEMU")
2222

2323
qemu, err := s.getQEMU(ctx)
24-
if err == nil { // if qemu is found, return it
25-
return qemu, nil
24+
if err != nil {
25+
if !rest.IsNotFound(err) {
26+
log.Error(err, "failed to get qemu")
27+
return nil, err
28+
}
29+
30+
// no qemu found, try to create new one
31+
log.V(3).Info("qemu wasn't found. new qemu will be created")
32+
if exist, err := s.client.VirtualMachineExistsWithName(ctx, s.scope.Name()); exist || err != nil {
33+
if exist {
34+
// there should no qemu with same name. occuring an error
35+
err = fmt.Errorf("qemu %s already exists", s.scope.Name())
36+
}
37+
log.Error(err, "stop creating new qemu to avoid replicating same qemu")
38+
return nil, err
39+
}
40+
qemu, err = s.createQEMU(ctx)
41+
if err != nil {
42+
log.Error(err, "failed to create qemu")
43+
return nil, err
44+
}
2645
}
27-
if !rest.IsNotFound(err) {
28-
log.Error(err, "failed to get qemu")
46+
47+
s.scope.SetVMID(qemu.VM.VMID)
48+
s.scope.SetNodeName(qemu.Node)
49+
if err := s.scope.PatchObject(); err != nil {
2950
return nil, err
3051
}
31-
32-
// no qemu found, create new one
33-
return s.createQEMU(ctx)
52+
return qemu, nil
3453
}
3554

3655
// get QEMU gets proxmox vm from vmid
@@ -62,12 +81,6 @@ func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, erro
6281
log.Error(err, "failed to create qemu instance")
6382
return nil, err
6483
}
65-
66-
s.scope.SetNodeName(result.Node())
67-
s.scope.SetVMID(result.VMID())
68-
if err := s.scope.PatchObject(); err != nil {
69-
return nil, err
70-
}
7184
return result.Instance(), nil
7285
}
7386

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/onsi/ginkgo/v2 v2.13.1
99
github.com/onsi/gomega v1.30.0
1010
github.com/pkg/errors v0.9.1
11-
github.com/sp-yduck/proxmox-go v0.0.0-alpha23
11+
github.com/sp-yduck/proxmox-go v0.0.0-alpha24
1212
github.com/spf13/pflag v1.0.5
1313
gopkg.in/yaml.v3 v3.0.1
1414
k8s.io/api v0.27.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
417417
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
418418
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
419419
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
420-
github.com/sp-yduck/proxmox-go v0.0.0-alpha23 h1:dRewwKbfqiw8dVvbfjdSR0id7DzmlljnAqyGqqva2TM=
421-
github.com/sp-yduck/proxmox-go v0.0.0-alpha23/go.mod h1:iEI7ilRwyUaWvoxuXs/30UJ2f8Z2TWXCW1cZ3QeU2JY=
420+
github.com/sp-yduck/proxmox-go v0.0.0-alpha24 h1:c858F6hudBHXZgTFlgOu2VuqWOXsAbbxV2axZICmVQQ=
421+
github.com/sp-yduck/proxmox-go v0.0.0-alpha24/go.mod h1:iEI7ilRwyUaWvoxuXs/30UJ2f8Z2TWXCW1cZ3QeU2JY=
422422
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
423423
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
424424
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=

0 commit comments

Comments
 (0)