Skip to content

Commit bea26c0

Browse files
committed
WIP: vmservice/ip.go: cleanup
1 parent 4e66b5d commit bea26c0

File tree

1 file changed

+2
-156
lines changed
  • internal/service/vmservice

1 file changed

+2
-156
lines changed

internal/service/vmservice/ip.go

Lines changed: 2 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3131
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
3232
"sigs.k8s.io/cluster-api/util/conditions"
33-
"sigs.k8s.io/controller-runtime/pkg/client"
3433

3534
infrav1 "github.com/ionos-cloud/cluster-api-provider-proxmox/api/v1alpha2"
3635
"github.com/ionos-cloud/cluster-api-provider-proxmox/pkg/scope"
@@ -45,6 +44,7 @@ func reconcileIPAddresses(ctx context.Context, machineScope *scope.MachineScope)
4544
machineScope.Logger.V(4).Info("reconciling IPAddresses.")
4645
conditions.MarkFalse(machineScope.ProxmoxMachine, infrav1.VMProvisionedCondition, infrav1.WaitingForStaticIPAllocationReason, clusterv1.ConditionSeverityInfo, "")
4746

47+
// TODO: This datastructure is BAD
4848
netPoolAddresses := make(map[string]map[string][]string)
4949

5050
if machineScope.ProxmoxMachine.Spec.Network != nil {
@@ -56,6 +56,7 @@ func reconcileIPAddresses(ctx context.Context, machineScope *scope.MachineScope)
5656

5757
// update the status.IpAddr.
5858

59+
// TODO: This datastructure should be redundant. Too many loops too
5960
statusAddresses := make(map[string]*infrav1.IPAddresses, len(netPoolAddresses))
6061
for net, pools := range netPoolAddresses {
6162
for _, ips := range pools {
@@ -81,14 +82,6 @@ func formatIPAddressName(name, device string) string {
8182
return fmt.Sprintf("%s-%s", name, device)
8283
}
8384

84-
func findIPAddress(ctx context.Context, machineScope *scope.MachineScope, device string) (*ipamv1.IPAddress, error) {
85-
key := client.ObjectKey{
86-
Namespace: machineScope.Namespace(),
87-
Name: formatIPAddressName(machineScope.Name(), device),
88-
}
89-
return machineScope.IPAMHelper.GetIPAddress(ctx, key)
90-
}
91-
9285
// findIPAddressV2 takes the likely name of an ipaddress and returns an ipamv1.IPAddress object or nil
9386
func findIPAddressV2(ctx context.Context, poolRef *corev1.TypedLocalObjectReference, machineScope *scope.MachineScope) ([]ipamv1.IPAddress, error) {
9487
return machineScope.IPAMHelper.GetIPAddressV2(ctx, *poolRef, machineScope.ProxmoxMachine)
@@ -132,101 +125,9 @@ func machineHasIPAddress(machine *infrav1.ProxmoxMachine) bool {
132125
return machine.Status.IPAddresses[infrav1.DefaultNetworkDevice] != nil
133126
}
134127

135-
/*
136-
func handleIPAddressForDevice(ctx context.Context, machineScope *scope.MachineScope, device, format string, ipamRef *corev1.TypedLocalObjectReference) (string, error) {
137-
suffix := infrav1.DefaultSuffix
138-
if format == infrav1.IPV6Format {
139-
suffix += "6"
140-
}
141-
formattedDevice := fmt.Sprintf("%s-%s", device, suffix)
142-
ipAddr, err := findIPAddress(ctx, machineScope, formattedDevice)
143-
if err != nil {
144-
if !apierrors.IsNotFound(err) {
145-
return "", err
146-
}
147-
machineScope.Logger.V(4).Info("IPAddress not found, creating it.", "device", device)
148-
// IpAddress not yet created.
149-
err = machineScope.IPAMHelper.CreateIPAddressClaim(ctx, machineScope.ProxmoxMachine, device, format, machineScope.InfraCluster.Cluster.GetName(), ipamRef)
150-
if err != nil {
151-
return "", errors.Wrapf(err, "unable to create Ip address claim for machine %s", machineScope.Name())
152-
}
153-
return "", nil
154-
}
155-
156-
ip := ipAddr.Spec.Address
157-
158-
machineScope.Logger.V(4).Info("IPAddress found, ", "ip", ip, "device", device)
159-
160-
// format ipTag as `ip_net0_<ipv4/6-address>`
161-
// to add it to the VM.
162-
ipTag := fmt.Sprintf("ip_%s_%s", device, ip)
163-
164-
// Add ip tag if the Virtual Machine doesn't have it.
165-
if vm := machineScope.VirtualMachine; device == infrav1.DefaultNetworkDevice && !vm.HasTag(ipTag) && isIPV4(ip) {
166-
machineScope.Logger.V(4).Info("adding virtual machine ip tag.")
167-
t, err := machineScope.InfraCluster.ProxmoxClient.TagVM(ctx, vm, ipTag)
168-
if err != nil {
169-
return "", errors.Wrapf(err, "unable to add Ip tag to VirtualMachine %s", machineScope.Name())
170-
}
171-
machineScope.ProxmoxMachine.Status.TaskRef = ptr.To(string(t.UPID))
172-
return "", nil
173-
}
174-
175-
return ip, nil
176-
}
177-
*/
178-
179-
/*
180-
func handleDefaultDevice(ctx context.Context, machineScope *scope.MachineScope, addresses map[string]infrav1.IPAddress) (bool, error) {
181-
// default network device ipv4.
182-
if machineScope.InfraCluster.ProxmoxCluster.Spec.IPv4Config != nil ||
183-
(machineScope.ProxmoxMachine.Spec.Network != nil && machineScope.ProxmoxMachine.Spec.Network.Default.IPv4PoolRef != nil) {
184-
var ipamRef *corev1.TypedLocalObjectReference
185-
if machineScope.ProxmoxMachine.Spec.Network != nil && machineScope.ProxmoxMachine.Spec.Network.Default.IPv4PoolRef != nil {
186-
ipamRef = machineScope.ProxmoxMachine.Spec.Network.Default.IPv4PoolRef
187-
}
188-
189-
ip, err := handleIPAddressForDevice(ctx, machineScope, infrav1.DefaultNetworkDevice, infrav1.IPV4Format, ipamRef)
190-
if err != nil || ip == "" {
191-
return true, err
192-
}
193-
addresses[infrav1.DefaultNetworkDevice] = infrav1.IPAddress{
194-
IPV4: ip,
195-
}
196-
}
197-
198-
// default network device ipv6.
199-
if machineScope.InfraCluster.ProxmoxCluster.Spec.IPv6Config != nil ||
200-
(machineScope.ProxmoxMachine.Spec.Network != nil && machineScope.ProxmoxMachine.Spec.Network.Default.IPv6PoolRef != nil) {
201-
var ipamRef *corev1.TypedLocalObjectReference
202-
if machineScope.ProxmoxMachine.Spec.Network != nil && machineScope.ProxmoxMachine.Spec.Network.Default.IPv6PoolRef != nil {
203-
ipamRef = machineScope.ProxmoxMachine.Spec.Network.Default.IPv6PoolRef
204-
}
205-
206-
ip, err := handleIPAddressForDevice(ctx, machineScope, infrav1.DefaultNetworkDevice, infrav1.IPV6Format, ipamRef)
207-
if err != nil || ip == "" {
208-
return true, err
209-
}
210-
211-
addr := addresses[infrav1.DefaultNetworkDevice]
212-
addr.IPV6 = ip
213-
addresses[infrav1.DefaultNetworkDevice] = addr
214-
}
215-
return false, nil
216-
}
217-
*/
218-
219128
func handleIPAddress(ctx context.Context, machineScope *scope.MachineScope, dev *string, poolNum int, ipamRef *corev1.TypedLocalObjectReference) ([]string, error) {
220-
// suffix := infrav1.DefaultSuffix
221-
222129
device := ptr.Deref(dev, infrav1.DefaultNetworkDevice)
223130

224-
// ipAddressName := fmt.Sprintf("%s-%s-%02d-%s", machineScope.Name(), device, poolNum, suffix)
225-
/* TODO: Generalise for default device prepending. Especially IPv6! */
226-
// if device == infrav1.DefaultNetworkDevice && poolNum == 0 {
227-
// ipAddressName = fmt.Sprintf("%s-%s-inet", machineScope.Name(), device)
228-
// }
229-
230131
ipAddresses, err := findIPAddressV2(ctx, ipamRef, machineScope)
231132
if err != nil {
232133
if !apierrors.IsNotFound(err) {
@@ -267,7 +168,6 @@ func handleIPAddress(ctx context.Context, machineScope *scope.MachineScope, dev
267168
}
268169

269170
func handleDevices(ctx context.Context, machineScope *scope.MachineScope, addresses map[string]map[string][]string) (bool, error) {
270-
// additional network devices.
271171
for _, net := range machineScope.ProxmoxMachine.Spec.Network.NetworkDevices {
272172
for i, ipPool := range net.InterfaceConfig.IPPoolRef {
273173
ipAddresses, err := handleIPAddress(ctx, machineScope, net.Name, i, &ipPool)
@@ -290,64 +190,10 @@ func handleDevices(ctx context.Context, machineScope *scope.MachineScope, addres
290190
addresses[*net.Name] = poolMap
291191
}
292192
}
293-
/*
294-
if net.IPv4PoolRef != nil {
295-
ip, err := handleIPAddressForDevice(ctx, machineScope, net.Name, infrav1.IPV4Format, net.IPv4PoolRef)
296-
if err != nil || ip == "" {
297-
return true, errors.Wrapf(err, "unable to handle IPAddress for device %s", net.Name)
298-
}
299-
300-
addresses[net.Name] = infrav1.IPAddress{
301-
IPV4: ip,
302-
}
303-
}
304-
305-
if net.IPv6PoolRef != nil {
306-
ip, err := handleIPAddressForDevice(ctx, machineScope, net.Name, infrav1.IPV6Format, net.IPv6PoolRef)
307-
if err != nil || ip == "" {
308-
return true, errors.Wrapf(err, "unable to handle IPAddress for device %s", net.Name)
309-
}
310-
311-
addresses[net.Name] = infrav1.IPAddress{
312-
IPV6: ip,
313-
}
314-
}
315-
*/
316-
}
317-
318-
return false, nil
319-
}
320-
321-
/*
322-
func handleAdditionalDevices(ctx context.Context, machineScope *scope.MachineScope, addresses map[string]infrav1.IPAddress) (bool, error) {
323-
// additional network devices.
324-
for _, net := range machineScope.ProxmoxMachine.Spec.Network.AdditionalDevices {
325-
if net.IPv4PoolRef != nil {
326-
ip, err := handleIPAddressForDevice(ctx, machineScope, net.Name, infrav1.IPV4Format, net.IPv4PoolRef)
327-
if err != nil || ip == "" {
328-
return true, errors.Wrapf(err, "unable to handle IPAddress for device %s", net.Name)
329-
}
330-
331-
addresses[net.Name] = infrav1.IPAddress{
332-
IPV4: ip,
333-
}
334-
}
335-
336-
if net.IPv6PoolRef != nil {
337-
ip, err := handleIPAddressForDevice(ctx, machineScope, net.Name, infrav1.IPV6Format, net.IPv6PoolRef)
338-
if err != nil || ip == "" {
339-
return true, errors.Wrapf(err, "unable to handle IPAddress for device %s", net.Name)
340-
}
341-
342-
addresses[net.Name] = infrav1.IPAddress{
343-
IPV6: ip,
344-
}
345-
}
346193
}
347194

348195
return false, nil
349196
}
350-
*/
351197

352198
func isIPV4(ip string) bool {
353199
return netip.MustParseAddr(ip).Is4()

0 commit comments

Comments
 (0)