Skip to content

Commit

Permalink
fileObserver: detect file existence when adding reactor
Browse files Browse the repository at this point in the history
When startingContent for non-existent file is added, detect
the non-existence of the file to avoid restart-loops.
  • Loading branch information
stlaz committed Apr 6, 2020
1 parent 4015ba6 commit a39bf28
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
32 changes: 23 additions & 9 deletions pkg/controller/fileobserver/observer_polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func (o *pollingObserver) AddReactor(reaction ReactorFn, startingFileContent map
// in case the file does not exists but empty string is specified as initial content, without this
// the content will be hashed and reaction will trigger as if the content changed.
if len(startingContent) == 0 {
o.files[f] = fileHashAndState{exists: true}
var fileExists bool
if fileExists, err = isFile(f); err != nil {
panic(fmt.Sprintf("unexpected error while adding reactor for %#v: %v", files, err))
}
o.files[f] = fileHashAndState{exists: fileExists}
o.reactors[f] = append(o.reactors[f], reaction)
continue
}
Expand Down Expand Up @@ -165,22 +169,15 @@ type fileHashAndState struct {

func calculateFileHash(path string) (fileHashAndState, error) {
result := fileHashAndState{}
stat, err := os.Stat(path)
if err != nil {
if exists, err := isFile(path); !exists || err != nil {
return result, err
}

// this is fatal
if stat.IsDir() {
return result, fmt.Errorf("you can watch only files, %s is a directory", path)
}

f, err := os.Open(path)
if err != nil {
return result, err
}
defer f.Close()

// at this point we know for sure the file exists and we can read its content even if that content is empty
result.exists = true

Expand All @@ -207,3 +204,20 @@ func calculateHash(content io.Reader) (string, bool, error) {
}
return hex.EncodeToString(hasher.Sum(nil)), false, nil
}

func isFile(path string) (bool, error) {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

// this is fatal
if stat.IsDir() {
return false, fmt.Errorf("%s is a directory", path)
}

return true, nil
}
8 changes: 8 additions & 0 deletions pkg/controller/fileobserver/observer_polling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func TestObserverPolling(t *testing.T) {
evaluateActions: observedNoChanges,
startWithNoFile: true,
},
{
// This is what controllercmd.NewCommandWithContext currently does to avoid races
name: "start with non-existing file with no change, force no starting hashing",
setInitialContent: true,
startFileContent: emptyContent,
evaluateActions: observedNoChanges,
startWithNoFile: true,
},
{
name: "start with non-existing file that is created as empty file",
evaluateActions: observedSingleFileCreated,
Expand Down

0 comments on commit a39bf28

Please sign in to comment.