Skip to content

Commit

Permalink
neonvm-controller: extract acquireOverlayIP (#1131)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Vasilev <[email protected]>
  • Loading branch information
Omrigan committed Nov 20, 2024
1 parent a65e1f8 commit 6d9501b
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions pkg/neonvm/controllers/vm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6d9501b

Please sign in to comment.