Skip to content

Commit 4000ef0

Browse files
committed
kubelet: upgrade sourceFile to use fsnotify
Mitigate some flakes for deleted watch directories and use the maintained fsnotify package.
1 parent ace0bde commit 4000ef0

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

pkg/kubelet/config/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ go_library(
4848
] + select({
4949
"@io_bazel_rules_go//go/platform:linux": [
5050
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
51-
"//vendor/github.com/sigma/go-inotify:go_default_library",
51+
"//vendor/github.com/fsnotify/fsnotify:go_default_library",
5252
],
5353
"//conditions:default": [],
5454
}),

pkg/kubelet/config/file_linux.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"strings"
2727
"time"
2828

29-
"github.com/sigma/go-inotify"
29+
"github.com/fsnotify/fsnotify"
3030
"k8s.io/klog"
3131

3232
"k8s.io/api/core/v1"
@@ -77,54 +77,47 @@ func (s *sourceFile) doWatch() error {
7777
return &retryableError{"path does not exist, ignoring"}
7878
}
7979

80-
w, err := inotify.NewWatcher()
80+
w, err := fsnotify.NewWatcher()
8181
if err != nil {
8282
return fmt.Errorf("unable to create inotify: %v", err)
8383
}
8484
defer w.Close()
8585

86-
err = w.AddWatch(s.path, inotify.IN_DELETE_SELF|inotify.IN_CREATE|inotify.IN_MOVED_TO|inotify.IN_MODIFY|inotify.IN_MOVED_FROM|inotify.IN_DELETE|inotify.IN_ATTRIB)
86+
err = w.Add(s.path)
8787
if err != nil {
8888
return fmt.Errorf("unable to create inotify for path %q: %v", s.path, err)
8989
}
9090

9191
for {
9292
select {
93-
case event := <-w.Event:
94-
if err = s.produceWatchEvent(event); err != nil {
93+
case event := <-w.Events:
94+
if err = s.produceWatchEvent(&event); err != nil {
9595
return fmt.Errorf("error while processing inotify event (%+v): %v", event, err)
9696
}
97-
case err = <-w.Error:
97+
case err = <-w.Errors:
9898
return fmt.Errorf("error while watching %q: %v", s.path, err)
9999
}
100100
}
101101
}
102102

103-
func (s *sourceFile) produceWatchEvent(e *inotify.Event) error {
103+
func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error {
104104
// Ignore file start with dots
105105
if strings.HasPrefix(filepath.Base(e.Name), ".") {
106106
klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name)
107107
return nil
108108
}
109109
var eventType podEventType
110110
switch {
111-
case (e.Mask & inotify.IN_ISDIR) > 0:
112-
klog.Errorf("Not recursing into manifest path %q", s.path)
113-
return nil
114-
case (e.Mask & inotify.IN_CREATE) > 0:
115-
eventType = podAdd
116-
case (e.Mask & inotify.IN_MOVED_TO) > 0:
111+
case (e.Op & fsnotify.Create) > 0:
117112
eventType = podAdd
118-
case (e.Mask & inotify.IN_MODIFY) > 0:
113+
case (e.Op & fsnotify.Write) > 0:
119114
eventType = podModify
120-
case (e.Mask & inotify.IN_ATTRIB) > 0:
115+
case (e.Op & fsnotify.Chmod) > 0:
121116
eventType = podModify
122-
case (e.Mask & inotify.IN_DELETE) > 0:
117+
case (e.Op & fsnotify.Remove) > 0:
123118
eventType = podDelete
124-
case (e.Mask & inotify.IN_MOVED_FROM) > 0:
119+
case (e.Op & fsnotify.Rename) > 0:
125120
eventType = podDelete
126-
case (e.Mask & inotify.IN_DELETE_SELF) > 0:
127-
return fmt.Errorf("the watched path is deleted")
128121
default:
129122
// Ignore rest events
130123
return nil

pkg/kubelet/config/file_linux_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"io"
2424
"os"
25-
"os/exec"
2625
"path/filepath"
2726
"sync"
2827
"testing"
@@ -428,7 +427,7 @@ func writeFile(filename string, data []byte) error {
428427
func changeFileName(dir, from, to string, t *testing.T) {
429428
fromPath := filepath.Join(dir, from)
430429
toPath := filepath.Join(dir, to)
431-
if err := exec.Command("mv", fromPath, toPath).Run(); err != nil {
430+
if err := os.Rename(fromPath, toPath); err != nil {
432431
t.Errorf("Fail to change file name: %s", err)
433432
}
434433
}

0 commit comments

Comments
 (0)