Skip to content

Commit ed324d4

Browse files
ahmetbk8s-ci-robot
authored andcommitted
pkg/index: ignore non-.yaml files during scanning (#413)
* 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 <[email protected]> * rename variables Signed-off-by: Ahmet Alp Balkan <[email protected]>
1 parent 952834f commit ed324d4

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

pkg/index/indexscanner/scanner.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,45 @@ import (
3030
"sigs.k8s.io/krew/pkg/index/validation"
3131
)
3232

33+
func findPluginManifestFiles(indexDir string) ([]string, error) {
34+
var out []string
35+
files, err := ioutil.ReadDir(indexDir)
36+
if err != nil {
37+
return nil, errors.Wrap(err, "failed to open index dir")
38+
}
39+
for _, file := range files {
40+
if file.Mode().IsRegular() && filepath.Ext(file.Name()) == constants.ManifestExtension {
41+
out = append(out, file.Name())
42+
}
43+
}
44+
return out, nil
45+
}
46+
3347
// LoadPluginListFromFS will parse and retrieve all plugin files.
3448
func LoadPluginListFromFS(indexDir string) ([]index.Plugin, error) {
3549
indexDir, err := filepath.EvalSymlinks(indexDir)
3650
if err != nil {
3751
return nil, err
3852
}
3953

40-
files, err := ioutil.ReadDir(indexDir)
54+
files, err := findPluginManifestFiles(indexDir)
4155
if err != nil {
42-
return nil, errors.Wrap(err, "failed to open index dir")
56+
return nil, errors.Wrap(err, "failed to scan plugins in index directory")
4357
}
58+
klog.V(4).Infof("found %d plugins in dir %s", len(files), indexDir)
4459

4560
list := make([]index.Plugin, 0, len(files))
46-
for _, f := range files {
47-
if f.IsDir() {
48-
continue
49-
}
50-
51-
pluginName := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
61+
for _, file := range files {
62+
pluginName := strings.TrimSuffix(file, filepath.Ext(file))
5263
p, err := LoadPluginFileFromFS(indexDir, pluginName)
5364
if err != nil {
5465
// Index loading shouldn't fail because of one plugin.
5566
// Show error instead.
56-
klog.Errorf("failed to load file %q, err: %v", pluginName, err)
67+
klog.Errorf("failed to read or parse plugin manifest %q: %v", pluginName, err)
5768
continue
5869
}
5970
list = append(list, p)
6071
}
61-
klog.V(4).Infof("Found %d plugins in dir %s", len(list), indexDir)
6272
return list, nil
6373
}
6474

0 commit comments

Comments
 (0)