Skip to content

Commit f4fe04d

Browse files
authored
Merge pull request #117 from sp-yduck/feature/network-bridge
Support network device configuration
2 parents c05bfb9 + 813aaa7 commit f4fe04d

File tree

7 files changed

+237
-19
lines changed

7 files changed

+237
-19
lines changed

api/v1beta1/proxmoxmachine_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ type ProxmoxMachineSpec struct {
5353
CloudInit CloudInit `json:"cloudInit,omitempty"`
5454

5555
// Hardware
56-
Hardware Hardware `json:"hardware,omitempty"`
56+
// +kubebuilder:default:={cpu:2,disk:"50G",memory:4096,networkDevice:{model:virtio,bridge:vmbr0,firewall:true}}
57+
Hardware Hardware `json:"hardware"`
5758

5859
// Network
5960
Network Network `json:"network,omitempty"`

api/v1beta1/type.go

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import (
77
"github.com/sp-yduck/proxmox-go/api"
88
)
99

10+
type InstanceStatus string
11+
12+
var (
13+
InstanceStatusPaused = InstanceStatus(api.ProcessStatusPaused)
14+
InstanceStatusRunning = InstanceStatus(api.ProcessStatusRunning)
15+
InstanceStatusStopped = InstanceStatus(api.ProcessStatusStopped)
16+
)
17+
1018
// ServerRef is used for configuring Proxmox client
1119
type ServerRef struct {
1220
// endpoint is the address of the Proxmox-VE REST API endpoint.
@@ -87,6 +95,81 @@ type Hardware struct {
8795
// +kubebuilder:validation:Pattern:=\+?\d+(\.\d+)?[KMGT]?
8896
// +kubebuilder:default:="50G"
8997
Disk string `json:"disk,omitempty"`
98+
99+
// network devices
100+
// to do: multiple devices
101+
// +kubebuilder:default:={model:virtio,bridge:vmbr0,firewall:true}
102+
NetworkDevice NetworkDevice `json:"networkDevice"`
103+
}
104+
105+
// Network Device
106+
type NetworkDevice struct {
107+
// +kubebuilder:default:="virtio"
108+
Model NetworkDeviceModel `json:"model"`
109+
110+
// +kubebuilder:default:="vmbr0"
111+
Bridge NetworkDeviceBridge `json:"bridge"`
112+
113+
// +kubebuilder:default:=true
114+
Firewall bool `json:"firewall,omitempty"`
115+
116+
LinkDown bool `json:"linkDown,omitempty"`
117+
118+
MacAddr string `json:"macAddr,omitempty"`
119+
120+
MTU int `json:"mtu,omitempty"`
121+
122+
Queues int `json:"queues,omitempty"`
123+
124+
// since float is highly discouraged, use string instead
125+
// +kubebuilder:validation:Pattern:=[0-9]+(\.|)[0-9]*
126+
Rate string `json:"rate,omitempty"`
127+
128+
Tag int `json:"tag,omitempty"`
129+
130+
// trunks: array of vlanid
131+
Trunks []int `json:"trunks,omitempty"`
132+
}
133+
134+
type (
135+
// +kubebuilder:validation:Enum:=e1000;virtio;rtl8139;vmxnet3
136+
NetworkDeviceModel string
137+
138+
// +kubebuilder:validation:Pattern:="vmbr[0-9]{1,4}"
139+
NetworkDeviceBridge string
140+
)
141+
142+
func (n *NetworkDevice) String() string {
143+
config := []string{}
144+
config = append(config, fmt.Sprintf("model=%s", string(n.Model)))
145+
if n.Bridge != "" {
146+
config = append(config, fmt.Sprintf("bridge=%s", string(n.Bridge)))
147+
}
148+
if n.Firewall {
149+
config = append(config, fmt.Sprintf("firewall=%d", btoi(n.Firewall)))
150+
}
151+
if n.LinkDown {
152+
config = append(config, fmt.Sprintf("link_down=%d", btoi(n.LinkDown)))
153+
}
154+
if n.MacAddr != "" {
155+
config = append(config, fmt.Sprintf("macaddr=%s,%s=%s", n.MacAddr, string(n.Model), n.MacAddr))
156+
}
157+
if n.MTU != 0 {
158+
config = append(config, fmt.Sprintf("mtu=%d", n.MTU))
159+
}
160+
if n.Queues != 0 {
161+
config = append(config, fmt.Sprintf("queues=%d", n.Queues))
162+
}
163+
if n.Rate != "" {
164+
config = append(config, fmt.Sprintf("rate=%s", n.Rate))
165+
}
166+
if n.Tag != 0 {
167+
config = append(config, fmt.Sprintf("tag=%d", n.Tag))
168+
}
169+
if n.Trunks != nil {
170+
config = append(config, fmt.Sprintf("trunks=%s", strings.Join(itoaSlice(n.Trunks), ";")))
171+
}
172+
return strings.Join(config, ",")
90173
}
91174

92175
// Network
@@ -149,10 +232,19 @@ type Storage struct {
149232
Path string `json:"path,omitempty"`
150233
}
151234

152-
type InstanceStatus string
235+
// bool to int
236+
func btoi(x bool) int8 {
237+
if x {
238+
return 1
239+
}
240+
return 0
241+
}
153242

154-
var (
155-
InstanceStatusPaused = InstanceStatus(api.ProcessStatusPaused)
156-
InstanceStatusRunning = InstanceStatus(api.ProcessStatusRunning)
157-
InstanceStatusStopped = InstanceStatus(api.ProcessStatusStopped)
158-
)
243+
// []int to []string
244+
func itoaSlice(a []int) []string {
245+
b := []string{}
246+
for _, x := range a {
247+
b = append(b, fmt.Sprintf("%d", x))
248+
}
249+
return b
250+
}

api/v1beta1/zz_generated.deepcopy.go

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

cloud/scope/machine.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,6 @@ func (m *MachineScope) GetNetwork() infrav1.Network {
196196
}
197197

198198
func (m *MachineScope) GetHardware() infrav1.Hardware {
199-
// set default value if empty
200-
if m.ProxmoxMachine.Spec.Hardware.CPU == 0 {
201-
m.ProxmoxMachine.Spec.Hardware.CPU = 2
202-
}
203-
if m.ProxmoxMachine.Spec.Hardware.Memory == 0 {
204-
m.ProxmoxMachine.Spec.Hardware.Memory = 4096
205-
}
206-
if m.ProxmoxMachine.Spec.Hardware.Disk == "" {
207-
m.ProxmoxMachine.Spec.Hardware.Disk = "50G"
208-
}
209199
return m.ProxmoxMachine.Spec.Hardware
210200
}
211201

cloud/services/compute/instance/qemu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (s *Service) generateVMOptions() api.VirtualMachineCreateOptions {
7777
cicustom := fmt.Sprintf("user=%s:%s", snippetStorageName, userSnippetPath(vmName))
7878
ide2 := fmt.Sprintf("file=%s:cloudinit,media=cdrom", imageStorageName)
7979
scsi0 := fmt.Sprintf("%s:0,import-from=%s", imageStorageName, rawImageFilePath(s.scope.GetImage()))
80-
net0 := "model=virtio,bridge=vmbr0,firewall=1"
80+
net0 := hardware.NetworkDevice.String()
8181

8282
vmoptions := api.VirtualMachineCreateOptions{
8383
ACPI: boolToInt8(options.ACPI),

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ spec:
219219
this Machine should be attached to, as defined in Cluster API.
220220
type: string
221221
hardware:
222+
default:
223+
cpu: 2
224+
disk: 50G
225+
memory: 4096
226+
networkDevice:
227+
bridge: vmbr0
228+
firewall: true
229+
model: virtio
222230
description: Hardware
223231
properties:
224232
bios:
@@ -248,10 +256,58 @@ spec:
248256
description: 'amount of RAM for the VM in MiB : 16 ~'
249257
minimum: 16
250258
type: integer
259+
networkDevice:
260+
default:
261+
bridge: vmbr0
262+
firewall: true
263+
model: virtio
264+
description: 'network devices to do: multiple devices'
265+
properties:
266+
bridge:
267+
default: vmbr0
268+
pattern: vmbr[0-9]{1,4}
269+
type: string
270+
firewall:
271+
default: true
272+
type: boolean
273+
linkDown:
274+
type: boolean
275+
macAddr:
276+
type: string
277+
model:
278+
default: virtio
279+
enum:
280+
- e1000
281+
- virtio
282+
- rtl8139
283+
- vmxnet3
284+
type: string
285+
mtu:
286+
type: integer
287+
queues:
288+
type: integer
289+
rate:
290+
description: since float is highly discouraged, use string
291+
instead
292+
pattern: '[0-9]+(\.|)[0-9]*'
293+
type: string
294+
tag:
295+
type: integer
296+
trunks:
297+
description: 'trunks: array of vlanid'
298+
items:
299+
type: integer
300+
type: array
301+
required:
302+
- bridge
303+
- model
304+
type: object
251305
sockets:
252306
description: The number of CPU sockets. Defaults to 1.
253307
minimum: 1
254308
type: integer
309+
required:
310+
- networkDevice
255311
type: object
256312
image:
257313
description: Image is the image to be provisioned
@@ -460,6 +516,7 @@ spec:
460516
minimum: 0
461517
type: integer
462518
required:
519+
- hardware
463520
- image
464521
type: object
465522
status:

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ spec:
236236
API.
237237
type: string
238238
hardware:
239+
default:
240+
cpu: 2
241+
disk: 50G
242+
memory: 4096
243+
networkDevice:
244+
bridge: vmbr0
245+
firewall: true
246+
model: virtio
239247
description: Hardware
240248
properties:
241249
bios:
@@ -265,10 +273,58 @@ spec:
265273
description: 'amount of RAM for the VM in MiB : 16 ~'
266274
minimum: 16
267275
type: integer
276+
networkDevice:
277+
default:
278+
bridge: vmbr0
279+
firewall: true
280+
model: virtio
281+
description: 'network devices to do: multiple devices'
282+
properties:
283+
bridge:
284+
default: vmbr0
285+
pattern: vmbr[0-9]{1,4}
286+
type: string
287+
firewall:
288+
default: true
289+
type: boolean
290+
linkDown:
291+
type: boolean
292+
macAddr:
293+
type: string
294+
model:
295+
default: virtio
296+
enum:
297+
- e1000
298+
- virtio
299+
- rtl8139
300+
- vmxnet3
301+
type: string
302+
mtu:
303+
type: integer
304+
queues:
305+
type: integer
306+
rate:
307+
description: since float is highly discouraged, use
308+
string instead
309+
pattern: '[0-9]+(\.|)[0-9]*'
310+
type: string
311+
tag:
312+
type: integer
313+
trunks:
314+
description: 'trunks: array of vlanid'
315+
items:
316+
type: integer
317+
type: array
318+
required:
319+
- bridge
320+
- model
321+
type: object
268322
sockets:
269323
description: The number of CPU sockets. Defaults to 1.
270324
minimum: 1
271325
type: integer
326+
required:
327+
- networkDevice
272328
type: object
273329
image:
274330
description: Image is the image to be provisioned
@@ -484,6 +540,7 @@ spec:
484540
minimum: 0
485541
type: integer
486542
required:
543+
- hardware
487544
- image
488545
type: object
489546
required:

0 commit comments

Comments
 (0)