From f446745777600c65b1d6a0b922eaf7e4b794a400 Mon Sep 17 00:00:00 2001 From: pegasas <616672335@qq.com> Date: Sat, 2 Sep 2023 14:04:15 +0800 Subject: [PATCH] Improve logging on kube-proxy exit --- pkg/proxy/node.go | 4 ++-- pkg/proxy/node_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/proxy/node.go b/pkg/proxy/node.go index 7cd24f6d4fc..6ae87fdcac6 100644 --- a/pkg/proxy/node.go +++ b/pkg/proxy/node.go @@ -57,7 +57,7 @@ func (n *NodePodCIDRHandler) OnNodeAdd(node *v1.Node) { if !reflect.DeepEqual(n.podCIDRs, podCIDRs) { klog.ErrorS(nil, "Using NodeCIDR LocalDetector mode, current PodCIDRs are different than previous PodCIDRs, restarting", "node", klog.KObj(node), "newPodCIDRs", podCIDRs, "oldPodCIDRs", n.podCIDRs) - panic("Current Node PodCIDRs are different than previous PodCIDRs, restarting") + klog.FlushAndExit(klog.ExitFlushTimeout, 1) } } @@ -75,7 +75,7 @@ func (n *NodePodCIDRHandler) OnNodeUpdate(_, node *v1.Node) { if !reflect.DeepEqual(n.podCIDRs, podCIDRs) { klog.ErrorS(nil, "Using NodeCIDR LocalDetector mode, current PodCIDRs are different than previous PodCIDRs, restarting", "node", klog.KObj(node), "newPodCIDRs", podCIDRs, "oldPODCIDRs", n.podCIDRs) - panic("Current Node PodCIDRs are different than previous PodCIDRs, restarting") + klog.FlushAndExit(klog.ExitFlushTimeout, 1) } } diff --git a/pkg/proxy/node_test.go b/pkg/proxy/node_test.go index 2f6d7b54ad7..a0fc5a9e209 100644 --- a/pkg/proxy/node_test.go +++ b/pkg/proxy/node_test.go @@ -17,13 +17,21 @@ limitations under the License. package proxy import ( + "strconv" "testing" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/klog/v2" ) func TestNodePodCIDRHandlerAdd(t *testing.T) { + oldKlogOsExit := klog.OsExit + defer func() { + klog.OsExit = oldKlogOsExit + }() + klog.OsExit = customExit + tests := []struct { name string oldNodePodCIDRs []string @@ -71,12 +79,19 @@ func TestNodePodCIDRHandlerAdd(t *testing.T) { t.Errorf("The code did panic") } }() + n.OnNodeAdd(node) }) } } func TestNodePodCIDRHandlerUpdate(t *testing.T) { + oldKlogOsExit := klog.OsExit + defer func() { + klog.OsExit = oldKlogOsExit + }() + klog.OsExit = customExit + tests := []struct { name string oldNodePodCIDRs []string @@ -125,7 +140,12 @@ func TestNodePodCIDRHandlerUpdate(t *testing.T) { t.Errorf("The code did panic") } }() + n.OnNodeUpdate(oldNode, node) }) } } + +func customExit(exitCode int) { + panic(strconv.Itoa(exitCode)) +}