Skip to content

Commit

Permalink
refactor: use all zero ip from net package
Browse files Browse the repository at this point in the history
Signed-off-by: Zespre Chang <[email protected]>
  • Loading branch information
starbops committed Jan 24, 2024
1 parent 005fc3e commit ec9367c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/vmnetcfg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vmnetcfg
import (
"context"
"fmt"
"net"
"reflect"

"github.com/rancher/wrangler/pkg/kv"
Expand All @@ -16,7 +17,6 @@ import (
ctlnetworkv1 "github.com/harvester/vm-dhcp-controller/pkg/generated/controllers/network.harvesterhci.io/v1alpha1"
"github.com/harvester/vm-dhcp-controller/pkg/ipam"
"github.com/harvester/vm-dhcp-controller/pkg/metrics"
"github.com/harvester/vm-dhcp-controller/pkg/util"
)

const controllerName = "vm-dhcp-vmnetcfg-controller"
Expand Down Expand Up @@ -177,7 +177,7 @@ func (h *Handler) Allocate(vmNetCfg *networkv1.VirtualMachineNetworkConfig, stat

// Allocate new IP

dIP := util.UnspecifiedIPAddress
dIP := net.IPv4zero.String()
if nc.IPAddress != nil {
dIP = *nc.IPAddress
}
Expand Down Expand Up @@ -329,7 +329,7 @@ func findIPAddressFromNetworkConfigStatusByMACAddress(ncStatuses []networkv1.Net
return ncStatus.AllocatedIPAddress, nil
}
}
return util.UnspecifiedIPAddress, fmt.Errorf("could not find allocated ip for mac %s", macAddress)
return net.IPv4zero.String(), fmt.Errorf("could not find allocated ip for mac %s", macAddress)
}

func updateAllNetworkConfigState(ncStatuses []networkv1.NetworkConfigStatus) {
Expand Down
11 changes: 5 additions & 6 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"sync"

"github.com/harvester/vm-dhcp-controller/pkg/util"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -110,7 +109,7 @@ func (a *IPAllocator) AllocateIP(name string, ipAddress string) (string, error)
return "", fmt.Errorf("network %s does not exist", name)
}
if ipAddress == "" {
ipAddress = util.UnspecifiedIPAddress
ipAddress = net.IPv4zero.String()
}

designatedIP := net.ParseIP(ipAddress)
Expand All @@ -119,7 +118,7 @@ func (a *IPAllocator) AllocateIP(name string, ipAddress string) (string, error)
ok := a.ipam[name].ipNet.Contains(designatedIP)
if !ok {
subnetMask, _ := a.ipam[name].ipNet.Mask.Size()
return util.UnspecifiedIPAddress, fmt.Errorf(
return net.IPv4zero.String(), fmt.Errorf(
"designated ip %s is not in subnet %s/%d",
designatedIP.String(),
a.ipam[name].ipNet.IP.String(),
Expand All @@ -128,15 +127,15 @@ func (a *IPAllocator) AllocateIP(name string, ipAddress string) (string, error)
}

if a.ipam[name].broadcast.Equal(designatedIP) {
return util.UnspecifiedIPAddress, fmt.Errorf("designated ip %s equals broadcast ip address %s", designatedIP.String(), a.ipam[name].broadcast.String())
return net.IPv4zero.String(), fmt.Errorf("designated ip %s equals broadcast ip address %s", designatedIP.String(), a.ipam[name].broadcast.String())
}
}

for ip, isAllocated := range a.ipam[name].ips {
if !designatedIP.IsUnspecified() {
if ip == designatedIP.String() {
if isAllocated {
return util.UnspecifiedIPAddress, fmt.Errorf("designated ip %s is already allocated", designatedIP.String())
return net.IPv4zero.String(), fmt.Errorf("designated ip %s is already allocated", designatedIP.String())
} else {
a.ipam[name].ips[ip] = true
return ip, nil
Expand All @@ -150,7 +149,7 @@ func (a *IPAllocator) AllocateIP(name string, ipAddress string) (string, error)
}
}

return util.UnspecifiedIPAddress, fmt.Errorf("no more ip addresses left in network %s ipam", name)
return net.IPv4zero.String(), fmt.Errorf("no more ip addresses left in network %s ipam", name)
}

func (a *IPAllocator) DeallocateIP(name, ipAddress string) error {
Expand Down
4 changes: 0 additions & 4 deletions pkg/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ package util
const (
ExcludedMark = "EXCLUDED"
)

var (
UnspecifiedIPAddress = "0.0.0.0"
)

0 comments on commit ec9367c

Please sign in to comment.