inspect symlinks to auto-reload k8s secrets

This commit is contained in:
Julien Laffaye 2020-09-11 10:56:32 -04:00 committed by Matthew Silverman
parent 87aa96b468
commit d280a344c4
2 changed files with 94 additions and 4 deletions

View file

@ -18,7 +18,9 @@ package watch
import (
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
@ -60,15 +62,31 @@ func (f *OSFileWatcher) watch() error {
}
f.watcher = watcher
realFile, err := filepath.EvalSymlinks(f.file)
if err != nil {
return err
}
dir, file := path.Split(f.file)
go func(file string) {
for {
select {
case event := <-watcher.Events:
if (event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Create == fsnotify.Create) &&
strings.HasSuffix(event.Name, file) {
f.onEvent()
if event.Op&fsnotify.Create == fsnotify.Create ||
event.Op&fsnotify.Write == fsnotify.Write {
if finfo, err := os.Lstat(event.Name); err != nil {
log.Printf("can not lstat file: %v\n", err)
} else if finfo.Mode()&os.ModeSymlink != 0 {
if currentRealFile, err := filepath.EvalSymlinks(f.file); err == nil &&
currentRealFile != realFile {
f.onEvent()
realFile = currentRealFile
}
continue
}
if strings.HasSuffix(event.Name, file) {
f.onEvent()
}
}
case err := <-watcher.Errors:
if err != nil {