/
utils.go
211 lines (169 loc) · 5.11 KB
/
utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright 2023-2024 IONOS Cloud.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vmservice
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/google/uuid"
infrav1alpha1 "github.com/ionos-cloud/cluster-api-provider-proxmox/api/v1alpha1"
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
)
const (
// DefaultNetworkDeviceIPV4 is the default network device name for ipv4.
DefaultNetworkDeviceIPV4 = "net0-inet"
// DefaultNetworkDeviceIPV6 is the default network device name for ipv6.
DefaultNetworkDeviceIPV6 = "net0-inet6"
)
func extractUUID(input string) string {
pattern := `(^|,)uuid=([0-9a-fA-F-]+)`
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(input)
if len(match) > 1 {
if parsed, err := uuid.Parse(match[len(match)-1]); err == nil {
return parsed.String()
}
}
return ""
}
// IPAddressWithPrefix return formatted IP Address with prefix.
func IPAddressWithPrefix(ip string, prefix int) string {
if ip == "" {
return ""
}
return fmt.Sprintf("%s/%d", ip, prefix)
}
// extractNetworkModel returns the model out of net device input e.g. virtio=A6:23:64:4D:84:CB,bridge=vmbr1,mtu=1500.
func extractNetworkModel(input string) string {
re := regexp.MustCompile(`([^,=]+)(?:=[^,]*)?,bridge=([^,]+)`)
matches := re.FindStringSubmatch(input)
if len(matches) >= 2 {
return matches[1]
}
return ""
}
// extractNetworkBridge returns the bridge out of net device input e.g. virtio=A6:23:64:4D:84:CB,bridge=vmbr1,mtu=1500.
func extractNetworkBridge(input string) string {
re := regexp.MustCompile(`bridge=(\w+)`)
match := re.FindStringSubmatch(input)
if len(match) > 1 {
return match[1]
}
return "unknown"
}
// extractNetworkMTU returns the mtu out of net device input e.g. virtio=A6:23:64:4D:84:CB,bridge=vmbr1,mtu=1500.
func extractNetworkMTU(input string) uint16 {
re := regexp.MustCompile(`mtu=(\d+)`)
match := re.FindStringSubmatch(input)
if len(match) > 1 {
mtu, err := strconv.ParseUint(match[1], 10, 16)
if err != nil {
return 0
}
return uint16(mtu)
}
return 0
}
// extractNetworkVLAN returns the vlan out of net device input e.g. virtio=A6:23:64:4D:84:CB,bridge=vmbr1,mtu=1500,tag=100.
func extractNetworkVLAN(input string) uint16 {
re := regexp.MustCompile(`tag=(\d+)`)
match := re.FindStringSubmatch(input)
if len(match) > 1 {
vlan, err := strconv.ParseUint(match[1], 10, 16)
if err != nil {
return 0
}
return uint16(vlan)
}
return 0
}
func shouldUpdateNetworkDevices(machineScope *scope.MachineScope) bool {
if machineScope.ProxmoxMachine.Spec.Network == nil {
// no network config needed
return false
}
nets := machineScope.VirtualMachine.VirtualMachineConfig.MergeNets()
if machineScope.ProxmoxMachine.Spec.Network.Default != nil {
net0 := nets[infrav1alpha1.DefaultNetworkDevice]
if net0 == "" {
return true
}
desiredDefault := *machineScope.ProxmoxMachine.Spec.Network.Default
model := extractNetworkModel(net0)
bridge := extractNetworkBridge(net0)
if model != *desiredDefault.Model || bridge != desiredDefault.Bridge {
return true
}
if desiredDefault.MTU != nil {
mtu := extractNetworkMTU(net0)
if mtu != *desiredDefault.MTU {
return true
}
}
if desiredDefault.VLAN != nil {
vlan := extractNetworkVLAN(net0)
if vlan != *desiredDefault.VLAN {
return true
}
}
}
devices := machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices
for _, v := range devices {
net := nets[v.Name]
// device is empty.
if len(net) == 0 {
return true
}
model := extractNetworkModel(net)
bridge := extractNetworkBridge(net)
// current is different from the desired spec.
if model != *v.Model || bridge != v.Bridge {
return true
}
if v.MTU != nil {
mtu := extractNetworkMTU(net)
if mtu != *v.MTU {
return true
}
}
if v.VLAN != nil {
vlan := extractNetworkVLAN(net)
if vlan != *v.VLAN {
return true
}
}
}
return false
}
// formatNetworkDevice formats a network device config
// example 'virtio,bridge=vmbr0,tag=100'.
func formatNetworkDevice(model, bridge string, mtu *uint16, vlan *uint16) string {
var components = []string{model, fmt.Sprintf("bridge=%s", bridge)}
if mtu != nil {
components = append(components, fmt.Sprintf("mtu=%d", *mtu))
}
if vlan != nil {
components = append(components, fmt.Sprintf("tag=%d", *vlan))
}
return strings.Join(components, ",")
}
// extractMACAddress returns the macaddress out of net device input e.g. virtio=A6:23:64:4D:84:CB,bridge=vmbr1.
func extractMACAddress(input string) string {
re := regexp.MustCompile(`=([^,]+),bridge`)
matches := re.FindStringSubmatch(input)
if len(matches) == 2 {
return matches[1]
}
return ""
}