Skip to content

Commit

Permalink
Make heartbeatPeriod const into a flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
mm4tt committed Sep 25, 2019
1 parent 219b408 commit 9b2e42e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cmd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type NodeProblemDetectorOptions struct {

// NodeName is the node name used to communicate with Kubernetes ApiServer.
NodeName string

// K8sExporterHeartbeatPeriod is the period at which the k8s exporter does forcibly sync with apiserver.
K8sExporterHeartbeatPeriod time.Duration
}

func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
Expand Down Expand Up @@ -120,6 +123,8 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&npdo.PrometheusServerAddress, "prometheus-address",
"127.0.0.1", "The address to bind the Prometheus scrape endpoint.")

fs.DurationVar(&npdo.K8sExporterHeartbeatPeriod, "k8s-exporter-heartbeat-period", 1*time.Minute, "The period at which k8s-exporter does forcibly sync with apiserver.")

for _, exporterName := range exporters.GetExporterNames() {
exporterHandler := exporters.GetExporterHandlerOrDie(exporterName)
exporterHandler.Options.SetFlags(fs)
Expand Down
17 changes: 9 additions & 8 deletions pkg/exporters/k8sexporter/condition/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion pkg/exporters/k8sexporter/condition/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/exporters/k8sexporter/k8s_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9b2e42e

Please sign in to comment.