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>
| ... | ... |
@@ -8,7 +8,6 @@ package netutils |
| 8 | 8 |
import ( |
| 9 | 9 |
"net" |
| 10 | 10 |
"os" |
| 11 |
- "strings" |
|
| 12 | 11 |
|
| 13 | 12 |
"github.com/docker/docker/libnetwork/ipamutils" |
| 14 | 13 |
"github.com/docker/docker/libnetwork/ns" |
| ... | ... |
@@ -49,11 +48,11 @@ func GenerateIfaceName(nlh *netlink.Handle, prefix string, len int) (string, err |
| 49 | 49 |
for i := 0; i < 3; i++ {
|
| 50 | 50 |
name, err := GenerateRandomName(prefix, len) |
| 51 | 51 |
if err != nil {
|
| 52 |
- continue |
|
| 52 |
+ return "", err |
|
| 53 | 53 |
} |
| 54 | 54 |
_, err = linkByName(name) |
| 55 | 55 |
if err != nil {
|
| 56 |
- if strings.Contains(err.Error(), "not found") {
|
|
| 56 |
+ if errors.As(err, &netlink.LinkNotFoundError{}) {
|
|
| 57 | 57 |
return name, nil |
| 58 | 58 |
} |
| 59 | 59 |
return "", err |