Merge pull request #3841 from weaveworks/fail-controller-if-goroutines-fail

Terminate NPC application if any of the watches panic
This commit is contained in:
Bryan Boreham authored and GitHub committed 2020-08-05 11:35:49 +01:00
commit 911f506b63
1 file changed
+19
+19
View File
@@ -2,8 +2,10 @@ package main
import ( import (
"context" "context"
"fmt"
"os" "os"
"os/signal" "os/signal"
goruntime "runtime"
"syscall" "syscall"
"github.com/coreos/go-iptables/iptables" "github.com/coreos/go-iptables/iptables"
@@ -216,6 +218,14 @@ func hCtx() context.Context {
return context.Background() return context.Background()
} }
func stopOnPanicRecover() {
if r := recover(); r != nil {
buf := make([]byte, 1<<20)
stacklen := goruntime.Stack(buf, false)
handleFatal(fmt.Errorf("panic: %v \n%s", r, buf[:stacklen]))
}
}
func root(cmd *cobra.Command, args []string) { func root(cmd *cobra.Command, args []string) {
var npController cache.Controller var npController cache.Controller
@@ -257,9 +267,11 @@ func root(cmd *cobra.Command, args []string) {
nsController := makeController(client.CoreV1().RESTClient(), "namespaces", &coreapi.Namespace{}, nsController := makeController(client.CoreV1().RESTClient(), "namespaces", &coreapi.Namespace{},
cache.ResourceEventHandlerFuncs{ cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { AddFunc: func(obj interface{}) {
defer stopOnPanicRecover()
handleError(npc.AddNamespace(hCtx(), obj.(*coreapi.Namespace))) handleError(npc.AddNamespace(hCtx(), obj.(*coreapi.Namespace)))
}, },
DeleteFunc: func(obj interface{}) { DeleteFunc: func(obj interface{}) {
defer stopOnPanicRecover()
switch obj := obj.(type) { switch obj := obj.(type) {
case *coreapi.Namespace: case *coreapi.Namespace:
handleError(npc.DeleteNamespace(hCtx(), obj)) handleError(npc.DeleteNamespace(hCtx(), obj))
@@ -271,15 +283,18 @@ func root(cmd *cobra.Command, args []string) {
} }
}, },
UpdateFunc: func(old, new interface{}) { UpdateFunc: func(old, new interface{}) {
defer stopOnPanicRecover()
handleError(npc.UpdateNamespace(hCtx(), old.(*coreapi.Namespace), new.(*coreapi.Namespace))) handleError(npc.UpdateNamespace(hCtx(), old.(*coreapi.Namespace), new.(*coreapi.Namespace)))
}}) }})
podController := makeController(client.CoreV1().RESTClient(), "pods", &coreapi.Pod{}, podController := makeController(client.CoreV1().RESTClient(), "pods", &coreapi.Pod{},
cache.ResourceEventHandlerFuncs{ cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { AddFunc: func(obj interface{}) {
defer stopOnPanicRecover()
handleError(npc.AddPod(hCtx(), obj.(*coreapi.Pod))) handleError(npc.AddPod(hCtx(), obj.(*coreapi.Pod)))
}, },
DeleteFunc: func(obj interface{}) { DeleteFunc: func(obj interface{}) {
defer stopOnPanicRecover()
switch obj := obj.(type) { switch obj := obj.(type) {
case *coreapi.Pod: case *coreapi.Pod:
handleError(npc.DeletePod(hCtx(), obj)) handleError(npc.DeletePod(hCtx(), obj))
@@ -291,14 +306,17 @@ func root(cmd *cobra.Command, args []string) {
} }
}, },
UpdateFunc: func(old, new interface{}) { UpdateFunc: func(old, new interface{}) {
defer stopOnPanicRecover()
handleError(npc.UpdatePod(hCtx(), old.(*coreapi.Pod), new.(*coreapi.Pod))) handleError(npc.UpdatePod(hCtx(), old.(*coreapi.Pod), new.(*coreapi.Pod)))
}}) }})
npHandlers := cache.ResourceEventHandlerFuncs{ npHandlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { AddFunc: func(obj interface{}) {
defer stopOnPanicRecover()
handleError(npc.AddNetworkPolicy(hCtx(), obj)) handleError(npc.AddNetworkPolicy(hCtx(), obj))
}, },
DeleteFunc: func(obj interface{}) { DeleteFunc: func(obj interface{}) {
defer stopOnPanicRecover()
switch obj := obj.(type) { switch obj := obj.(type) {
case cache.DeletedFinalStateUnknown: case cache.DeletedFinalStateUnknown:
// We know this object has gone away, but its final state is no longer // We know this object has gone away, but its final state is no longer
@@ -310,6 +328,7 @@ func root(cmd *cobra.Command, args []string) {
} }
}, },
UpdateFunc: func(old, new interface{}) { UpdateFunc: func(old, new interface{}) {
defer stopOnPanicRecover()
handleError(npc.UpdateNetworkPolicy(hCtx(), old, new)) handleError(npc.UpdateNetworkPolicy(hCtx(), old, new))
}, },
} }