Parcourir la source

libnetwork/osl: Namespace.DeleteNeighbor: remove intermediate vars

- store linkIndex in a local variable so that it can be reused
- remove / rename some intermediate vars that shadowed existing declaration

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn il y a 1 an
Parent
commit
cd204f1118
1 fichiers modifiés avec 18 ajouts et 28 suppressions
  1. 18 28
      libnetwork/osl/neigh_linux.go

+ 18 - 28
libnetwork/osl/neigh_linux.go

@@ -46,11 +46,6 @@ func (n *Namespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
 
 // DeleteNeighbor deletes neighbor entry from the sandbox.
 func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error {
-	var (
-		iface netlink.Link
-		err   error
-	)
-
 	nh := n.findNeighbor(dstIP, dstMac)
 	if nh == nil {
 		return NeighborSearchError{dstIP, dstMac, false}
@@ -60,28 +55,27 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
 	nlh := n.nlHandle
 	n.Unlock()
 
+	var linkIndex int
 	if nh.linkDst != "" {
-		iface, err = nlh.LinkByName(nh.linkDst)
+		iface, err := nlh.LinkByName(nh.linkDst)
 		if err != nil {
 			return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err)
 		}
+		linkIndex = iface.Attrs().Index
 	}
 
 	nlnh := &netlink.Neigh{
-		IP:     dstIP,
-		State:  netlink.NUD_PERMANENT,
-		Family: nh.family,
+		LinkIndex: linkIndex,
+		IP:        dstIP,
+		State:     netlink.NUD_PERMANENT,
+		Family:    nh.family,
 	}
 
-	if nlnh.Family > 0 {
+	if nh.family > 0 {
 		nlnh.HardwareAddr = dstMac
 		nlnh.Flags = netlink.NTF_SELF
 	}
 
-	if nh.linkDst != "" {
-		nlnh.LinkIndex = iface.Attrs().Index
-	}
-
 	// If the kernel deletion fails for the neighbor entry still remove it
 	// from the namespace cache, otherwise kernel update can fail if the
 	// neighbor moves back to the same host again.
@@ -90,25 +84,21 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
 	}
 
 	// Delete the dynamic entry in the bridge
-	if nlnh.Family > 0 {
-		nlnh := &netlink.Neigh{
-			IP:     dstIP,
-			Family: nh.family,
-		}
-
-		nlnh.HardwareAddr = dstMac
-		nlnh.Flags = netlink.NTF_MASTER
-		if nh.linkDst != "" {
-			nlnh.LinkIndex = iface.Attrs().Index
-		}
-		if err := nlh.NeighDel(nlnh); err != nil && !errors.Is(err, os.ErrNotExist) {
+	if nh.family > 0 {
+		if err := nlh.NeighDel(&netlink.Neigh{
+			LinkIndex:    linkIndex,
+			IP:           dstIP,
+			Family:       nh.family,
+			HardwareAddr: dstMac,
+			Flags:        netlink.NTF_MASTER,
+		}); err != nil && !errors.Is(err, os.ErrNotExist) {
 			log.G(context.TODO()).WithError(err).Warn("error while deleting neighbor entry")
 		}
 	}
 
 	n.Lock()
-	for i, nh := range n.neighbors {
-		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
+	for i, neighbor := range n.neighbors {
+		if neighbor.dstIP.Equal(dstIP) && bytes.Equal(neighbor.dstMac, dstMac) {
 			n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
 			break
 		}