From 6d9501b611b6236b3c2ce7b5da6644406619a1ea Mon Sep 17 00:00:00 2001 From: Oleg Vasilev Date: Tue, 5 Nov 2024 14:22:37 +0100 Subject: [PATCH] neonvm-controller: extract acquireOverlayIP (#1131) Signed-off-by: Oleg Vasilev --- pkg/neonvm/controllers/vm_controller.go | 67 ++++++++++++++----------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/pkg/neonvm/controllers/vm_controller.go b/pkg/neonvm/controllers/vm_controller.go index 80a35615d..ca485ac0c 100644 --- a/pkg/neonvm/controllers/vm_controller.go +++ b/pkg/neonvm/controllers/vm_controller.go @@ -311,6 +311,42 @@ func (r *VMReconciler) updateVMStatusMemory( } } +func (r *VMReconciler) acquireOverlayIP(ctx context.Context, vm *vmv1.VirtualMachine) error { + if vm.Spec.ExtraNetwork == nil || !vm.Spec.ExtraNetwork.Enable || len(vm.Status.ExtraNetIP) != 0 { + // If the VM has extra network disabled or already has an IP, do nothing. + return nil + } + + log := log.FromContext(ctx) + + // Create IPAM object + nadName, err := nadIpamName() + if err != nil { + return err + } + nadNamespace, err := nadIpamNamespace() + if err != nil { + return err + } + ipam, err := ipam.New(ctx, nadName, nadNamespace) + if err != nil { + log.Error(err, "failed to create IPAM") + return err + } + defer ipam.Close() + ip, err := ipam.AcquireIP(ctx, vm.Name, vm.Namespace) + if err != nil { + log.Error(err, "fail to acquire IP") + return err + } + message := fmt.Sprintf("Acquired IP %s for overlay network interface", ip.String()) + log.Info(message) + vm.Status.ExtraNetIP = ip.IP.String() + vm.Status.ExtraNetMask = fmt.Sprintf("%d.%d.%d.%d", ip.Mask[0], ip.Mask[1], ip.Mask[2], ip.Mask[3]) + r.Recorder.Event(vm, "Normal", "OverlayNet", message) + return nil +} + func (r *VMReconciler) doReconcile(ctx context.Context, vm *vmv1.VirtualMachine) error { log := log.FromContext(ctx) @@ -342,35 +378,8 @@ func (r *VMReconciler) doReconcile(ctx context.Context, vm *vmv1.VirtualMachine) switch vm.Status.Phase { case "": - // Acquire overlay IP address - if vm.Spec.ExtraNetwork != nil && - vm.Spec.ExtraNetwork.Enable && - len(vm.Status.ExtraNetIP) == 0 { - // Create IPAM object - nadName, err := nadIpamName() - if err != nil { - return err - } - nadNamespace, err := nadIpamNamespace() - if err != nil { - return err - } - ipam, err := ipam.New(ctx, nadName, nadNamespace) - if err != nil { - log.Error(err, "failed to create IPAM") - return err - } - defer ipam.Close() - ip, err := ipam.AcquireIP(ctx, vm.Name, vm.Namespace) - if err != nil { - log.Error(err, "fail to acquire IP") - return err - } - message := fmt.Sprintf("Acquired IP %s for overlay network interface", ip.String()) - log.Info(message) - vm.Status.ExtraNetIP = ip.IP.String() - vm.Status.ExtraNetMask = fmt.Sprintf("%d.%d.%d.%d", ip.Mask[0], ip.Mask[1], ip.Mask[2], ip.Mask[3]) - r.Recorder.Event(vm, "Normal", "OverlayNet", message) + if err := r.acquireOverlayIP(ctx, vm); err != nil { + return err } // VirtualMachine just created, change Phase to "Pending" vm.Status.Phase = vmv1.VmPending