pkg/ip: Return correct error if container name provided exists, and test cases
If interface name for a container provided by a user is already present, Veth creation fails with incorrect error. If os.IsExist error is returned by makeVethPair: * Check for peer name, if exists generate another random peer name, * else, IsExist error is due to container interface present, return error. Fixes #155
This commit is contained in:
13
ip/link.go
13
ip/link.go
@ -41,6 +41,13 @@ func makeVethPair(name, peer string, mtu int) (netlink.Link, error) {
|
||||
return veth, nil
|
||||
}
|
||||
|
||||
func peerExists(name string) bool {
|
||||
if _, err := netlink.LinkByName(name); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func makeVeth(name string, mtu int) (peerName string, veth netlink.Link, err error) {
|
||||
for i := 0; i < 10; i++ {
|
||||
peerName, err = RandomVethName()
|
||||
@ -54,7 +61,11 @@ func makeVeth(name string, mtu int) (peerName string, veth netlink.Link, err err
|
||||
return
|
||||
|
||||
case os.IsExist(err):
|
||||
continue
|
||||
if peerExists(peerName) {
|
||||
continue
|
||||
}
|
||||
err = fmt.Errorf("container veth name provided (%v) already exists", name)
|
||||
return
|
||||
|
||||
default:
|
||||
err = fmt.Errorf("failed to make veth pair: %v", err)
|
||||
|
Reference in New Issue
Block a user