-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathloader.go
158 lines (133 loc) · 5.18 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2018 Heptio Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/driver/daemonset"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/driver/job"
"github.com/vmware-tanzu/sonobuoy/pkg/plugin/manifest"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
kuberuntime "k8s.io/apimachinery/pkg/runtime"
)
// LoadAllPlugins loads all plugins by finding plugin definitions in the given
// directory, taking a user's plugin selections, and a sonobuoy phone home
// address (host:port) and returning all of the active, configured plugins for
// this sonobuoy run.
func LoadAllPlugins(namespace, sonobuoyImage, imagePullPolicy, imagePullSecrets string, customAnnotations map[string]string, searchPath []string, selections []plugin.Selection) (ret []plugin.Interface, err error) {
pluginDefinitionFiles := make(map[string]struct{})
for _, dir := range searchPath {
wd, _ := os.Getwd()
logrus.Infof("Scanning plugins in %v (pwd: %v)", dir, wd)
// We only care about configured plugin directories that exist,
// since we may have a broad search path.
if _, err := os.Stat(dir); os.IsNotExist(err) {
logrus.Infof("Directory (%v) does not exist", dir)
continue
}
files, err := findPlugins(dir)
if err != nil {
return []plugin.Interface{}, errors.Wrapf(err, "couldn't scan %v for plugins", dir)
}
for _, file := range files {
pluginDefinitionFiles[file] = struct{}{}
}
}
pluginDefinitions := []manifest.Manifest{}
for file := range pluginDefinitionFiles {
pluginDefinition, err := LoadDefinitionFromFile(file)
if err != nil {
return []plugin.Interface{}, errors.Wrapf(err, "couldn't load plugin definition file %v", file)
}
pluginDefinitions = append(pluginDefinitions, *pluginDefinition)
}
// The zero value for the slice, nil, indicates that all plugins should be run.
// If the user wants to run 0 plugins, they should explicitly pass an empty array.
if selections != nil {
pluginDefinitions = filterPluginDef(pluginDefinitions, selections)
}
plugins := []plugin.Interface{}
for _, def := range pluginDefinitions {
loadedPlugin, err := loadPlugin(def, namespace, sonobuoyImage, imagePullPolicy, imagePullSecrets, customAnnotations)
if err != nil {
return nil, errors.Wrapf(err, "couldn't load plugin %v", def.SonobuoyConfig.PluginName)
}
plugins = append(plugins, loadedPlugin)
}
return plugins, nil
}
func findPlugins(dir string) ([]string, error) {
candidates, err := os.ReadDir(dir)
if err != nil {
return []string{}, errors.Wrapf(err, "couldn't search path %v", dir)
}
plugins := []string{}
for _, candidate := range candidates {
if candidate.IsDir() {
continue
}
ext := filepath.Ext(candidate.Name())
if ext == ".yml" || ext == ".yaml" {
plugins = append(plugins, filepath.Join(dir, candidate.Name()))
}
}
return plugins, nil
}
func LoadDefinitionFromFile(file string) (*manifest.Manifest, error) {
definitionFile, err := readDefinitionFromFile(file)
if err != nil {
return nil, errors.Wrapf(err, "couldn't load plugin definition file %v", file)
}
pluginDefinition, err := LoadDefinition(definitionFile)
if err != nil {
return nil, errors.Wrapf(err, "couldn't load plugin definition for file %v", file)
}
return &pluginDefinition, nil
}
func readDefinitionFromFile(file string) ([]byte, error) {
bytes, err := os.ReadFile(file)
return bytes, errors.Wrapf(err, "couldn't open plugin definition %v", file)
}
func LoadDefinition(bytes []byte) (manifest.Manifest, error) {
var def manifest.Manifest
err := kuberuntime.DecodeInto(manifest.Decoder, bytes, &def)
return def, errors.Wrap(err, "couldn't decode yaml for plugin definition")
}
func loadPlugin(def manifest.Manifest, namespace, sonobuoyImage, imagePullPolicy, imagePullSecrets string, customAnnotations map[string]string) (plugin.Interface, error) {
switch strings.ToLower(def.SonobuoyConfig.Driver) {
case "job":
return job.NewPlugin(def, namespace, sonobuoyImage, imagePullPolicy, imagePullSecrets, customAnnotations), nil
case "daemonset":
return daemonset.NewPlugin(def, namespace, sonobuoyImage, imagePullPolicy, imagePullSecrets, customAnnotations), nil
default:
return nil, fmt.Errorf("unknown driver %q for plugin %v",
def.SonobuoyConfig.Driver, def.SonobuoyConfig.PluginName)
}
}
func filterPluginDef(defs []manifest.Manifest, selections []plugin.Selection) []manifest.Manifest {
m := make(map[string]bool)
for _, selection := range selections {
m[selection.Name] = true
}
filtered := []manifest.Manifest{}
for _, def := range defs {
if m[def.SonobuoyConfig.PluginName] {
filtered = append(filtered, def)
}
}
return filtered
}