Skip to content

Commit 9a65598

Browse files
committed
nfd-worker: Watch features.d changes
Signed-off-by: Oleg Zhurakivskyy <oleg.zhurakivskyy@intel.com>
1 parent 04d835d commit 9a65598

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pkg/nfd-worker/nfd-worker.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"maps"
3232

33+
"github.com/fsnotify/fsnotify"
3334
"github.com/prometheus/client_golang/prometheus"
3435
"github.com/prometheus/client_golang/prometheus/promhttp"
3536
"golang.org/x/net/context"
@@ -55,7 +56,7 @@ import (
5556
_ "sigs.k8s.io/node-feature-discovery/source/custom"
5657
_ "sigs.k8s.io/node-feature-discovery/source/fake"
5758
_ "sigs.k8s.io/node-feature-discovery/source/kernel"
58-
_ "sigs.k8s.io/node-feature-discovery/source/local"
59+
"sigs.k8s.io/node-feature-discovery/source/local"
5960
_ "sigs.k8s.io/node-feature-discovery/source/memory"
6061
_ "sigs.k8s.io/node-feature-discovery/source/network"
6162
_ "sigs.k8s.io/node-feature-discovery/source/pci"
@@ -121,6 +122,7 @@ type nfdWorker struct {
121122
k8sClient k8sclient.Interface
122123
nfdClient nfdclient.Interface
123124
stop chan struct{} // channel for signaling stop
125+
fsWatcher *fsnotify.Watcher
124126
featureSources []source.FeatureSource
125127
labelSources []source.LabelSource
126128
ownerReference []metav1.OwnerReference
@@ -304,6 +306,13 @@ func (w *nfdWorker) Run() error {
304306
labelTrigger.Reset(w.config.Core.SleepInterval.Duration)
305307
defer labelTrigger.Stop()
306308

309+
if s := source.GetFeatureSource("local"); s != nil {
310+
w.fsWatcher = local.FSWatcher
311+
if w.fsWatcher != nil {
312+
defer w.fsWatcher.Close()
313+
}
314+
}
315+
307316
httpMux := http.NewServeMux()
308317

309318
// Register to metrics server
@@ -341,6 +350,16 @@ func (w *nfdWorker) Run() error {
341350
return err
342351
}
343352

353+
case event := <-w.fsWatcher.Events:
354+
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 {
355+
err = w.runFeatureDiscovery()
356+
if err != nil {
357+
return err
358+
}
359+
}
360+
case err := <-w.fsWatcher.Errors:
361+
klog.ErrorS(err, "failed to to watch features.d changes")
362+
return err
344363
case <-w.stop:
345364
klog.InfoS("shutting down nfd-worker")
346365
return nil

source/local/local.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"k8s.io/klog/v2"
2828

29+
"github.com/fsnotify/fsnotify"
2930
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
3031
"sigs.k8s.io/node-feature-discovery/pkg/utils"
3132
"sigs.k8s.io/node-feature-discovery/source"
@@ -63,6 +64,7 @@ const MaxFeatureFileSize = 65536
6364
// Config
6465
var (
6566
featureFilesDir = "/etc/kubernetes/node-feature-discovery/features.d/"
67+
FSWatcher *fsnotify.Watcher
6668
)
6769

6870
// localSource implements the FeatureSource and LabelSource interfaces.
@@ -126,6 +128,13 @@ func (s *localSource) GetLabels() (source.FeatureLabels, error) {
126128
func (s *localSource) Discover() error {
127129
s.features = nfdv1alpha1.NewFeatures()
128130

131+
if FSWatcher == nil {
132+
err := watch()
133+
if err != nil {
134+
klog.ErrorS(err, "failed to watch features.d directory")
135+
}
136+
}
137+
129138
featuresFromFiles, labelsFromFiles, err := getFeaturesFromFiles()
130139
if err != nil {
131140
klog.ErrorS(err, "failed to read feature files")
@@ -318,6 +327,30 @@ func getFileContent(fileName string) ([][]byte, error) {
318327
return lines, nil
319328
}
320329

330+
func watch() error {
331+
info, err := os.Stat(featureFilesDir)
332+
if err != nil {
333+
if !os.IsNotExist(err) {
334+
return err
335+
}
336+
}
337+
338+
if info != nil && info.IsDir() {
339+
watcher, err := fsnotify.NewWatcher()
340+
if err != nil {
341+
return err
342+
}
343+
344+
err = watcher.Add(featureFilesDir)
345+
if err != nil {
346+
return fmt.Errorf("unable to access %v: %w", featureFilesDir, err)
347+
}
348+
FSWatcher = watcher
349+
}
350+
351+
return nil
352+
}
353+
321354
func init() {
322355
source.Register(&src)
323356
}

0 commit comments

Comments
 (0)