Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change nfd-worker to use Ticker instead of After. #1050

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 59 additions & 26 deletions pkg/nfd-worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ type duration struct {
time.Duration
}

// This ticker can represent infinite and normal intervals.
type infiniteTicker struct {
*time.Ticker
}

// NewNfdWorker creates new NfdWorker instance.
func NewNfdWorker(args *Args) (NfdWorker, error) {
nfd := &nfdWorker{
Expand Down Expand Up @@ -172,6 +177,37 @@ func newDefaultConfig() *NFDConfig {
}
}

func (i *infiniteTicker) Reset(d time.Duration) {
switch {
case d > 0:
i.Ticker.Reset(d)
default:
// If the sleep interval is not a positive number the ticker will act
// as if it was set to an infinite duration by not ticking.
i.Ticker.Stop()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you format this comment to fit in 80 columns, see how other comments when too long they are several lines. makes it easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the if-else to a switch and made the comment fit into 80 columns

}
}

// Run feature discovery.
func (w *nfdWorker) runFeatureDiscovery() error {
for _, s := range w.featureSources {
klog.V(2).Infof("running discovery for %q source", s.Name())
if err := s.Discover(); err != nil {
klog.Errorf("feature discovery of %q source failed: %v", s.Name(), err)
}
}

// Get the set of feature labels.
labels := createFeatureLabels(w.labelSources, w.config.Core.LabelWhiteList.Regexp)

// Update the node with the feature labels.
if !w.config.Core.NoPublish {
return w.advertiseFeatures(labels)
}

return nil
}

// Run NfdWorker client. Returns if a fatal error is encountered, or, after
// one request if OneShot is set to 'true' in the worker args.
func (w *nfdWorker) Run() error {
Expand All @@ -196,34 +232,27 @@ func (w *nfdWorker) Run() error {

defer w.grpcDisconnect()

labelTrigger := time.After(0)
for {
select {
case <-labelTrigger:
// Run feature discovery
for _, s := range w.featureSources {
klog.V(2).Infof("running discovery for %q source", s.Name())
if err := s.Discover(); err != nil {
klog.Errorf("feature discovery of %q source failed: %v", s.Name(), err)
}
}

// Get the set of feature labels.
labels := createFeatureLabels(w.labelSources, w.config.Core.LabelWhiteList.Regexp)
// Create ticker for feature discovery and run feature discovery once before the loop.
labelTrigger := infiniteTicker{Ticker: time.NewTicker(1)}
labelTrigger.Reset(w.config.Core.SleepInterval.Duration)
defer labelTrigger.Stop()

// Update the node with the feature labels.
if !w.config.Core.NoPublish {
if err := w.advertiseFeatures(labels); err != nil {
return err
}
}
err = w.runFeatureDiscovery()
if err != nil {
return err
}

if w.args.Oneshot {
return nil
}
// Only run feature disovery once if Oneshot is set to 'true'.
if w.args.Oneshot {
return nil
}

if w.config.Core.SleepInterval.Duration > 0 {
labelTrigger = time.After(w.config.Core.SleepInterval.Duration)
for {
select {
case <-labelTrigger.C:
err = w.runFeatureDiscovery()
if err != nil {
return err
}

case <-configWatch.Events:
Expand All @@ -238,7 +267,11 @@ func (w *nfdWorker) Run() error {

// Always re-label after a re-config event. This way the new config
// comes into effect even if the sleep interval is long (or infinite)
labelTrigger = time.After(0)
labelTrigger.Reset(w.config.Core.SleepInterval.Duration)
err = w.runFeatureDiscovery()
if err != nil {
return err
}

case <-w.certWatch.Events:
klog.Infof("TLS certificate update, renewing connection to nfd-master")
Expand Down