Skip to content

Commit 73588d1

Browse files
committed
network device support
1 parent eb82d23 commit 73588d1

File tree

5 files changed

+195
-8
lines changed

5 files changed

+195
-8
lines changed

api/v1beta1/type.go

Lines changed: 96 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,79 @@ 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+
NetworkDevice NetworkDevice `json:"networkDevice,omitempty"`
102+
}
103+
104+
// Network Device
105+
type NetworkDevice struct {
106+
// +kubebuilder:default:="virtio"
107+
Model NetworkDeviceModel `json:"model,omitempty"`
108+
109+
// +kubebuilder:default:="vmbr0"
110+
Bridge NetworkDeviceBridge `json:"bridge,omitempty"`
111+
112+
Firewall bool `json:"firewall,omitempty"`
113+
114+
LinkDown bool `json:"linkDown,omitempty"`
115+
116+
MacAddr string `json:"macAddr,omitempty"`
117+
118+
MTU int `json:"mtu,omitempty"`
119+
120+
Queues int `json:"queues,omitempty"`
121+
122+
// since float is highly discouraged, use string instead
123+
// +kubebuilder:validation:Pattern:=[0-9]+(\.|)[0-9]*
124+
Rate string `json:"rate,omitempty"`
125+
126+
Tag int `json:"tag,omitempty"`
127+
128+
// trunks: array of vlanid
129+
Trunks []int `json:"trunks,omitempty"`
130+
}
131+
132+
type (
133+
// +kubebuilder:validation:Enum:=e1000;virtio;rtl8139;vmxnet3
134+
NetworkDeviceModel string
135+
136+
// +kubebuilder:validation:Pattern:="vmbr[0-9]{1,4}"
137+
NetworkDeviceBridge string
138+
)
139+
140+
func (n *NetworkDevice) String() string {
141+
config := []string{}
142+
config = append(config, fmt.Sprintf("model=%s", string(n.Model)))
143+
if n.Bridge != "" {
144+
config = append(config, fmt.Sprintf("bridge=%s", string(n.Bridge)))
145+
}
146+
if n.Firewall {
147+
config = append(config, fmt.Sprintf("firewall=%d", btoi(n.Firewall)))
148+
}
149+
if n.LinkDown {
150+
config = append(config, fmt.Sprintf("link_down=%d", btoi(n.LinkDown)))
151+
}
152+
if n.MacAddr != "" {
153+
config = append(config, fmt.Sprintf("macaddr=%s,%s=%s", n.MacAddr, string(n.Model), n.MacAddr))
154+
}
155+
if n.MTU != 0 {
156+
config = append(config, fmt.Sprintf("mtu=%d", n.MTU))
157+
}
158+
if n.Queues != 0 {
159+
config = append(config, fmt.Sprintf("queues=%d", n.Queues))
160+
}
161+
if n.Rate != "" {
162+
config = append(config, fmt.Sprintf("rate=%s", n.Rate))
163+
}
164+
if n.Tag != 0 {
165+
config = append(config, fmt.Sprintf("tag=%d", n.Tag))
166+
}
167+
if n.Trunks != nil {
168+
config = append(config, fmt.Sprintf("trunks=%s", strings.Join(itoaSlice(n.Trunks), ";")))
169+
}
170+
return strings.Join(config, ",")
90171
}
91172

92173
// Network
@@ -149,10 +230,19 @@ type Storage struct {
149230
Path string `json:"path,omitempty"`
150231
}
151232

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

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

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/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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,44 @@ spec:
248248
description: 'amount of RAM for the VM in MiB : 16 ~'
249249
minimum: 16
250250
type: integer
251+
networkDevice:
252+
description: 'network devices to do: multiple devices'
253+
properties:
254+
bridge:
255+
default: vmbr0
256+
pattern: vmbr[0-9]{1,4}
257+
type: string
258+
firewall:
259+
type: boolean
260+
linkDown:
261+
type: boolean
262+
macAddr:
263+
type: string
264+
model:
265+
default: virtio
266+
enum:
267+
- e1000
268+
- virtio
269+
- rtl8139
270+
- vmxnet3
271+
type: string
272+
mtu:
273+
type: integer
274+
queues:
275+
type: integer
276+
rate:
277+
description: since float is highly discouraged, use string
278+
instead
279+
pattern: '[0-9]+(\.|)[0-9]*'
280+
type: string
281+
tag:
282+
type: integer
283+
trunks:
284+
description: 'trunks: array of vlanid'
285+
items:
286+
type: integer
287+
type: array
288+
type: object
251289
sockets:
252290
description: The number of CPU sockets. Defaults to 1.
253291
minimum: 1

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,44 @@ spec:
265265
description: 'amount of RAM for the VM in MiB : 16 ~'
266266
minimum: 16
267267
type: integer
268+
networkDevice:
269+
description: 'network devices to do: multiple devices'
270+
properties:
271+
bridge:
272+
default: vmbr0
273+
pattern: vmbr[0-9]{1,4}
274+
type: string
275+
firewall:
276+
type: boolean
277+
linkDown:
278+
type: boolean
279+
macAddr:
280+
type: string
281+
model:
282+
default: virtio
283+
enum:
284+
- e1000
285+
- virtio
286+
- rtl8139
287+
- vmxnet3
288+
type: string
289+
mtu:
290+
type: integer
291+
queues:
292+
type: integer
293+
rate:
294+
description: since float is highly discouraged, use
295+
string instead
296+
pattern: '[0-9]+(\.|)[0-9]*'
297+
type: string
298+
tag:
299+
type: integer
300+
trunks:
301+
description: 'trunks: array of vlanid'
302+
items:
303+
type: integer
304+
type: array
305+
type: object
268306
sockets:
269307
description: The number of CPU sockets. Defaults to 1.
270308
minimum: 1

0 commit comments

Comments
 (0)