Skip to content

Commit

Permalink
refactor: improve vmnetcfg allocate reconcile loop
Browse files Browse the repository at this point in the history
Signed-off-by: Zespre Chang <zespre.chang@suse.com>
Co-authored-by: Jack Yu <jack.yu@suse.com>
  • Loading branch information
starbops and Yu-Jack committed Feb 15, 2024
1 parent 6380f8f commit 2057fe0
Showing 1 changed file with 18 additions and 61 deletions.
79 changes: 18 additions & 61 deletions pkg/controller/vmnetcfg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,79 +119,35 @@ func (h *Handler) Allocate(vmNetCfg *networkv1.VirtualMachineNetworkConfig, stat
if err != nil {
return status, err
}

var ip string

if exists {
// Recover IP from cache

ip, err := h.cacheAllocator.GetIPByMAC(nc.NetworkName, nc.MACAddress)
ip, err = h.cacheAllocator.GetIPByMAC(nc.NetworkName, nc.MACAddress)
if err != nil {
return status, err
}

// Prepare VirtualMachineNetworkConfig status
ncStatus := networkv1.NetworkConfigStatus{
AllocatedIPAddress: ip,
MACAddress: nc.MACAddress,
NetworkName: nc.NetworkName,
State: networkv1.AllocatedState,
} else {
dIP := net.IPv4zero.String()
if nc.IPAddress != nil {
dIP = *nc.IPAddress
}
ncStatuses = append(ncStatuses, ncStatus)

// Update VirtualMachineNetworkConfig metrics
h.metricsAllocator.UpdateVmNetCfgStatus(
fmt.Sprintf("%s/%s", vmNetCfg.Namespace, vmNetCfg.Name),
ncStatus.NetworkName,
ncStatus.MACAddress,
ncStatus.AllocatedIPAddress,
string(ncStatus.State),
)

// Update IPPool status
ipPoolCpy := ipPool.DeepCopy()

ipv4Status := ipPoolCpy.Status.IPv4
if ipv4Status == nil {
ipv4Status = new(networkv1.IPv4Status)
// Recover IP from status (resume from paused state)
if oIP, err := findIPAddressFromNetworkConfigStatusByMACAddress(vmNetCfg.Status.NetworkConfig, nc.MACAddress); err == nil {
dIP = oIP
}

allocated := ipv4Status.Allocated
if allocated == nil {
allocated = make(map[string]string)
// Allocate new IP
ip, err = h.ipAllocator.AllocateIP(nc.NetworkName, dIP)
if err != nil {
return status, err
}

allocated[ip] = nc.MACAddress

ipv4Status.Allocated = allocated
ipPoolCpy.Status.IPv4 = ipv4Status

if !reflect.DeepEqual(ipPoolCpy, ipPool) {
logrus.Infof("(vmnetcfg.Allocate) update ippool %s/%s", ipPool.Namespace, ipPool.Name)
ipPoolCpy.Status.LastUpdate = metav1.Now()
if _, err = h.ippoolClient.UpdateStatus(ipPoolCpy); err != nil {
return status, err
}
if err := h.cacheAllocator.AddMAC(nc.NetworkName, nc.MACAddress, ip); err != nil {
return status, err
}

continue
}

// Allocate new IP

dIP := net.IPv4zero.String()
if nc.IPAddress != nil {
dIP = *nc.IPAddress
}
// Recover IP from status (resume from paused state)
if oIP, err := findIPAddressFromNetworkConfigStatusByMACAddress(vmNetCfg.Status.NetworkConfig, nc.MACAddress); err == nil {
dIP = oIP
}

ip, err := h.ipAllocator.AllocateIP(nc.NetworkName, dIP)
if err != nil {
return status, err
}

if err := h.cacheAllocator.AddMAC(nc.NetworkName, nc.MACAddress, ip); err != nil {
return status, err
}

// Prepare VirtualMachineNetworkConfig status
Expand All @@ -201,6 +157,7 @@ func (h *Handler) Allocate(vmNetCfg *networkv1.VirtualMachineNetworkConfig, stat
NetworkName: nc.NetworkName,
State: networkv1.AllocatedState,
}

ncStatuses = append(ncStatuses, ncStatus)

// Update VirtualMachineNetworkConfig metrics
Expand Down

0 comments on commit 2057fe0

Please sign in to comment.