-
Notifications
You must be signed in to change notification settings - Fork 11
/
qemu.go
166 lines (149 loc) · 4.88 KB
/
qemu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package instance
import (
"context"
"fmt"
"github.com/k8s-proxmox/cluster-api-provider-proxmox/cloud/scheduler/framework"
"github.com/k8s-proxmox/proxmox-go/api"
"github.com/k8s-proxmox/proxmox-go/proxmox"
"github.com/k8s-proxmox/proxmox-go/rest"
"sigs.k8s.io/controller-runtime/pkg/log"
)
const (
bootDvice = "scsi0"
)
// reconciles QEMU instance
func (s *Service) reconcileQEMU(ctx context.Context) (*proxmox.VirtualMachine, error) {
log := log.FromContext(ctx)
log.Info("Reconciling QEMU")
qemu, err := s.getQEMU(ctx)
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
}
}
s.scope.SetVMID(qemu.VM.VMID)
s.scope.SetNodeName(qemu.Node)
if err := s.scope.PatchObject(); err != nil {
return nil, err
}
return qemu, nil
}
// get QEMU gets proxmox vm from vmid
func (s *Service) getQEMU(ctx context.Context) (*proxmox.VirtualMachine, error) {
log := log.FromContext(ctx)
log.Info("getting qemu from vmid")
vmid := s.scope.GetVMID()
if vmid != nil {
return s.client.VirtualMachine(ctx, *vmid)
}
return nil, rest.NotFoundErr
}
func (s *Service) createQEMU(ctx context.Context) (*proxmox.VirtualMachine, error) {
log := log.FromContext(ctx)
log.Info("creating qemu")
if err := s.ensureStorageAvailable(ctx); err != nil {
return nil, err
}
// create qemu
log.Info("making qemu spec")
vmoption := s.generateVMOptions()
// bind annotation key-values to context
schedCtx := framework.ContextWithMap(ctx, s.scope.Annotations())
result, err := s.scheduler.CreateQEMU(schedCtx, &vmoption)
if err != nil {
log.Error(err, "failed to schedule qemu instance")
return nil, err
}
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 {
vmName := s.scope.Name()
snippetStorageName := s.scope.GetClusterStorage().Name
imageStorageName := s.scope.GetStorage()
network := s.scope.GetNetwork()
hardware := s.scope.GetHardware()
options := s.scope.GetOptions()
cicustom := fmt.Sprintf("user=%s:%s", snippetStorageName, userSnippetPath(vmName))
ide2 := fmt.Sprintf("file=%s:cloudinit,media=cdrom", imageStorageName)
scsi0 := fmt.Sprintf("%s:0,import-from=%s", imageStorageName, rawImageFilePath(s.scope.GetImage()))
net0 := hardware.NetworkDevice.String()
vmoptions := api.VirtualMachineCreateOptions{
ACPI: boolToInt8(options.ACPI),
Agent: "enabled=1",
Arch: api.Arch(options.Arch),
Balloon: options.Balloon,
BIOS: string(hardware.BIOS),
Boot: fmt.Sprintf("order=%s", bootDvice),
CiCustom: cicustom,
Cores: hardware.CPU,
Cpu: hardware.CPUType,
CpuLimit: hardware.CPULimit,
Description: options.Description,
HugePages: options.HugePages.String(),
Ide: api.Ide{Ide2: ide2},
IPConfig: api.IPConfig{IPConfig0: network.IPConfig.String()},
KeepHugePages: boolToInt8(options.KeepHugePages),
KVM: boolToInt8(options.KVM),
LocalTime: boolToInt8(options.LocalTime),
Lock: string(options.Lock),
Memory: hardware.Memory,
Name: vmName,
NameServer: network.NameServer,
Net: api.Net{Net0: net0},
Numa: boolToInt8(options.NUMA),
Node: s.scope.NodeName(),
OnBoot: boolToInt8(options.OnBoot),
OSType: api.OSType(options.OSType),
Protection: boolToInt8(options.Protection),
Reboot: int(boolToInt8(options.Reboot)),
Scsi: api.Scsi{Scsi0: scsi0},
ScsiHw: api.VirtioScsiPci,
SearchDomain: network.SearchDomain,
Serial: api.Serial{Serial0: "socket"},
Shares: options.Shares,
Sockets: hardware.Sockets,
Tablet: boolToInt8(options.Tablet),
Tags: options.Tags.String(),
TDF: boolToInt8(options.TimeDriftFix),
Template: boolToInt8(options.Template),
VCPUs: options.VCPUs,
VMGenID: options.VMGenerationID,
VMID: s.scope.GetVMID(),
VGA: "serial0",
}
return vmoptions
}
func boolToInt8(b bool) int8 {
if b {
return 1
}
return 0
}