dhcp: make netns path absolute prior to RPC

The dhcp daemon may be running with a different cwd
and so the netns paths need to be absolute. This patch
also refactors the code to pull out the common, RPC
parts, out into a separate function.
This commit is contained in:
Eugene Yakubovich 2015-06-15 12:31:07 -07:00
parent d4d3f914e7
commit 30316e3fa9

View File

@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"net/rpc" "net/rpc"
"os" "os"
"path/filepath"
"github.com/appc/cni/pkg/plugin" "github.com/appc/cni/pkg/plugin"
"github.com/appc/cni/pkg/skel" "github.com/appc/cni/pkg/skel"
@ -34,30 +35,38 @@ func main() {
} }
func cmdAdd(args *skel.CmdArgs) error { func cmdAdd(args *skel.CmdArgs) error {
client, err := rpc.DialHTTP("unix", socketPath) result := plugin.Result{}
if err != nil { if err := rpcCall("DHCP.Allocate", args, &result); err != nil {
return fmt.Errorf("error dialing DHCP daemon: %v", err) return err
} }
result := &plugin.Result{}
err = client.Call("DHCP.Allocate", args, result)
if err != nil {
return fmt.Errorf("error calling DHCP.Add: %v", err)
}
return result.Print() return result.Print()
} }
func cmdDel(args *skel.CmdArgs) error { func cmdDel(args *skel.CmdArgs) error {
result := struct{}{}
if err := rpcCall("DHCP.Release", args, &result); err != nil {
return fmt.Errorf("error dialing DHCP daemon: %v", err)
}
return nil
}
func rpcCall(method string, args *skel.CmdArgs, result interface{}) error {
client, err := rpc.DialHTTP("unix", socketPath) client, err := rpc.DialHTTP("unix", socketPath)
if err != nil { if err != nil {
return fmt.Errorf("error dialing DHCP daemon: %v", err) return fmt.Errorf("error dialing DHCP daemon: %v", err)
} }
dummy := struct{}{} // The daemon may be running under a different working dir
err = client.Call("DHCP.Release", args, &dummy) // so make sure the netns path is absolute.
netns, err := filepath.Abs(args.Netns)
if err != nil { if err != nil {
return fmt.Errorf("error calling DHCP.Del: %v", err) return fmt.Errorf("failed to make %q an absolute path: %v", args.Netns, err)
}
args.Netns = netns
err = client.Call(method, args, result)
if err != nil {
return fmt.Errorf("error calling %v: %v", method, err)
} }
return nil return nil