Skip to content

Commit

Permalink
pkg/index: ignore non-.yaml files during scanning (#413)
Browse files Browse the repository at this point in the history
* pkg/index: ignore non-.yaml files during scanning

Turns out if there was a file in index/ directory with a non-.yaml extension,
we still targeted that as a plugin, so we tried to parse its manifest. (But
read/parsing of individual manifests do not cause fatal errors, just printed
to stderr like a warning.)

This creates a helper method that properly scans for .yaml files in an
immediate directory.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>

* rename variables

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
  • Loading branch information
ahmetb authored and k8s-ci-robot committed Dec 2, 2019
1 parent 952834f commit ed324d4
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pkg/index/indexscanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,45 @@ import (
"sigs.k8s.io/krew/pkg/index/validation"
)

func findPluginManifestFiles(indexDir string) ([]string, error) {
var out []string
files, err := ioutil.ReadDir(indexDir)
if err != nil {
return nil, errors.Wrap(err, "failed to open index dir")
}
for _, file := range files {
if file.Mode().IsRegular() && filepath.Ext(file.Name()) == constants.ManifestExtension {
out = append(out, file.Name())
}
}
return out, nil
}

// LoadPluginListFromFS will parse and retrieve all plugin files.
func LoadPluginListFromFS(indexDir string) ([]index.Plugin, error) {
indexDir, err := filepath.EvalSymlinks(indexDir)
if err != nil {
return nil, err
}

files, err := ioutil.ReadDir(indexDir)
files, err := findPluginManifestFiles(indexDir)
if err != nil {
return nil, errors.Wrap(err, "failed to open index dir")
return nil, errors.Wrap(err, "failed to scan plugins in index directory")
}
klog.V(4).Infof("found %d plugins in dir %s", len(files), indexDir)

list := make([]index.Plugin, 0, len(files))
for _, f := range files {
if f.IsDir() {
continue
}

pluginName := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
for _, file := range files {
pluginName := strings.TrimSuffix(file, filepath.Ext(file))
p, err := LoadPluginFileFromFS(indexDir, pluginName)
if err != nil {
// Index loading shouldn't fail because of one plugin.
// Show error instead.
klog.Errorf("failed to load file %q, err: %v", pluginName, err)
klog.Errorf("failed to read or parse plugin manifest %q: %v", pluginName, err)
continue
}
list = append(list, p)
}
klog.V(4).Infof("Found %d plugins in dir %s", len(list), indexDir)
return list, nil
}

Expand Down

0 comments on commit ed324d4

Please sign in to comment.