Skip to content

Commit

Permalink
Merge pull request containernetworking#60 from eyakubovich/fix-plugin…
Browse files Browse the repository at this point in the history
…-del

bug fix: exec of DEL cmd caused JSON decode error
  • Loading branch information
squaremo committed Sep 22, 2015
2 parents cc918a1 + 9ea5693 commit 2a58bd9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
15 changes: 6 additions & 9 deletions libcni/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,23 @@ type CNIConfig struct {
}

func (c *CNIConfig) AddNetwork(net *NetworkConfig, rt *RuntimeConf) (*types.Result, error) {
return c.execPlugin("ADD", net, rt)
pluginPath := invoke.FindInPath(net.Network.Type, c.Path)
return invoke.ExecPluginWithResult(pluginPath, net.Bytes, c.args("ADD", rt))
}

func (c *CNIConfig) DelNetwork(net *NetworkConfig, rt *RuntimeConf) error {
_, err := c.execPlugin("DEL", net, rt)
return err
pluginPath := invoke.FindInPath(net.Network.Type, c.Path)
return invoke.ExecPluginWithoutResult(pluginPath, net.Bytes, c.args("DEL", rt))
}

// =====

func (c *CNIConfig) execPlugin(action string, conf *NetworkConfig, rt *RuntimeConf) (*types.Result, error) {
pluginPath := invoke.FindInPath(conf.Network.Type, c.Path)

args := &invoke.Args{
func (c *CNIConfig) args(action string, rt *RuntimeConf) *invoke.Args {
return &invoke.Args{
Command: action,
ContainerID: rt.ContainerID,
NetNS: rt.NetNS,
PluginArgs: rt.Args,
IfName: rt.IfName,
Path: strings.Join(c.Path, ":"),
}
return invoke.ExecPlugin(pluginPath, conf.Bytes, args)
}
22 changes: 18 additions & 4 deletions pkg/invoke/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,23 @@ func pluginErr(err error, output []byte) error {
return err
}

func ExecPlugin(pluginPath string, netconf []byte, args CNIArgs) (*types.Result, error) {
func ExecPluginWithResult(pluginPath string, netconf []byte, args CNIArgs) (*types.Result, error) {
stdoutBytes, err := execPlugin(pluginPath, netconf, args)
if err != nil {
return nil, err
}

res := &types.Result{}
err = json.Unmarshal(stdoutBytes, res)
return res, err
}

func ExecPluginWithoutResult(pluginPath string, netconf []byte, args CNIArgs) error {
_, err := execPlugin(pluginPath, netconf, args)
return err
}

func execPlugin(pluginPath string, netconf []byte, args CNIArgs) ([]byte, error) {
if pluginPath == "" {
return nil, fmt.Errorf("could not find %q plugin", filepath.Base(pluginPath))
}
Expand All @@ -60,7 +76,5 @@ func ExecPlugin(pluginPath string, netconf []byte, args CNIArgs) (*types.Result,
return nil, pluginErr(err, stdout.Bytes())
}

res := &types.Result{}
err := json.Unmarshal(stdout.Bytes(), res)
return res, err
return stdout.Bytes(), nil
}
5 changes: 2 additions & 3 deletions pkg/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ func ExecAdd(plugin string, netconf []byte) (*types.Result, error) {
if os.Getenv("CNI_COMMAND") != "ADD" {
return nil, fmt.Errorf("CNI_COMMAND is not ADD")
}
return invoke.ExecPlugin(invoke.Find(plugin), netconf, invoke.ArgsFromEnv())
return invoke.ExecPluginWithResult(invoke.Find(plugin), netconf, invoke.ArgsFromEnv())
}

func ExecDel(plugin string, netconf []byte) error {
if os.Getenv("CNI_COMMAND") != "DEL" {
return fmt.Errorf("CNI_COMMAND is not DEL")
}
_, err := invoke.ExecPlugin(invoke.Find(plugin), netconf, invoke.ArgsFromEnv())
return err
return invoke.ExecPluginWithoutResult(invoke.Find(plugin), netconf, invoke.ArgsFromEnv())
}

// ConfigureIface takes the result of IPAM plugin and
Expand Down

0 comments on commit 2a58bd9

Please sign in to comment.