Recover from panics and convert to CNI errors

This commit is contained in:
Onur Filiz 2017-06-16 12:03:54 -07:00
Родитель b305ca8ba6
Коммит 9ae3dd149c
2 изменённых файлов: 23 добавлений и 1 удалений

Просмотреть файл

@ -13,6 +13,9 @@ const (
CmdAdd = "ADD"
CmdDel = "DEL"
// CNI errors.
ErrRuntime = 100
// DefaultVersion is the CNI version used when no version is specified in a network config file.
defaultVersion = "0.2.0"
)

Просмотреть файл

@ -6,6 +6,7 @@ package cni
import (
"fmt"
"os"
"runtime"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
@ -85,7 +86,25 @@ func (plugin *Plugin) Uninitialize() {
}
// Execute executes the CNI command.
func (plugin *Plugin) Execute(api PluginApi) error {
func (plugin *Plugin) Execute(api PluginApi) (err error) {
// Recover from panics and convert them to CNI errors.
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 1<<12)
len := runtime.Stack(buf, false)
cniErr := &cniTypes.Error{
Code: ErrRuntime,
Msg: fmt.Sprintf("%v", r),
Details: string(buf[:len]),
}
cniErr.Print()
err = cniErr
log.Printf("[cni] Recovered panic: %v %v\n", cniErr.Msg, cniErr.Details)
}
}()
// Set supported CNI versions.
pluginInfo := cniVers.PluginSupports(supportedVersions...)