Remove veth on error

This commit is contained in:
Bryan Boreham committed 2016-05-25 17:17:19 +01:00
1 parent f11d91badf
commit d59c170cd5
1 file changed
+11 -6
+11 -6
View File
@@ -28,29 +28,34 @@ func CreateAndAttachVeth(localName, peerName, bridgeName string, mtu int) (*netl
return nil, fmt.Errorf(`could not create veth pair %s-%s: %s`, local.Name, local.PeerName, err)
}
cleanup := func(format string, a ...interface{}) (*netlink.Veth, error) {
netlink.LinkDel(local)
return nil, fmt.Errorf(format, a...)
}
switch maybeBridge.(type) {
case *netlink.Bridge:
if err := netlink.LinkSetMasterByIndex(local, maybeBridge.Attrs().Index); err != nil {
return nil, fmt.Errorf(`unable to set master of %s: %s`, local.Name, err)
return cleanup(`unable to set master of %s: %s`, local.Name, err)
}
case *netlink.GenericLink:
if maybeBridge.Type() != "openvswitch" {
return nil, fmt.Errorf(`device "%s" is of type "%s"`, bridgeName, maybeBridge.Type())
return cleanup(`device "%s" is of type "%s"`, bridgeName, maybeBridge.Type())
}
if err := odp.AddDatapathInterface(bridgeName, local.Name); err != nil {
return nil, fmt.Errorf(`failed to attach %s to device "%s": %s`, local.Name, bridgeName, err)
return cleanup(`failed to attach %s to device "%s": %s`, local.Name, bridgeName, err)
}
case *netlink.Device:
// Assume it's our openvswitch device, and the kernel has not been updated to report the kind.
if err := odp.AddDatapathInterface(bridgeName, local.Name); err != nil {
return nil, fmt.Errorf(`failed to attach %s to device "%s": %s`, local.Name, bridgeName, err)
return cleanup(`failed to attach %s to device "%s": %s`, local.Name, bridgeName, err)
}
default:
return nil, fmt.Errorf(`device "%s" is not a bridge`, bridgeName)
return cleanup(`device "%s" is not a bridge`, bridgeName)
}
if err := netlink.LinkSetUp(local); err != nil {
return nil, fmt.Errorf("unable to bring veth up: %s", err)
return cleanup("unable to bring veth up: %s", err)
}
return local, nil