Skip to content

Commit 9ae3dd1

Browse files
committed
Recover from panics and convert to CNI errors
1 parent b305ca8 commit 9ae3dd1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cni/cni.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const (
1313
CmdAdd = "ADD"
1414
CmdDel = "DEL"
1515

16+
// CNI errors.
17+
ErrRuntime = 100
18+
1619
// DefaultVersion is the CNI version used when no version is specified in a network config file.
1720
defaultVersion = "0.2.0"
1821
)

cni/plugin.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cni
66
import (
77
"fmt"
88
"os"
9+
"runtime"
910

1011
"github.com/Azure/azure-container-networking/common"
1112
"github.com/Azure/azure-container-networking/log"
@@ -85,7 +86,25 @@ func (plugin *Plugin) Uninitialize() {
8586
}
8687

8788
// Execute executes the CNI command.
88-
func (plugin *Plugin) Execute(api PluginApi) error {
89+
func (plugin *Plugin) Execute(api PluginApi) (err error) {
90+
// Recover from panics and convert them to CNI errors.
91+
defer func() {
92+
if r := recover(); r != nil {
93+
buf := make([]byte, 1<<12)
94+
len := runtime.Stack(buf, false)
95+
96+
cniErr := &cniTypes.Error{
97+
Code: ErrRuntime,
98+
Msg: fmt.Sprintf("%v", r),
99+
Details: string(buf[:len]),
100+
}
101+
cniErr.Print()
102+
err = cniErr
103+
104+
log.Printf("[cni] Recovered panic: %v %v\n", cniErr.Msg, cniErr.Details)
105+
}
106+
}()
107+
89108
// Set supported CNI versions.
90109
pluginInfo := cniVers.PluginSupports(supportedVersions...)
91110

0 commit comments

Comments
 (0)