Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Terminate NPC application if any of the watches panic #3792

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion prog/weave-npc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,17 @@ func ipsetExist(ips ipset.Interface, name ipset.Name) (bool, error) {
return false, nil
}

func stopOnPanicRecover(stopChan chan struct{}) {
if r := recover(); r != nil {
os.Exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the user experience if this happens? Can they understand where the real problem occurred?

}
}

func root(cmd *cobra.Command, args []string) {
var npController cache.Controller
var (
npController cache.Controller
stopChan chan struct{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopChan seems to be left-over and unneeded now.

)

common.SetLogLevel(logLevel)
if nodeName == "" {
Expand Down Expand Up @@ -259,9 +268,11 @@ func root(cmd *cobra.Command, args []string) {
nsController := makeController(client.Core().RESTClient(), "namespaces", &coreapi.Namespace{},
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
defer stopOnPanicRecover(stopChan)
handleError(npc.AddNamespace(obj.(*coreapi.Namespace)))
},
DeleteFunc: func(obj interface{}) {
defer stopOnPanicRecover(stopChan)
switch obj := obj.(type) {
case *coreapi.Namespace:
handleError(npc.DeleteNamespace(obj))
Expand All @@ -273,15 +284,18 @@ func root(cmd *cobra.Command, args []string) {
}
},
UpdateFunc: func(old, new interface{}) {
defer stopOnPanicRecover(stopChan)
handleError(npc.UpdateNamespace(old.(*coreapi.Namespace), new.(*coreapi.Namespace)))
}})

podController := makeController(client.Core().RESTClient(), "pods", &coreapi.Pod{},
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
defer stopOnPanicRecover(stopChan)
handleError(npc.AddPod(obj.(*coreapi.Pod)))
},
DeleteFunc: func(obj interface{}) {
defer stopOnPanicRecover(stopChan)
switch obj := obj.(type) {
case *coreapi.Pod:
handleError(npc.DeletePod(obj))
Expand All @@ -293,14 +307,17 @@ func root(cmd *cobra.Command, args []string) {
}
},
UpdateFunc: func(old, new interface{}) {
defer stopOnPanicRecover(stopChan)
handleError(npc.UpdatePod(old.(*coreapi.Pod), new.(*coreapi.Pod)))
}})

npHandlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
defer stopOnPanicRecover(stopChan)
handleError(npc.AddNetworkPolicy(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
Expand All @@ -312,6 +329,7 @@ func root(cmd *cobra.Command, args []string) {
}
},
UpdateFunc: func(old, new interface{}) {
defer stopOnPanicRecover(stopChan)
handleError(npc.UpdateNetworkPolicy(old, new))
},
}
Expand Down