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>
This commit is contained in:
Bjorn Neergaard 2023-01-21 16:23:07 -07:00
parent 3775939303
commit b3e6aa9316
No known key found for this signature in database

View file

@ -8,7 +8,6 @@ package netutils
import (
"net"
"os"
"strings"
"github.com/docker/docker/libnetwork/ipamutils"
"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++ {
name, err := GenerateRandomName(prefix, len)
if err != nil {
continue
return "", err
}
_, err = linkByName(name)
if err != nil {
if strings.Contains(err.Error(), "not found") {
if errors.As(err, &netlink.LinkNotFoundError{}) {
return name, nil
}
return "", err