From 269786abf51b43f1d935c21c08400eeeb03c2e3e Mon Sep 17 00:00:00 2001 From: naemono Date: Wed, 18 Mar 2020 15:05:53 -0500 Subject: [PATCH] Simply run os.Exit on panic of goroutine --- prog/weave-npc/main.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/prog/weave-npc/main.go b/prog/weave-npc/main.go index 7eb987fb..efd6af13 100644 --- a/prog/weave-npc/main.go +++ b/prog/weave-npc/main.go @@ -216,8 +216,17 @@ func hCtx() context.Context { return context.Background() } +func stopOnPanicRecover(stopChan chan struct{}) { + if r := recover(); r != nil { + os.Exit(1) + } +} + func root(cmd *cobra.Command, args []string) { - var npController cache.Controller + var ( + npController cache.Controller + stopChan chan struct{} + ) common.SetLogLevel(logLevel) if nodeName == "" { @@ -257,9 +266,11 @@ func root(cmd *cobra.Command, args []string) { nsController := makeController(client.CoreV1().RESTClient(), "namespaces", &coreapi.Namespace{}, cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { + defer stopOnPanicRecover(stopChan) handleError(npc.AddNamespace(hCtx(), obj.(*coreapi.Namespace))) }, DeleteFunc: func(obj interface{}) { + defer stopOnPanicRecover(stopChan) switch obj := obj.(type) { case *coreapi.Namespace: handleError(npc.DeleteNamespace(hCtx(), obj)) @@ -271,15 +282,18 @@ func root(cmd *cobra.Command, args []string) { } }, UpdateFunc: func(old, new interface{}) { + defer stopOnPanicRecover(stopChan) handleError(npc.UpdateNamespace(hCtx(), old.(*coreapi.Namespace), new.(*coreapi.Namespace))) }}) podController := makeController(client.CoreV1().RESTClient(), "pods", &coreapi.Pod{}, cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { + defer stopOnPanicRecover(stopChan) handleError(npc.AddPod(hCtx(), obj.(*coreapi.Pod))) }, DeleteFunc: func(obj interface{}) { + defer stopOnPanicRecover(stopChan) switch obj := obj.(type) { case *coreapi.Pod: handleError(npc.DeletePod(hCtx(), obj)) @@ -291,14 +305,17 @@ func root(cmd *cobra.Command, args []string) { } }, UpdateFunc: func(old, new interface{}) { + defer stopOnPanicRecover(stopChan) handleError(npc.UpdatePod(hCtx(), old.(*coreapi.Pod), new.(*coreapi.Pod))) }}) npHandlers := cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { + defer stopOnPanicRecover(stopChan) handleError(npc.AddNetworkPolicy(hCtx(), obj)) }, DeleteFunc: func(obj interface{}) { + defer stopOnPanicRecover(stopChan) switch obj := obj.(type) { case cache.DeletedFinalStateUnknown: // We know this object has gone away, but its final state is no longer @@ -310,6 +327,7 @@ func root(cmd *cobra.Command, args []string) { } }, UpdateFunc: func(old, new interface{}) { + defer stopOnPanicRecover(stopChan) handleError(npc.UpdateNetworkPolicy(hCtx(), old, new)) }, }