mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Merge pull request #73703 from rphillips/fixes/kubelet_file_fsnotify
kubelet: upgrade sourceFile to use fsnotify
This commit is contained in:
commit
77cf7c7b86
@ -48,7 +48,7 @@ go_library(
|
|||||||
] + select({
|
] + select({
|
||||||
"@io_bazel_rules_go//go/platform:linux": [
|
"@io_bazel_rules_go//go/platform:linux": [
|
||||||
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
|
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||||
"//vendor/github.com/sigma/go-inotify:go_default_library",
|
"//vendor/github.com/fsnotify/fsnotify:go_default_library",
|
||||||
],
|
],
|
||||||
"//conditions:default": [],
|
"//conditions:default": [],
|
||||||
}),
|
}),
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sigma/go-inotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
@ -77,30 +77,30 @@ func (s *sourceFile) doWatch() error {
|
|||||||
return &retryableError{"path does not exist, ignoring"}
|
return &retryableError{"path does not exist, ignoring"}
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := inotify.NewWatcher()
|
w, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create inotify: %v", err)
|
return fmt.Errorf("unable to create inotify: %v", err)
|
||||||
}
|
}
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
|
|
||||||
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)
|
err = w.Add(s.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create inotify for path %q: %v", s.path, err)
|
return fmt.Errorf("unable to create inotify for path %q: %v", s.path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-w.Event:
|
case event := <-w.Events:
|
||||||
if err = s.produceWatchEvent(event); err != nil {
|
if err = s.produceWatchEvent(&event); err != nil {
|
||||||
return fmt.Errorf("error while processing inotify event (%+v): %v", event, err)
|
return fmt.Errorf("error while processing inotify event (%+v): %v", event, err)
|
||||||
}
|
}
|
||||||
case err = <-w.Error:
|
case err = <-w.Errors:
|
||||||
return fmt.Errorf("error while watching %q: %v", s.path, err)
|
return fmt.Errorf("error while watching %q: %v", s.path, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sourceFile) produceWatchEvent(e *inotify.Event) error {
|
func (s *sourceFile) produceWatchEvent(e *fsnotify.Event) error {
|
||||||
// Ignore file start with dots
|
// Ignore file start with dots
|
||||||
if strings.HasPrefix(filepath.Base(e.Name), ".") {
|
if strings.HasPrefix(filepath.Base(e.Name), ".") {
|
||||||
klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name)
|
klog.V(4).Infof("Ignored pod manifest: %s, because it starts with dots", e.Name)
|
||||||
@ -108,23 +108,16 @@ func (s *sourceFile) produceWatchEvent(e *inotify.Event) error {
|
|||||||
}
|
}
|
||||||
var eventType podEventType
|
var eventType podEventType
|
||||||
switch {
|
switch {
|
||||||
case (e.Mask & inotify.IN_ISDIR) > 0:
|
case (e.Op & fsnotify.Create) > 0:
|
||||||
klog.Errorf("Not recursing into manifest path %q", s.path)
|
|
||||||
return nil
|
|
||||||
case (e.Mask & inotify.IN_CREATE) > 0:
|
|
||||||
eventType = podAdd
|
eventType = podAdd
|
||||||
case (e.Mask & inotify.IN_MOVED_TO) > 0:
|
case (e.Op & fsnotify.Write) > 0:
|
||||||
eventType = podAdd
|
|
||||||
case (e.Mask & inotify.IN_MODIFY) > 0:
|
|
||||||
eventType = podModify
|
eventType = podModify
|
||||||
case (e.Mask & inotify.IN_ATTRIB) > 0:
|
case (e.Op & fsnotify.Chmod) > 0:
|
||||||
eventType = podModify
|
eventType = podModify
|
||||||
case (e.Mask & inotify.IN_DELETE) > 0:
|
case (e.Op & fsnotify.Remove) > 0:
|
||||||
eventType = podDelete
|
eventType = podDelete
|
||||||
case (e.Mask & inotify.IN_MOVED_FROM) > 0:
|
case (e.Op & fsnotify.Rename) > 0:
|
||||||
eventType = podDelete
|
eventType = podDelete
|
||||||
case (e.Mask & inotify.IN_DELETE_SELF) > 0:
|
|
||||||
return fmt.Errorf("the watched path is deleted")
|
|
||||||
default:
|
default:
|
||||||
// Ignore rest events
|
// Ignore rest events
|
||||||
return nil
|
return nil
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@ -428,7 +427,7 @@ func writeFile(filename string, data []byte) error {
|
|||||||
func changeFileName(dir, from, to string, t *testing.T) {
|
func changeFileName(dir, from, to string, t *testing.T) {
|
||||||
fromPath := filepath.Join(dir, from)
|
fromPath := filepath.Join(dir, from)
|
||||||
toPath := filepath.Join(dir, to)
|
toPath := filepath.Join(dir, to)
|
||||||
if err := exec.Command("mv", fromPath, toPath).Run(); err != nil {
|
if err := os.Rename(fromPath, toPath); err != nil {
|
||||||
t.Errorf("Fail to change file name: %s", err)
|
t.Errorf("Fail to change file name: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user