Propagate json error object to the caller

When plugin errors out, it prints out a JSON object to stdout
describing the failure. This object needs to be propagated out
through the plugins and to the container runtime. This change
also adds Print method to both the result and error structs
for easy serialization to stdout.
This commit is contained in:
Eugene Yakubovich
2015-06-01 17:34:00 -07:00
parent 54dfc9ef5d
commit 33a575f49d
10 changed files with 79 additions and 37 deletions

View File

@ -64,12 +64,12 @@ func PluginMain(cmdAdd, cmdDel func(_ *CmdArgs) error) {
}
if argsMissing {
die("required env variables missing")
dieMsg("required env variables missing")
}
stdinData, err := ioutil.ReadAll(os.Stdin)
if err != nil {
die("error reading from stdin: %v", err)
dieMsg("error reading from stdin: %v", err)
}
cmdArgs := &CmdArgs{
@ -89,18 +89,29 @@ func PluginMain(cmdAdd, cmdDel func(_ *CmdArgs) error) {
err = cmdDel(cmdArgs)
default:
die("unknown CNI_COMMAND: %v", cmd)
dieMsg("unknown CNI_COMMAND: %v", cmd)
}
if err != nil {
die(err.Error())
if e, ok := err.(*plugin.Error); ok {
// don't wrap Error in Error
dieErr(e)
}
dieMsg(err.Error())
}
}
func die(f string, args ...interface{}) {
plugin.PrintError(&plugin.Error{
func dieMsg(f string, args ...interface{}) {
e := &plugin.Error{
Code: 100,
Msg: fmt.Sprintf(f, args...),
})
}
dieErr(e)
}
func dieErr(e *plugin.Error) {
if err := e.Print(); err != nil {
log.Print("Error writing error JSON to stdout: ", err)
}
os.Exit(1)
}