From 2e76fe9c1bc98592a1eac1a20d6489a04cc65042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Tue, 23 Jul 2024 20:19:36 +0800 Subject: [PATCH] pinger: fix process not terminated on sigkill (#4329) Signed-off-by: zhangzujian --- cmd/ovn_ic_controller/ovn_ic_controller.go | 2 +- cmd/pinger/pinger.go | 7 ++++--- cmd/speaker/speaker.go | 2 +- go.mod | 1 - go.sum | 2 -- pkg/pinger/ping.go | 15 +++++++++++++-- 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cmd/ovn_ic_controller/ovn_ic_controller.go b/cmd/ovn_ic_controller/ovn_ic_controller.go index 570637898bd..610c3cb107e 100644 --- a/cmd/ovn_ic_controller/ovn_ic_controller.go +++ b/cmd/ovn_ic_controller/ovn_ic_controller.go @@ -2,7 +2,7 @@ package ovn_ic_controller import ( "k8s.io/klog/v2" - "k8s.io/sample-controller/pkg/signals" + "sigs.k8s.io/controller-runtime/pkg/manager/signals" "github.com/kubeovn/kube-ovn/pkg/ovn_ic_controller" "github.com/kubeovn/kube-ovn/pkg/util" diff --git a/cmd/pinger/pinger.go b/cmd/pinger/pinger.go index b7d68cd1616..342d5f4a407 100644 --- a/cmd/pinger/pinger.go +++ b/cmd/pinger/pinger.go @@ -4,7 +4,7 @@ import ( _ "net/http/pprof" // #nosec "k8s.io/klog/v2" - "k8s.io/sample-controller/pkg/signals" + "sigs.k8s.io/controller-runtime/pkg/manager/signals" "github.com/kubeovn/kube-ovn/pkg/metrics" "github.com/kubeovn/kube-ovn/pkg/pinger" @@ -20,12 +20,13 @@ func CmdMain() { if err != nil { util.LogFatalAndExit(err, "failed to parse config") } + + ctx := signals.SetupSignalHandler() if config.Mode == "server" { if config.EnableMetrics { go func() { pinger.InitPingerMetrics() metrics.InitKlogMetrics() - ctx := signals.SetupSignalHandler() if err := metrics.Run(ctx, nil, util.JoinHostPort("0.0.0.0", config.Port), false); err != nil { util.LogFatalAndExit(err, "failed to run metrics server") } @@ -45,5 +46,5 @@ func CmdMain() { } } } - pinger.StartPinger(config) + pinger.StartPinger(config, ctx.Done()) } diff --git a/cmd/speaker/speaker.go b/cmd/speaker/speaker.go index 8372e36eefe..0a9441ab729 100644 --- a/cmd/speaker/speaker.go +++ b/cmd/speaker/speaker.go @@ -7,7 +7,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "k8s.io/klog/v2" - "k8s.io/sample-controller/pkg/signals" + "sigs.k8s.io/controller-runtime/pkg/manager/signals" "github.com/kubeovn/kube-ovn/pkg/speaker" "github.com/kubeovn/kube-ovn/pkg/util" diff --git a/go.mod b/go.mod index 87d54afc1c8..9bc5d32c504 100644 --- a/go.mod +++ b/go.mod @@ -48,7 +48,6 @@ require ( k8s.io/client-go v12.0.0+incompatible k8s.io/klog/v2 v2.130.1 k8s.io/kubernetes v1.30.3 - k8s.io/sample-controller v0.30.3 k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 kubevirt.io/client-go v1.3.0 sigs.k8s.io/controller-runtime v0.18.4 diff --git a/go.sum b/go.sum index db7eb655375..ca58d2388e2 100644 --- a/go.sum +++ b/go.sum @@ -2135,8 +2135,6 @@ k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f h1:0LQagt0gDpKqvIkAMPaRGc k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f/go.mod h1:S9tOR0FxgyusSNR+MboCuiDpVWkAifZvaYI1Q2ubgro= k8s.io/kubernetes v1.30.3 h1:A0qoXI1YQNzrQZiff33y5zWxYHFT/HeZRK98/sRDJI0= k8s.io/kubernetes v1.30.3/go.mod h1:yPbIk3MhmhGigX62FLJm+CphNtjxqCvAIFQXup6RKS0= -k8s.io/sample-controller v0.30.3 h1:oZTxERF8U3gANT2H5VxpkW32asgmW0IYGyUv9Opspvs= -k8s.io/sample-controller v0.30.3/go.mod h1:yhy/cWCzevQLa2+7Gvj0J9+xzmNExypunffSNANBy7o= k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= diff --git a/pkg/pinger/ping.go b/pkg/pinger/ping.go index cb49ee66494..366b23fd540 100644 --- a/pkg/pinger/ping.go +++ b/pkg/pinger/ping.go @@ -19,10 +19,14 @@ import ( "github.com/kubeovn/kube-ovn/pkg/util" ) -func StartPinger(config *Configuration) { +func StartPinger(config *Configuration, stopCh <-chan struct{}) { errHappens := false var exporter *Exporter withMetrics := config.Mode == "server" && config.EnableMetrics + internval := time.Duration(config.Interval) * time.Second + timer := time.NewTimer(internval) + timer.Stop() +LOOP: for { if config.NetworkMode == "kube-ovn" { if checkOvs(config, withMetrics) != nil { @@ -47,8 +51,15 @@ func StartPinger(config *Configuration) { if config.Mode != "server" { break } - time.Sleep(time.Duration(config.Interval) * time.Second) + + timer.Reset(internval) + select { + case <-stopCh: + break LOOP + case <-timer.C: + } } + timer.Stop() if errHappens && config.ExitCode != 0 { os.Exit(config.ExitCode) }