From d016c18e1c8ec024b17ae9cac92fcc556e8e60d1 Mon Sep 17 00:00:00 2001 From: hty Date: Thu, 11 Mar 2021 20:09:26 +0800 Subject: [PATCH] Improve logs when there is a timeout error The agent logs will print the failure message when the connection to the controller is timeout. Also adding HealthCheck to remind the users of the connectivity issue. Fixes #822 --- cmd/antrea-agent/agent.go | 9 +++++++++ .../controller/networkpolicy/networkpolicy_controller.go | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/antrea-agent/agent.go b/cmd/antrea-agent/agent.go index 8588bb07e50..5213e7f9f48 100644 --- a/cmd/antrea-agent/agent.go +++ b/cmd/antrea-agent/agent.go @@ -17,8 +17,10 @@ package main import ( "fmt" "net" + "net/http" "time" + "k8s.io/apiserver/pkg/server/healthz" "k8s.io/client-go/informers" "k8s.io/klog" @@ -319,6 +321,13 @@ func run(o *Options) error { if err != nil { return fmt.Errorf("error when creating agent API server: %v", err) } + check := healthz.NamedCheck("watcher", func(_ *http.Request) error { + if networkPolicyController.GetControllerConnectionStatus() { + return nil + } + return fmt.Errorf("Some watchers may not be connected") + }) + apiServer.GenericAPIServer.AddHealthChecks(check) go apiServer.Run(stopCh) // Start PacketIn for features and specify their own reason. diff --git a/pkg/agent/controller/networkpolicy/networkpolicy_controller.go b/pkg/agent/controller/networkpolicy/networkpolicy_controller.go index 7316c543f56..9a05891ba4f 100644 --- a/pkg/agent/controller/networkpolicy/networkpolicy_controller.go +++ b/pkg/agent/controller/networkpolicy/networkpolicy_controller.go @@ -576,7 +576,12 @@ func (w *watcher) watch() { klog.Warningf("Failed to start watch for %s: %v", w.objectType, err) return } - + //Make sure that watcher is not the type of watch.NewEmptyWatch() + _, ok := watcher.(*watch.StreamWatcher) + if !ok { + klog.Warningf("Failed to start watch for %s. Something wrong with the connection?", w.objectType) + return + } klog.Infof("Started watch for %s", w.objectType) w.setConnected(true) eventCount := 0