Simply run os.Exit on panic of goroutine

This commit is contained in:
naemono authored and Bryan Boreham committed 2020-08-05 09:40:02 +00:00
1 parent 9cf4104de7
commit 269786abf5
1 file changed
+19 -1
+19 -1
View File
@@ -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))
},
}