Skip to content

Commit

Permalink
IpAssigner UT Linux
Browse files Browse the repository at this point in the history
Signed-off-by: Rajnish Kumar <rajnishk1@vmware.com>
  • Loading branch information
rajnkamr committed Jan 10, 2024
1 parent 830cf55 commit e7c5d4c
Show file tree
Hide file tree
Showing 4 changed files with 595 additions and 7 deletions.
1 change: 1 addition & 0 deletions hack/update-codegen-dockerized.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ MOCKGEN_TARGETS=(
"pkg/agent/querier AgentQuerier testing"
"pkg/agent/route Interface testing"
"pkg/agent/ipassigner IPAssigner testing"
"pkg/agent/ipassigner/responder Responder testing"
"pkg/agent/secondarynetwork/podwatch InterfaceConfigurator,IPAMAllocator testing"
"pkg/agent/servicecidr Interface testing"
"pkg/agent/util/ipset Interface testing"
Expand Down
29 changes: 22 additions & 7 deletions pkg/agent/ipassigner/ip_assigner_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import (
crdv1b1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
)

type netlinkAddrAddel func(link netlink.Link, addr *netlink.Addr) error

//type advertiseFunc func(ip net.IP, externalInterface *net.Interface)



// VLAN interfaces created by antrea-agent will be named with the prefix.
// For example, when VLAN ID is 10, the name will be antrea-ext.10.
// It can be used to determine whether it's safe to delete an interface when it's no longer used.
Expand All @@ -56,6 +62,8 @@ type assignee struct {
ndpResponder responder.Responder
// ips tracks IPs that have been assigned to this assignee.
ips sets.Set[string]
advertiseFn func(ip net.IP, externalInterface *net.Interface)
netlinkAddel netlinkAddrAddel
}

// deletable returns whether this assignee can be safely deleted.
Expand Down Expand Up @@ -89,7 +97,10 @@ func (as *assignee) assign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
// If there is a real link, add the IP to its address list.
if as.link != nil {
addr := getIPNet(ip, subnetInfo)
if err := netlink.AddrAdd(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if as.netlinkAddel == nil {
as.netlinkAddel = netlink.AddrAdd
}
if err := as.netlinkAddel(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EEXIST) {
return fmt.Errorf("failed to add IP %v to interface %s: %v", addr, as.link.Attrs().Name, err)
} else {
Expand All @@ -111,20 +122,20 @@ func (as *assignee) assign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
}
}
// Always advertise the IP when the IP is newly assigned to this Node.
as.advertise(ip)
as.advertiseFn(ip, as.logicalInterface)
as.ips.Insert(ip.String())
return nil
}

func (as *assignee) advertise(ip net.IP) {
func advertise(ip net.IP, logicalInterface *net.Interface) {
if utilnet.IsIPv4(ip) {
klog.V(2).InfoS("Sending gratuitous ARP", "ip", ip)
if err := arping.GratuitousARPOverIface(ip, as.logicalInterface); err != nil {
if err := arping.GratuitousARPOverIface(ip, logicalInterface); err != nil {
klog.ErrorS(err, "Failed to send gratuitous ARP", "ip", ip)
}
} else {
klog.V(2).InfoS("Sending neighbor advertisement", "ip", ip)
if err := ndp.NeighborAdvertisement(ip, as.logicalInterface); err != nil {
if err := ndp.NeighborAdvertisement(ip, logicalInterface); err != nil {
klog.ErrorS(err, "Failed to send neighbor advertisement", "ip", ip)
}
}
Expand All @@ -134,7 +145,10 @@ func (as *assignee) unassign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
// If there is a real link, delete the IP from its address list.
if as.link != nil {
addr := getIPNet(ip, subnetInfo)
if err := netlink.AddrDel(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if as.netlinkAddel == nil {
as.netlinkAddel = netlink.AddrDel
}
if err := as.netlinkAddel(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EADDRNOTAVAIL) {
return fmt.Errorf("failed to delete IP %v from interface %s: %v", ip, as.link.Attrs().Name, err)
} else {
Expand Down Expand Up @@ -267,6 +281,7 @@ func NewIPAssigner(nodeTransportInterface string, dummyDeviceName string) (IPAss
for _, vlan := range vlans {
a.addVLANAssignee(vlan, int32(vlan.VlanId))
}
a.defaultAssignee.advertiseFn = advertise
return a, nil
}

Expand Down Expand Up @@ -362,7 +377,7 @@ func (a *ipAssigner) AssignIP(ip string, subnetInfo *crdv1b1.SubnetInfo, forceAd
if crdv1b1.CompareSubnetInfo(subnetInfo, oldSubnetInfo, true) {
klog.V(2).InfoS("The IP is already assigned", "ip", ip)
if forceAdvertise {
as.advertise(parsedIP)
as.advertiseFn(parsedIP, as.logicalInterface)
}
return false, nil
}
Expand Down
Loading

0 comments on commit e7c5d4c

Please sign in to comment.