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

Switch to fsnotify #1522

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- (Feature) TLS CA Secret Key
- (Refactoring) Extract Resource Helpers
- (Feature) (ML) Extension Storage Condition
- (Improvement) (ML) Switch to fsnotify for file watching for MacOS support

## [1.2.35](https://github.com/arangodb/kube-arangodb/tree/1.2.35) (2023-11-06)
- (Maintenance) Update go-driver to v1.6.0, update IsNotFound() checks
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.1.0
golang.org/x/sys v0.13.0
golang.org/x/sys v0.15.0
golang.org/x/text v0.13.0
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
google.golang.org/grpc v1.56.3
Expand Down Expand Up @@ -80,6 +80,7 @@ require (
github.com/dchest/siphash v1.2.2 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw
github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
Expand All @@ -194,6 +196,7 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -825,6 +828,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
48 changes: 25 additions & 23 deletions pkg/util/fwatch/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,11 @@ import (
"os"
"sync"

"k8s.io/utils/inotify"
"github.com/fsnotify/fsnotify"

"github.com/arangodb/kube-arangodb/pkg/logging"
)

const (
inotifyEventsModification = inotify.InCreate | inotify.InDelete | inotify.InDeleteSelf |
jwierzbo marked this conversation as resolved.
Show resolved Hide resolved
inotify.InCloseWrite | inotify.InMove | inotify.InMove | inotify.InMoveSelf | inotify.InUnmount
)

type FileContentWatcher interface {
// Start a routine to watch for file content changes. It will be stopped when context finishes
Start(ctx context.Context)
Expand All @@ -48,7 +43,7 @@ type FileContentWatcher interface {
type fileContentWatcher struct {
isRunning bool
p string
w *inotify.Watcher
w *fsnotify.Watcher
log logging.Logger

changed bool
Expand All @@ -59,11 +54,11 @@ type fileContentWatcher struct {
// Returns error if filePath is a directory.
// Caller must Close() the watcher once work finished.
func NewFileContentWatcher(filePath string, log logging.Logger) (FileContentWatcher, error) {
watcher, err := inotify.NewWatcher()
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("unable to setup inotify: %s", err)
return nil, fmt.Errorf("unable to setup fsnotify: %s", err)
}
err = watcher.AddWatch(filePath, inotifyEventsModification)
err = watcher.Add(filePath)
if err != nil {
return nil, fmt.Errorf("unable to AddWatch: %s", err)
}
Expand Down Expand Up @@ -98,25 +93,32 @@ func (fw *fileContentWatcher) Start(ctx context.Context) {
case <-ctx.Done():
err := fw.w.Close()
if err != nil {
fw.log.Err(err).Info("error while closing inotify watcher")
fw.log.Err(err).Info("error while closing fsnotify watcher")
} else {
fw.log.Info("fsnotify watcher closed")
}
return
case err := <-fw.w.Error:
case err, ok := <-fw.w.Errors:
if !ok {
return
}
fw.log.Err(err).Debug("error while watching for file content")
case e := <-fw.w.Event:
fw.log.Info("changed: %s", e.String())
case event, ok := <-fw.w.Events:
if !ok {
return
}

fw.log.Info("modified file: %s", event.Name)
fw.markAsChanged()

if e.Mask&inotify.InIgnored == 0 {
// IN_IGNORED can happen if file is deleted
// restart watch:
err := fw.w.RemoveWatch(fw.p)
if err != nil {
fw.log.Err(err).Warn("RemoveWatch failed")
if event.Op == fsnotify.Remove {
// restart watch on removed file
if err := fw.w.Remove(fw.p); err != nil {
fw.log.Err(err).Error("unable to remove watch")
}
err = fw.w.AddWatch(fw.p, inotifyEventsModification)
if err != nil {
fw.log.Err(err).Error("Could not start watch again after getting IN_IGNORED")

if err := fw.w.Add(fw.p); err != nil {
fw.log.Err(err).Error("could not start watch again")
}
}
}
Expand Down