Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Avoid panic when directory does not exist
Browse files Browse the repository at this point in the history
The PR #1559 rearranged the filesystem walk during Load, so that it
only resulted in an error if there was a problem reading a YAML file
or non-chart directory (which might contain YAML files).

To decide whether a file is of interest, it first checks the stat to
see if it's a directory (in which case, recurse if not a chart ..) --
but if there's an error, that will be nil, and it will panic.

In general, you don't know if the file you can't read is (supposed to
be) a directory or a regular file, so there's no way to treat those
differently. Instead, this commit makes it check before walking that
the path supplied exists, then during the walk, ignore errors unless
it looks like a YAML file.
  • Loading branch information
squaremo committed Jul 14, 2020
1 parent e3b1876 commit 7bc0da9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/cluster/kubernetes/resource/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ func Load(base string, paths []string, sopsEnabled bool) (map[string]KubeManifes
return nil, errors.Wrapf(err, "walking %q for chartdirs", base)
}
for _, root := range paths {
// In the walk, we ignore errors (indicating a failure to read
// a file) if it's not a file of interest. However, we _are_
// interested in the error if an explicitly-mentioned path
// does not exist.
if _, err := os.Stat(root); err != nil {
return nil, errors.Wrapf(err, "unable to read root path %q", root)
}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if err == nil && info.IsDir() {
if charts.isDirChart(path) {
return filepath.SkipDir
}
if err != nil {
return errors.Wrapf(err, "walking dir %q for yaml files", path)
}
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/cluster/kubernetes/resource/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,15 @@ func TestLoadSomeWithSopsAllEncrypted(t *testing.T) {
assert.NotNil(t, objs[expected.String()], "expected to find %s in manifest map after decryption", expected)
}
}

func TestNoPanic(t *testing.T) {
dir, cleanup := testfiles.TempDir(t)
defer cleanup()
if err := testfiles.WriteTestFiles(dir, testfiles.Files); err != nil {
t.Fatal(err)
}
_, err := Load(dir, []string{filepath.Join(dir, "doesnotexist")}, true)
if err == nil {
t.Error("expected error (but not panic) when loading from directory that doesn't exist")
}
}

0 comments on commit 7bc0da9

Please sign in to comment.