Skip to content

Commit

Permalink
pinger: fix process not terminated on sigkill (kubeovn#4329)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Jul 23, 2024
1 parent 7719ce5 commit 2e76fe9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/ovn_ic_controller/ovn_ic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions cmd/pinger/pinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand All @@ -45,5 +46,5 @@ func CmdMain() {
}
}
}
pinger.StartPinger(config)
pinger.StartPinger(config, ctx.Done())
}
2 changes: 1 addition & 1 deletion cmd/speaker/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
15 changes: 13 additions & 2 deletions pkg/pinger/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 2e76fe9

Please sign in to comment.