From cfc245b1934fa061e1f4035dd3f627d081684e4f Mon Sep 17 00:00:00 2001 From: jwierzbo Date: Mon, 4 Dec 2023 22:13:39 +0100 Subject: [PATCH 1/2] Switch to fsnotify --- CHANGELOG.md | 1 + go.mod | 3 ++- go.sum | 5 ++++ pkg/util/fwatch/watcher.go | 48 ++++++++++++++++++++------------------ 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b6c2fa4..7f374b936 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/go.mod b/go.mod index 3debb7b34..b1121b879 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index a64f9b8b4..cf741ed6b 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/pkg/util/fwatch/watcher.go b/pkg/util/fwatch/watcher.go index b5215f222..5d7e19879 100644 --- a/pkg/util/fwatch/watcher.go +++ b/pkg/util/fwatch/watcher.go @@ -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 | - 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) @@ -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 @@ -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) } @@ -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") } } } From 2da5c2664c298483b271cd26e8776c4c22d39144 Mon Sep 17 00:00:00 2001 From: jwierzbo Date: Mon, 4 Dec 2023 22:28:52 +0100 Subject: [PATCH 2/2] Skip chmod --- pkg/util/fwatch/watcher.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/util/fwatch/watcher.go b/pkg/util/fwatch/watcher.go index 5d7e19879..873dc7382 100644 --- a/pkg/util/fwatch/watcher.go +++ b/pkg/util/fwatch/watcher.go @@ -108,6 +108,11 @@ func (fw *fileContentWatcher) Start(ctx context.Context) { return } + // File attributes were changed - skip it + if event.Op == fsnotify.Chmod { + continue + } + fw.log.Info("modified file: %s", event.Name) fw.markAsChanged()