Skip to content

Commit f0104c2

Browse files
committed
add vm options
1 parent 4d6b37a commit f0104c2

File tree

9 files changed

+118
-26
lines changed

9 files changed

+118
-26
lines changed

api/v1beta1/options_types.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package v1beta1
22

3+
import "strconv"
4+
35
// import "encoding/json"
46

57
// +kubebuilder:validation:Enum:=x86_64;aarch64
@@ -17,6 +19,28 @@ type Lock string
1719
// +kubebuilder:validation:Enum:=other;wxp;w2k;w2k3;w2k8;wvista;win7;win8;win10;win11;l24;l26;solaris
1820
type OSType string
1921

22+
// +kubebuilder:validation:Pattern:="[a-zA-Z0-9-_.;]+"
23+
type Tag string
24+
25+
type Tags []Tag
26+
27+
func (h *HugePages) String() string {
28+
if h == nil {
29+
return ""
30+
} else if *h == 0 {
31+
return "any"
32+
}
33+
return strconv.Itoa(int(*h))
34+
}
35+
36+
func (t *Tags) String() string {
37+
var tags string
38+
for _, tag := range *t {
39+
tags += string(tag) + ";"
40+
}
41+
return tags
42+
}
43+
2044
// Options
2145
type Options struct {
2246
// Enable/Disable ACPI. Defaults to true.
@@ -41,7 +65,7 @@ type Options struct {
4165
// HotPlug []HotPlugDevice `json:"hotPlug,omitempty"`
4266

4367
// enable/disable hugepages memory. 0 or 2 or 1024. 0 indicated 'any'
44-
HugePages HugePages `json:"hugePages,omitempty"`
68+
HugePages *HugePages `json:"hugePages,omitempty"`
4569

4670
// Use together with hugepages. If enabled, hugepages will not not be deleted
4771
// after VM shutdown and can be used for subsequent starts. Defaults to false.
@@ -105,7 +129,7 @@ type Options struct {
105129
Tablet bool `json:"tablet,omitempty"`
106130

107131
// Tags of the VM. This is only meta information.
108-
Tags []string `json:"tags,omitempty"`
132+
Tags Tags `json:"tags,omitempty"`
109133

110134
// Enable/disable time drift fix. Defaults to false.
111135
TimeDriftFix bool `json:"timeDriftFix,omitempty"`
@@ -121,6 +145,7 @@ type Options struct {
121145

122146
// VGA string `json:"vga,omitempty"`
123147

148+
// +kubebuilder:validation:Pattern:="(?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])"
124149
// The VM generation ID (vmgenid) device exposes a 128-bit integer value identifier to the guest OS.
125150
// This allows to notify the guest operating system when the virtual machine is executed with a different configuration
126151
// (e.g. snapshot execution or creation from a template).

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type MachineGetter interface {
6262
GetNetwork() infrav1.Network
6363
GetHardware() infrav1.Hardware
6464
GetVMID() *int
65+
GetOptions() infrav1.Options
6566
}
6667

6768
// MachineSetter is an interface which can set machine information.

cloud/scope/machine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ func (m *MachineScope) GetHardware() infrav1.Hardware {
185185
return m.ProxmoxMachine.Spec.Hardware
186186
}
187187

188+
func (m *MachineScope) GetOptions() infrav1.Options {
189+
return m.ProxmoxMachine.Spec.Options
190+
}
191+
188192
// SetProviderID sets the ProxmoxMachine providerID in spec.
189193
func (m *MachineScope) SetProviderID(uuid string) error {
190194
providerid, err := providerid.New(uuid)

cloud/services/compute/instance/qemu.go

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"github.com/sp-yduck/proxmox-go/proxmox"
1212
"github.com/sp-yduck/proxmox-go/rest"
1313
"sigs.k8s.io/controller-runtime/pkg/log"
14-
15-
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
1614
)
1715

1816
const (
@@ -72,7 +70,7 @@ func (s *Service) createQEMU(ctx context.Context, nodeName string, vmid *int) (*
7270
s.scope.SetVMID(*vmid)
7371
}
7472

75-
vmoption := generateVMOptions(s.scope.Name(), s.scope.GetStorage().Name, s.scope.GetNetwork(), s.scope.GetHardware())
73+
vmoption := s.generateVMOptions()
7674
vm, err := s.client.CreateVirtualMachine(ctx, nodeName, *vmid, vmoption)
7775
if err != nil {
7876
log.Error(err, fmt.Sprintf("failed to create qemu instance %s", vm.VM.Name))
@@ -103,24 +101,60 @@ func (s *Service) getRandomNode(ctx context.Context) (*api.Node, error) {
103101
return nodes[r.Intn(len(nodes))], nil
104102
}
105103

106-
func generateVMOptions(vmName, storageName string, network infrav1.Network, hardware infrav1.Hardware) api.VirtualMachineCreateOptions {
104+
func (s *Service) generateVMOptions() api.VirtualMachineCreateOptions {
105+
vmName := s.scope.Name()
106+
storageName := s.scope.GetStorage().Name
107+
network := s.scope.GetNetwork()
108+
hardware := s.scope.GetHardware()
109+
options := s.scope.GetOptions()
110+
107111
vmoptions := api.VirtualMachineCreateOptions{
108-
Agent: "enabled=1",
109-
Cores: hardware.CPU,
110-
Memory: hardware.Memory,
111-
Name: vmName,
112-
NameServer: network.NameServer,
113-
Boot: fmt.Sprintf("order=%s", bootDvice),
114-
Ide: api.Ide{Ide2: fmt.Sprintf("file=%s:cloudinit,media=cdrom", storageName)},
115-
CiCustom: fmt.Sprintf("user=%s:%s", storageName, userSnippetPath(vmName)),
116-
IPConfig: api.IPConfig{IPConfig0: network.IPConfig.String()},
117-
OSType: api.L26,
118-
Net: api.Net{Net0: "model=virtio,bridge=vmbr0,firewall=1"},
119-
Scsi: api.Scsi{Scsi0: fmt.Sprintf("file=%s:8", storageName)},
120-
ScsiHw: api.VirtioScsiPci,
121-
SearchDomain: network.SearchDomain,
122-
Serial: api.Serial{Serial0: "socket"},
123-
VGA: "serial0",
112+
ACPI: boolToInt8(options.ACPI),
113+
Agent: "enabled=1",
114+
Arch: api.Arch(options.Arch),
115+
Balloon: options.Balloon,
116+
BIOS: string(hardware.BIOS),
117+
Boot: fmt.Sprintf("order=%s", bootDvice),
118+
CiCustom: fmt.Sprintf("user=%s:%s", storageName, userSnippetPath(vmName)),
119+
Cores: hardware.CPU,
120+
CpuLimit: hardware.CPULimit,
121+
Description: options.Description,
122+
HugePages: options.HugePages.String(),
123+
Ide: api.Ide{Ide2: fmt.Sprintf("file=%s:cloudinit,media=cdrom", storageName)},
124+
IPConfig: api.IPConfig{IPConfig0: network.IPConfig.String()},
125+
KeepHugePages: boolToInt8(options.KeepHugePages),
126+
KVM: boolToInt8(options.KVM),
127+
LocalTime: boolToInt8(options.LocalTime),
128+
Lock: string(options.Lock),
129+
Memory: hardware.Memory,
130+
Name: vmName,
131+
NameServer: network.NameServer,
132+
Net: api.Net{Net0: "model=virtio,bridge=vmbr0,firewall=1"},
133+
Numa: boolToInt8(options.NUMA),
134+
OnBoot: boolToInt8(options.OnBoot),
135+
OSType: api.OSType(options.OSType),
136+
Protection: boolToInt8(options.Protection),
137+
Reboot: int(boolToInt8(options.Reboot)),
138+
Scsi: api.Scsi{Scsi0: fmt.Sprintf("file=%s:8", storageName)},
139+
ScsiHw: api.VirtioScsiPci,
140+
SearchDomain: network.SearchDomain,
141+
Serial: api.Serial{Serial0: "socket"},
142+
Shares: options.Shares,
143+
Sockets: hardware.Sockets,
144+
Tablet: boolToInt8(options.Tablet),
145+
Tags: options.Tags.String(),
146+
TDF: boolToInt8(options.TimeDriftFix),
147+
Template: boolToInt8(options.Template),
148+
VCPUs: options.VCPUs,
149+
VMGenID: options.VMGenerationID,
150+
VGA: "serial0",
124151
}
125152
return vmoptions
126153
}
154+
155+
func boolToInt8(b bool) int8 {
156+
if b {
157+
return 1
158+
}
159+
return 0
160+
}

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachines.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ spec:
287287
tags:
288288
description: Tags of the VM. This is only meta information.
289289
items:
290+
pattern: '[a-zA-Z0-9-_.;]+'
290291
type: string
291292
type: array
292293
template:
@@ -311,6 +312,7 @@ spec:
311312
works when done through API/CLI create or update methods, but
312313
not when manually editing the config file. regex: (?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01]).
313314
Defaults to 1 (autogenerated)'
315+
pattern: (?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])
314316
type: string
315317
type: object
316318
providerID:

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachinetemplates.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ spec:
315315
tags:
316316
description: Tags of the VM. This is only meta information.
317317
items:
318+
pattern: '[a-zA-Z0-9-_.;]+'
318319
type: string
319320
type: array
320321
template:
@@ -342,6 +343,7 @@ spec:
342343
methods, but not when manually editing the config file.
343344
regex: (?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01]).
344345
Defaults to 1 (autogenerated)'
346+
pattern: (?:[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}|[01])
345347
type: string
346348
type: object
347349
providerID:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/onsi/ginkgo/v2 v2.11.0
88
github.com/onsi/gomega v1.27.10
99
github.com/pkg/errors v0.9.1
10-
github.com/sp-yduck/proxmox-go v0.0.0-20230803145313-0e5082ef8998
10+
github.com/sp-yduck/proxmox-go v0.0.0-20230807140547-f03780ca1110
1111
gopkg.in/yaml.v3 v3.0.1
1212
k8s.io/api v0.26.1
1313
k8s.io/apimachinery v0.26.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g
290290
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
291291
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
292292
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
293-
github.com/sp-yduck/proxmox-go v0.0.0-20230803145313-0e5082ef8998 h1:W9Ab56G2klKV0XzPOSnX10kGwvConkftI5riBj48+KE=
294-
github.com/sp-yduck/proxmox-go v0.0.0-20230803145313-0e5082ef8998/go.mod h1:XtUQue7w5tuB6c7xputkhnN+A3EZbzDWq+uXmlkOPfQ=
293+
github.com/sp-yduck/proxmox-go v0.0.0-20230807140547-f03780ca1110 h1:TJqu6ZnBbz7rL+ZTMX7RyNUoKhAGlBpbAREb2M/ANMo=
294+
github.com/sp-yduck/proxmox-go v0.0.0-20230807140547-f03780ca1110/go.mod h1:XtUQue7w5tuB6c7xputkhnN+A3EZbzDWq+uXmlkOPfQ=
295295
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
296296
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
297297
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

0 commit comments

Comments
 (0)