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:
parent
3775939303
commit
b3e6aa9316
1 changed files with 2 additions and 3 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue