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 2705150b544..c76707bb20e 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,6 @@ require ( k8s.io/kubectl v0.30.3 k8s.io/kubernetes v1.30.3 k8s.io/pod-security-admission v0.30.3 - k8s.io/sample-controller v0.30.3 k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 kubevirt.io/api v1.3.0 kubevirt.io/client-go v1.3.0 diff --git a/go.sum b/go.sum index b86a94206c8..3951260614a 100644 --- a/go.sum +++ b/go.sum @@ -2302,8 +2302,6 @@ k8s.io/pod-security-admission v0.30.3 h1:UDGZWR3ry/XrN/Ki/w7qrp49OwgQsKyh+6xWbex k8s.io/pod-security-admission v0.30.3/go.mod h1:T1EQSOLl9YyDMnXNJfsq2jeci6uoymY0mrRkkKihd98= k8s.io/sample-apiserver v0.30.3 h1:SGlc1FvY+5CGolD0Qn1iGuhbhBWMUru/kcjQ9ki2iEs= k8s.io/sample-apiserver v0.30.3/go.mod h1:P4g1Jw2lq2wtCiibqVX3KIRAfXtHpw6pOD/dzwmVG/w= -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 f747dadc371..d423c727e8b 100644 --- a/pkg/pinger/ping.go +++ b/pkg/pinger/ping.go @@ -21,10 +21,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 { @@ -49,8 +53,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) }