Skip to content

Commit 76865bd

Browse files
authored
Merge pull request #356 from mm4tt/heartbeat-period-flag
Make heartbeatPeriod const into a flag
2 parents 219b408 + 2e9da85 commit 76865bd

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

cmd/options/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type NodeProblemDetectorOptions struct {
5757
// APIServerWaitInterval is the interval between the checks on the
5858
// readiness of kube-apiserver.
5959
APIServerWaitInterval time.Duration
60+
// K8sExporterHeartbeatPeriod is the period at which the k8s exporter does forcibly sync with apiserver.
61+
K8sExporterHeartbeatPeriod time.Duration
6062

6163
// prometheusExporter options
6264
// 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) {
107109
"", "Custom URI used to connect to Kubernetes ApiServer. This is ignored if --enable-k8s-exporter is false.")
108110
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.")
109111
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.")
112+
fs.DurationVar(&npdo.K8sExporterHeartbeatPeriod, "k8s-exporter-heartbeat-period", 1*time.Minute, "The period at which k8s-exporter does forcibly sync with apiserver.")
110113
fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit")
111114
fs.StringVar(&npdo.HostnameOverride, "hostname-override",
112115
"", "Custom node name used to override hostname")

pkg/exporters/k8sexporter/condition/manager.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const (
3636
updatePeriod = 1 * time.Second
3737
// resyncPeriod is the period at which condition manager does resync, only updates when needed.
3838
resyncPeriod = 10 * time.Second
39-
// heartbeatPeriod is the period at which condition manager does forcibly sync with apiserver.
40-
heartbeatPeriod = 1 * time.Minute
4139
)
4240

4341
// ConditionManager synchronizes node conditions with the apiserver with problem client.
@@ -75,15 +73,18 @@ type conditionManager struct {
7573
client problemclient.Client
7674
updates map[string]types.Condition
7775
conditions map[string]types.Condition
76+
// heartbeatPeriod is the period at which condition manager does forcibly sync with apiserver.
77+
heartbeatPeriod time.Duration
7878
}
7979

8080
// NewConditionManager creates a condition manager.
81-
func NewConditionManager(client problemclient.Client, clock clock.Clock) ConditionManager {
81+
func NewConditionManager(client problemclient.Client, clock clock.Clock, heartbeatPeriod time.Duration) ConditionManager {
8282
return &conditionManager{
83-
client: client,
84-
clock: clock,
85-
updates: make(map[string]types.Condition),
86-
conditions: make(map[string]types.Condition),
83+
client: client,
84+
clock: clock,
85+
updates: make(map[string]types.Condition),
86+
conditions: make(map[string]types.Condition),
87+
heartbeatPeriod: heartbeatPeriod,
8788
}
8889
}
8990

@@ -145,7 +146,7 @@ func (c *conditionManager) needResync() bool {
145146

146147
// needHeartbeat checks whether a forcible heartbeat is needed.
147148
func (c *conditionManager) needHeartbeat() bool {
148-
return c.clock.Now().Sub(c.latestTry) >= heartbeatPeriod
149+
return c.clock.Now().Sub(c.latestTry) >= c.heartbeatPeriod
149150
}
150151

151152
// sync synchronizes node conditions with the apiserver.

pkg/exporters/k8sexporter/condition/manager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ import (
3131
"k8s.io/apimachinery/pkg/util/clock"
3232
)
3333

34+
const heartbeatPeriod = 1 * time.Minute
35+
3436
func newTestManager() (*conditionManager, *problemclient.FakeProblemClient, *clock.FakeClock) {
3537
fakeClient := problemclient.NewFakeProblemClient()
3638
fakeClock := clock.NewFakeClock(time.Now())
37-
manager := NewConditionManager(fakeClient, fakeClock)
39+
manager := NewConditionManager(fakeClient, fakeClock, heartbeatPeriod)
3840
return manager.(*conditionManager), fakeClient, fakeClock
3941
}
4042

pkg/exporters/k8sexporter/k8s_exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewExporterOrDie(npdo *options.NodeProblemDetectorOptions) types.Exporter {
5858

5959
ke := k8sExporter{
6060
client: c,
61-
conditionManager: condition.NewConditionManager(c, clock.RealClock{}),
61+
conditionManager: condition.NewConditionManager(c, clock.RealClock{}, npdo.K8sExporterHeartbeatPeriod),
6262
}
6363

6464
ke.startHTTPReporting(npdo)

0 commit comments

Comments
 (0)