Prechádzať zdrojové kódy

libnetwork/netutils: clean up GenerateIfaceName

netlink offers the netlink.LinkNotFoundError type, which we can use with
errors.As() to detect a unused link name.

Additionally, early return if GenerateRandomName fails, as reading
random bytes should be a highly reliable operation, and otherwise the
error would be swallowed by the fall-through return.

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
Bjorn Neergaard 2 rokov pred
rodič
commit
b3e6aa9316
1 zmenil súbory, kde vykonal 2 pridanie a 3 odobranie
  1. 2 3
      libnetwork/netutils/utils_linux.go

+ 2 - 3
libnetwork/netutils/utils_linux.go

@@ -8,7 +8,6 @@ package netutils
 import (
 import (
 	"net"
 	"net"
 	"os"
 	"os"
-	"strings"
 
 
 	"github.com/docker/docker/libnetwork/ipamutils"
 	"github.com/docker/docker/libnetwork/ipamutils"
 	"github.com/docker/docker/libnetwork/ns"
 	"github.com/docker/docker/libnetwork/ns"
@@ -49,11 +48,11 @@ func GenerateIfaceName(nlh *netlink.Handle, prefix string, len int) (string, err
 	for i := 0; i < 3; i++ {
 	for i := 0; i < 3; i++ {
 		name, err := GenerateRandomName(prefix, len)
 		name, err := GenerateRandomName(prefix, len)
 		if err != nil {
 		if err != nil {
-			continue
+			return "", err
 		}
 		}
 		_, err = linkByName(name)
 		_, err = linkByName(name)
 		if err != nil {
 		if err != nil {
-			if strings.Contains(err.Error(), "not found") {
+			if errors.As(err, &netlink.LinkNotFoundError{}) {
 				return name, nil
 				return name, nil
 			}
 			}
 			return "", err
 			return "", err