From 2e9da8569d0ee17bc13e5f45db0960b61511a869 Mon Sep 17 00:00:00 2001 From: Matt Matejczyk Date: Mon, 23 Sep 2019 16:01:01 +0200 Subject: [PATCH] Make heartbeatPeriod const into a flag. --- cmd/options/options.go | 3 +++ pkg/exporters/k8sexporter/condition/manager.go | 17 +++++++++-------- .../k8sexporter/condition/manager_test.go | 4 +++- pkg/exporters/k8sexporter/k8s_exporter.go | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/options/options.go b/cmd/options/options.go index acc8327d..6099cca9 100644 --- a/cmd/options/options.go +++ b/cmd/options/options.go @@ -57,6 +57,8 @@ type NodeProblemDetectorOptions struct { // APIServerWaitInterval is the interval between the checks on the // readiness of kube-apiserver. APIServerWaitInterval time.Duration + // K8sExporterHeartbeatPeriod is the period at which the k8s exporter does forcibly sync with apiserver. + K8sExporterHeartbeatPeriod time.Duration // prometheusExporter options // PrometheusServerPort is the port to bind the Prometheus scrape endpoint. Use 0 to disable. @@ -107,6 +109,7 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) { "", "Custom URI used to connect to Kubernetes ApiServer. This is ignored if --enable-k8s-exporter is false.") fs.DurationVar(&npdo.APIServerWaitTimeout, "apiserver-wait-timeout", time.Duration(5)*time.Minute, "The timeout on waiting for kube-apiserver to be ready. This is ignored if --enable-k8s-exporter is false.") fs.DurationVar(&npdo.APIServerWaitInterval, "apiserver-wait-interval", time.Duration(5)*time.Second, "The interval between the checks on the readiness of kube-apiserver. This is ignored if --enable-k8s-exporter is false.") + fs.DurationVar(&npdo.K8sExporterHeartbeatPeriod, "k8s-exporter-heartbeat-period", 1*time.Minute, "The period at which k8s-exporter does forcibly sync with apiserver.") fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit") fs.StringVar(&npdo.HostnameOverride, "hostname-override", "", "Custom node name used to override hostname") diff --git a/pkg/exporters/k8sexporter/condition/manager.go b/pkg/exporters/k8sexporter/condition/manager.go index 7ed0bc21..bf74f542 100644 --- a/pkg/exporters/k8sexporter/condition/manager.go +++ b/pkg/exporters/k8sexporter/condition/manager.go @@ -36,8 +36,6 @@ const ( updatePeriod = 1 * time.Second // resyncPeriod is the period at which condition manager does resync, only updates when needed. resyncPeriod = 10 * time.Second - // heartbeatPeriod is the period at which condition manager does forcibly sync with apiserver. - heartbeatPeriod = 1 * time.Minute ) // ConditionManager synchronizes node conditions with the apiserver with problem client. @@ -75,15 +73,18 @@ type conditionManager struct { client problemclient.Client updates map[string]types.Condition conditions map[string]types.Condition + // heartbeatPeriod is the period at which condition manager does forcibly sync with apiserver. + heartbeatPeriod time.Duration } // NewConditionManager creates a condition manager. -func NewConditionManager(client problemclient.Client, clock clock.Clock) ConditionManager { +func NewConditionManager(client problemclient.Client, clock clock.Clock, heartbeatPeriod time.Duration) ConditionManager { return &conditionManager{ - client: client, - clock: clock, - updates: make(map[string]types.Condition), - conditions: make(map[string]types.Condition), + client: client, + clock: clock, + updates: make(map[string]types.Condition), + conditions: make(map[string]types.Condition), + heartbeatPeriod: heartbeatPeriod, } } @@ -145,7 +146,7 @@ func (c *conditionManager) needResync() bool { // needHeartbeat checks whether a forcible heartbeat is needed. func (c *conditionManager) needHeartbeat() bool { - return c.clock.Now().Sub(c.latestTry) >= heartbeatPeriod + return c.clock.Now().Sub(c.latestTry) >= c.heartbeatPeriod } // sync synchronizes node conditions with the apiserver. diff --git a/pkg/exporters/k8sexporter/condition/manager_test.go b/pkg/exporters/k8sexporter/condition/manager_test.go index f0066ed9..effcb81b 100644 --- a/pkg/exporters/k8sexporter/condition/manager_test.go +++ b/pkg/exporters/k8sexporter/condition/manager_test.go @@ -31,10 +31,12 @@ import ( "k8s.io/apimachinery/pkg/util/clock" ) +const heartbeatPeriod = 1 * time.Minute + func newTestManager() (*conditionManager, *problemclient.FakeProblemClient, *clock.FakeClock) { fakeClient := problemclient.NewFakeProblemClient() fakeClock := clock.NewFakeClock(time.Now()) - manager := NewConditionManager(fakeClient, fakeClock) + manager := NewConditionManager(fakeClient, fakeClock, heartbeatPeriod) return manager.(*conditionManager), fakeClient, fakeClock } diff --git a/pkg/exporters/k8sexporter/k8s_exporter.go b/pkg/exporters/k8sexporter/k8s_exporter.go index 47900ddc..ba6ae393 100644 --- a/pkg/exporters/k8sexporter/k8s_exporter.go +++ b/pkg/exporters/k8sexporter/k8s_exporter.go @@ -58,7 +58,7 @@ func NewExporterOrDie(npdo *options.NodeProblemDetectorOptions) types.Exporter { ke := k8sExporter{ client: c, - conditionManager: condition.NewConditionManager(c, clock.RealClock{}), + conditionManager: condition.NewConditionManager(c, clock.RealClock{}, npdo.K8sExporterHeartbeatPeriod), } ke.startHTTPReporting(npdo)