-
Notifications
You must be signed in to change notification settings - Fork 281
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
ozhuraki
wants to merge
1
commit into
kubernetes-sigs:master
Choose a base branch
from
ozhuraki:watch-featuresd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−19
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,8 +24,10 @@ import ( | |||||||
| "strings" | ||||||||
| "time" | ||||||||
|
|
||||||||
| "golang.org/x/net/context" | ||||||||
| "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" | ||||||||
|
|
@@ -65,10 +67,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 { | ||||||||
|
|
@@ -87,6 +90,7 @@ var ( | |||||||
| _ source.FeatureSource = &src | ||||||||
| _ source.LabelSource = &src | ||||||||
| _ source.ConfigurableSource = &src | ||||||||
| _ source.EventSource = &src | ||||||||
| ) | ||||||||
|
|
||||||||
| // Name method of the LabelSource interface | ||||||||
|
|
@@ -318,6 +322,63 @@ func getFileContent(fileName string) ([][]byte, error) { | |||||||
| return lines, nil | ||||||||
| } | ||||||||
|
|
||||||||
| func (s *localSource) runNotifier(ctx context.Context, ch chan *source.FeatureSource) { | ||||||||
| rateLimit := time.NewTicker(time.Second) | ||||||||
| defer rateLimit.Stop() | ||||||||
| limit := false | ||||||||
| for { | ||||||||
| select { | ||||||||
| case event := <-s.fsWatcher.Events: | ||||||||
| opAny := fsnotify.Create | fsnotify.Write | fsnotify.Remove | fsnotify.Rename | fsnotify.Chmod | ||||||||
| if event.Op&opAny != 0 { | ||||||||
| klog.V(2).InfoS("fsnotify event", "eventName", event.Name, "eventOp", event.Op) | ||||||||
| if !limit { | ||||||||
| fs := source.FeatureSource(s) | ||||||||
| ch <- &fs | ||||||||
| limit = true | ||||||||
| } | ||||||||
| } | ||||||||
| case err := <-s.fsWatcher.Errors: | ||||||||
| klog.ErrorS(err, "failed to watch features.d changes") | ||||||||
| case <-rateLimit.C: | ||||||||
| limit = false | ||||||||
| case <-ctx.Done(): | ||||||||
| return | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // SetNotifyChannel method of the EventSource Interface | ||||||||
| func (s *localSource) SetNotifyChannel(ctx context.Context, ch chan *source.FeatureSource) error { | ||||||||
| info, err := os.Stat(featureFilesDir) | ||||||||
| if err != nil { | ||||||||
| if !os.IsNotExist(err) { | ||||||||
| return err | ||||||||
| } | ||||||||
ozhuraki marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| } | ||||||||
|
|
||||||||
| if info != nil && info.IsDir() { | ||||||||
ozhuraki marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| if s.fsWatcher == nil { | ||||||||
| watcher, err := fsnotify.NewWatcher() | ||||||||
| if err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| err = watcher.Add(featureFilesDir) | ||||||||
| if err != nil { | ||||||||
| errWatcher := watcher.Close() | ||||||||
| if errWatcher != nil { | ||||||||
| klog.ErrorS(errWatcher, "failed to close fsnotify watcher") | ||||||||
| } | ||||||||
| return fmt.Errorf("unable to access %v: %w", featureFilesDir, err) | ||||||||
| } | ||||||||
| s.fsWatcher = watcher | ||||||||
| } | ||||||||
| go s.runNotifier(ctx, ch) | ||||||||
ozhuraki marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| } | ||||||||
|
|
||||||||
|
||||||||
| go s.runNotifier(ch) | |
| } |
ozhuraki marked this conversation as resolved.
Show resolved
Hide resolved
ozhuraki marked this conversation as resolved.
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runNotifier goroutine runs indefinitely without a way to stop it. Consider adding a context or stop channel to allow graceful shutdown and prevent goroutine leaks.