Skip to content

nfd-worker: Watch features.d changes #2156

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions pkg/nfd-worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type nfdWorker struct {
k8sClient k8sclient.Interface
nfdClient nfdclient.Interface
stop chan struct{} // channel for signaling stop
sourceEvent chan struct{} // channel for events from soures
featureSources []source.FeatureSource
labelSources []source.LabelSource
ownerReference []metav1.OwnerReference
Expand Down Expand Up @@ -304,6 +305,12 @@ func (w *nfdWorker) Run() error {
labelTrigger.Reset(w.config.Core.SleepInterval.Duration)
defer labelTrigger.Stop()

w.sourceEvent = make(chan struct{})
eventSources := source.GetAllEventSources()
for _, s := range eventSources {
s.SetChannel(w.sourceEvent)
}

httpMux := http.NewServeMux()

// Register to metrics server
Expand Down Expand Up @@ -341,6 +348,12 @@ func (w *nfdWorker) Run() error {
return err
}

case <-w.sourceEvent:
err = w.runFeatureDiscovery()
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't want to run full feature discovery of all sources. Just the one that evented and then update the NodeFeature object. Prolly requires a bit of refactoring on the nfd-worker.go side.

Either do s.Discover() here or then in the source (and then just notify nfd-worker that it needs to re-advertise features).

if err != nil {
return err
}

case <-w.stop:
klog.InfoS("shutting down nfd-worker")
return nil
Expand Down
50 changes: 47 additions & 3 deletions source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"k8s.io/klog/v2"

"github.com/fsnotify/fsnotify"
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
"sigs.k8s.io/node-feature-discovery/pkg/utils"
"sigs.k8s.io/node-feature-discovery/source"
Expand Down Expand Up @@ -65,10 +66,11 @@ var (
featureFilesDir = "/etc/kubernetes/node-feature-discovery/features.d/"
)

// localSource implements the FeatureSource and LabelSource interfaces.
// localSource implements the FeatureSource, LabelSource, EventSource interfaces.
type localSource struct {
features *nfdv1alpha1.Features
config *Config
features *nfdv1alpha1.Features
config *Config
fsWatcher *fsnotify.Watcher
}

type Config struct {
Expand All @@ -87,6 +89,7 @@ var (
_ source.FeatureSource = &src
_ source.LabelSource = &src
_ source.ConfigurableSource = &src
_ source.EventSource = &src
)

// Name method of the LabelSource interface
Expand Down Expand Up @@ -318,6 +321,47 @@ func getFileContent(fileName string) ([][]byte, error) {
return lines, nil
}

func (s *localSource) runNotifier(ch chan struct{}) {
for {
select {
case event := <-s.fsWatcher.Events:
if event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Chmod == fsnotify.Chmod {
Copy link
Contributor

Choose a reason for hiding this comment

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

Small suggestion, could be more readable with something like:

			opAny := fsnotify.Create | fsnotify.Write | fsnotify.Remove | fsnotify.Rename | fsnotify.Chmod
			
			if event.Op&opAny != 0 {

WDYT?

klog.InfoS("fsnotify event", event)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest not to log these as "debug", (to avoid potentially flooding the logs). Also the usage of klog.InfoS is not correct.

Suggested change
klog.InfoS("fsnotify event", event)
klog.V(2).Infos("fsnotify event", "eventName", event.Name, "eventOp", event.Op)

ch <- struct{}{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at this I think we need some filtering of the events. For example, using temporary files in the dir (as recommended by our documentation, to avoid races) we will get a ton of events, each of them causing rediscovery. E.g. wait for one second (to get the slew of fsnotify events) and then send the feature event.

}
case err := <-s.fsWatcher.Errors:
klog.ErrorS(err, "failed to to watch features.d changes")
}
}
}

// SetChannel method of the EventSource Interface
func (s *localSource) SetChannel(ch chan struct{}) error {
info, err := os.Stat(featureFilesDir)
if err != nil {
if !os.IsNotExist(err) {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If it does not exist we probably want to exit, too (return nil)?

}

if info != nil && info.IsDir() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe we should also check if s.fsWatcher is not nil? Kinda theoretical but thinking if this would be called multiple times

watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}

err = watcher.Add(featureFilesDir)
if err != nil {
return fmt.Errorf("unable to access %v: %w", featureFilesDir, err)
}
s.fsWatcher = watcher
}

go s.runNotifier(ch)

return nil
}

func init() {
source.Register(&src)
}
19 changes: 19 additions & 0 deletions source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ type SupplementalSource interface {
DisableByDefault() bool
}

// EventSource is an interface for a source that can send events
type EventSource interface {
Source

// SetChannel sets the channel
SetChannel(chan struct{}) error
Copy link
Contributor

Choose a reason for hiding this comment

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

nit/suggestion: maybe indicate in the name what channel we're setting. E.g. SetNotifyChannel or smth, maybe you can come up with a better name. And, maybe it should be AddSmthChannel calling the method multiple times will not unwire the old notifier channels.

}

// FeatureLabelValue represents the value of one feature label
type FeatureLabelValue interface{}

Expand Down Expand Up @@ -155,6 +163,17 @@ func GetAllConfigurableSources() map[string]ConfigurableSource {
return all
}

// GetAllEventSources returns all registered event sources
func GetAllEventSources() map[string]EventSource {
all := make(map[string]EventSource)
for k, v := range sources {
if s, ok := v.(EventSource); ok {
all[k] = s
}
}
return all
}

// GetAllFeatures returns a combined set of all features from all feature
// sources.
func GetAllFeatures() *nfdv1alpha1.Features {
Expand Down