From 1f02326d56e8824a0905bb001292d361d54d4727 Mon Sep 17 00:00:00 2001 From: oilbeater Date: Mon, 27 Nov 2017 15:26:07 +0800 Subject: [PATCH] delete link and ip if err when cmdAdd to avoid resource leak. --- plugins/main/macvlan/macvlan.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plugins/main/macvlan/macvlan.go b/plugins/main/macvlan/macvlan.go index 0589831f9..618fe96da 100644 --- a/plugins/main/macvlan/macvlan.go +++ b/plugins/main/macvlan/macvlan.go @@ -161,11 +161,28 @@ func cmdAdd(args *skel.CmdArgs) error { return err } + // Delete link if err to avoid link leak in this ns + defer func() { + if err != nil { + netns.Do(func(_ ns.NetNS) error { + return ip.DelLinkByName(args.IfName) + }) + } + }() + // run the IPAM plugin and get back the config to apply r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData) if err != nil { return err } + + // Invoke ipam del if err to avoid ip leak + defer func() { + if err != nil { + ipam.ExecDel(n.IPAM.Type, args.StdinData) + } + }() + // Convert whatever the IPAM result was into the current Result type result, err := current.NewResultFromResult(r) if err != nil {