Switch from skel.PluginMain to skel.PluginMainWithError, as the log has to be flushed before os.Exit is called.
@@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"net"
+ "os"
"runtime"
"golang.org/x/net/context"
@@ -296,10 +297,20 @@ func del(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
}
func main() {
- defer log.Flush()
logger.SetupLogger(logger.GetLogFileLocation(defaultLogFilePath))
log.Infof("Starting CNI Plugin %s ...", version)
- skel.PluginMain(cmdAdd, cmdDel, cniSpecVersion.All)
+ exitCode := 0
+
+ if e := skel.PluginMainWithError(cmdAdd, cmdDel, cniSpecVersion.All); e != nil {
+ exitCode = 1
+ log.Error("Failed CNI request: ", e)
+ if err := e.Print(); err != nil {
+ log.Error("Error writing error JSON to stdout: ", err)
+ }
+ }
+
+ log.Flush()
+ os.Exit(exitCode)
}
