forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
367 lines (330 loc) · 11 KB
/
plugins.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package skipper
import (
"fmt"
"os"
"path/filepath"
"plugin"
"strings"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/routing"
)
func (o *Options) findAndLoadPlugins() error {
found := make(map[string]string)
done := make(map[string][]string)
for _, dir := range o.PluginDirs {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
// don't fail when default plugin dir is missing
if _, ok := err.(*os.PathError); ok && dir == DefaultPluginDir {
return err
}
log.Fatalf("failed to search for plugins: %s", err)
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".so") {
name := filepath.Base(path)
name = name[:len(name)-3] // strip suffix
found[name] = path
log.Printf("found plugin %s at %s", name, path)
}
return nil
})
}
if err := o.loadPlugins(found, done); err != nil {
return err
}
if err := o.loadFilterPlugins(found, done); err != nil {
return err
}
if err := o.loadPredicatePlugins(found, done); err != nil {
return err
}
if err := o.loadDataClientPlugins(found, done); err != nil {
return err
}
for name, path := range found {
log.Printf("attempting to load plugin from %s", path)
mod, err := plugin.Open(path)
if err != nil {
return fmt.Errorf("open plugin %s from %s: %s", name, path, err)
}
conf, err := readPluginConfig(path)
if err != nil {
return fmt.Errorf("failed to read config for %s: %s", path, err)
}
if !pluginIsLoaded(done, name, "InitPlugin") {
if sym, err := mod.Lookup("InitPlugin"); err == nil {
fltrs, preds, dcs, err := initPlugin(sym, path, conf)
if err != nil {
return fmt.Errorf("filter plugin %s returned: %s", path, err)
}
o.CustomFilters = append(o.CustomFilters, fltrs...)
o.CustomPredicates = append(o.CustomPredicates, preds...)
o.CustomDataClients = append(o.CustomDataClients, dcs...)
log.Printf("multitype plugin %s loaded from %s (filter: %d, predicate: %d, dataclient: %d)",
name, path, len(fltrs), len(preds), len(dcs))
markPluginLoaded(done, name, "InitPlugin")
}
} else {
log.Printf("plugin %s already loaded with InitPlugin", name)
}
if !pluginIsLoaded(done, name, "InitFilter") {
if sym, err := mod.Lookup("InitFilter"); err == nil {
spec, err := initFilterPlugin(sym, path, conf)
if err != nil {
return fmt.Errorf("filter plugin %s returned: %s", path, err)
}
o.CustomFilters = append(o.CustomFilters, spec)
log.Printf("filter plugin %s loaded from %s", name, path)
markPluginLoaded(done, name, "InitFilter")
}
} else {
log.Printf("plugin %s already loaded with InitFilter", name)
}
if !pluginIsLoaded(done, name, "InitPredicate") {
if sym, err := mod.Lookup("InitPredicate"); err == nil {
spec, err := initPredicatePlugin(sym, path, conf)
if err != nil {
return fmt.Errorf("predicate plugin %s returned: %s", path, err)
}
o.CustomPredicates = append(o.CustomPredicates, spec)
log.Printf("predicate plugin %s loaded from %s", name, path)
markPluginLoaded(done, name, "InitPredicate")
}
} else {
log.Printf("plugin %s already loaded with InitPredicate", name)
}
if !pluginIsLoaded(done, name, "InitDataClient") {
if sym, err := mod.Lookup("InitDataClient"); err == nil {
spec, err := initDataClientPlugin(sym, path, conf)
if err != nil {
return fmt.Errorf("data client plugin %s returned: %s", path, err)
}
o.CustomDataClients = append(o.CustomDataClients, spec)
log.Printf("data client plugin %s loaded from %s", name, path)
markPluginLoaded(done, name, "InitDataClient")
}
} else {
log.Printf("plugin %s already loaded with InitDataClient", name)
}
}
var implementsMultiple []string
for name, specs := range done {
if len(specs) > 1 {
implementsMultiple = append(implementsMultiple, name)
}
}
if len(implementsMultiple) != 0 {
return fmt.Errorf("found plugins implementing multiple Init* functions: %v", implementsMultiple)
}
return nil
}
func pluginIsLoaded(done map[string][]string, name, spec string) bool {
loaded, ok := done[name]
if !ok {
return false
}
for _, s := range loaded {
if s == spec {
return true
}
}
return false
}
func markPluginLoaded(done map[string][]string, name, spec string) {
done[name] = append(done[name], spec)
}
func (o *Options) loadPlugins(found map[string]string, done map[string][]string) error {
for _, plug := range o.Plugins {
name := plug[0]
path, ok := found[name]
if !ok {
return fmt.Errorf("multitype plugin %s not found in plugin dirs", name)
}
fltrs, preds, dcs, err := loadPlugin(path, plug[1:])
if err != nil {
return fmt.Errorf("failed to load plugin %s: %s", path, err)
}
o.CustomFilters = append(o.CustomFilters, fltrs...)
o.CustomPredicates = append(o.CustomPredicates, preds...)
o.CustomDataClients = append(o.CustomDataClients, dcs...)
log.Printf("multitype plugin %s loaded from %s (filter: %d, predicate: %d, dataclient: %d)",
name, path, len(fltrs), len(preds), len(dcs))
markPluginLoaded(done, name, "InitPlugin")
}
return nil
}
func loadPlugin(path string, args []string) ([]filters.Spec, []routing.PredicateSpec, []routing.DataClient, error) {
mod, err := plugin.Open(path)
if err != nil {
return nil, nil, nil, fmt.Errorf("open multitype plugin %s: %s", path, err)
}
conf, err := readPluginConfig(path)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to read config for %s: %s", path, err)
}
sym, err := mod.Lookup("InitPlugin")
if err != nil {
return nil, nil, nil, fmt.Errorf("lookup module symbol failed for %s: %s", path, err)
}
return initPlugin(sym, path, append(conf, args...))
}
func initPlugin(sym plugin.Symbol, path string, args []string) ([]filters.Spec, []routing.PredicateSpec, []routing.DataClient, error) {
fn, ok := sym.(func([]string) ([]filters.Spec, []routing.PredicateSpec, []routing.DataClient, error))
if !ok {
return nil, nil, nil, fmt.Errorf("plugin %s's InitPlugin function has wrong signature", path)
}
fltrs, preds, dcs, err := fn(args)
if err != nil {
return nil, nil, nil, fmt.Errorf("plugin %s returned: %s", path, err)
}
return fltrs, preds, dcs, nil
}
func (o *Options) loadFilterPlugins(found map[string]string, done map[string][]string) error {
for _, fltr := range o.FilterPlugins {
name := fltr[0]
path, ok := found[name]
if !ok {
return fmt.Errorf("filter plugin %s not found in plugin dirs", name)
}
spec, err := loadFilterPlugin(path, fltr[1:])
if err != nil {
return fmt.Errorf("failed to load plugin %s: %s", path, err)
}
o.CustomFilters = append(o.CustomFilters, spec)
log.Printf("loaded plugin %s (%s) from %s", name, spec.Name(), path)
markPluginLoaded(done, name, "InitFilter")
}
return nil
}
func loadFilterPlugin(path string, args []string) (filters.Spec, error) {
mod, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("open filter plugin %s: %s", path, err)
}
conf, err := readPluginConfig(path)
if err != nil {
return nil, fmt.Errorf("failed to read config for %s: %s", path, err)
}
sym, err := mod.Lookup("InitFilter")
if err != nil {
return nil, fmt.Errorf("lookup module symbol failed for %s: %s", path, err)
}
return initFilterPlugin(sym, path, append(conf, args...))
}
func initFilterPlugin(sym plugin.Symbol, path string, args []string) (filters.Spec, error) {
fn, ok := sym.(func([]string) (filters.Spec, error))
if !ok {
return nil, fmt.Errorf("plugin %s's InitFilter function has wrong signature", path)
}
spec, err := fn(args)
if err != nil {
return nil, fmt.Errorf("plugin %s returned: %s", path, err)
}
return spec, nil
}
func (o *Options) loadPredicatePlugins(found map[string]string, done map[string][]string) error {
for _, pred := range o.PredicatePlugins {
name := pred[0]
path, ok := found[name]
if !ok {
return fmt.Errorf("predicate plugin %s not found in plugin dirs", name)
}
spec, err := loadPredicatePlugin(path, pred[1:])
if err != nil {
return fmt.Errorf("failed to load plugin %s: %s", path, err)
}
o.CustomPredicates = append(o.CustomPredicates, spec)
log.Printf("loaded plugin %s (%s) from %s", name, spec.Name(), path)
markPluginLoaded(done, name, "InitPredicate")
}
return nil
}
func loadPredicatePlugin(path string, args []string) (routing.PredicateSpec, error) {
mod, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("open predicate module %s: %s", path, err)
}
conf, err := readPluginConfig(path)
if err != nil {
return nil, fmt.Errorf("failed to read config for %s: %s", path, err)
}
sym, err := mod.Lookup("InitPredicate")
if err != nil {
return nil, fmt.Errorf("lookup module symbol failed for %s: %s", path, err)
}
return initPredicatePlugin(sym, path, append(conf, args...))
}
func initPredicatePlugin(sym plugin.Symbol, path string, args []string) (routing.PredicateSpec, error) {
fn, ok := sym.(func([]string) (routing.PredicateSpec, error))
if !ok {
return nil, fmt.Errorf("plugin %s's InitPredicate function has wrong signature", path)
}
spec, err := fn(args)
if err != nil {
return nil, fmt.Errorf("plugin %s returned: %s", path, err)
}
return spec, nil
}
func (o *Options) loadDataClientPlugins(found map[string]string, done map[string][]string) error {
for _, pred := range o.DataClientPlugins {
name := pred[0]
path, ok := found[name]
if !ok {
return fmt.Errorf("data client plugin %s not found in plugin dirs", name)
}
spec, err := loadDataClientPlugin(path, pred[1:])
if err != nil {
return fmt.Errorf("failed to load plugin %s: %s", path, err)
}
o.CustomDataClients = append(o.CustomDataClients, spec)
log.Printf("loaded plugin %s from %s", name, path)
markPluginLoaded(done, name, "InitDataClient")
}
return nil
}
func loadDataClientPlugin(path string, args []string) (routing.DataClient, error) {
mod, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("open data client module %s: %s", path, err)
}
conf, err := readPluginConfig(path)
if err != nil {
return nil, fmt.Errorf("failed to read config for %s: %s", path, err)
}
sym, err := mod.Lookup("InitDataClient")
if err != nil {
return nil, fmt.Errorf("lookup module symbol failed for %s: %s", path, err)
}
return initDataClientPlugin(sym, path, append(conf, args...))
}
func initDataClientPlugin(sym plugin.Symbol, path string, args []string) (routing.DataClient, error) {
fn, ok := sym.(func([]string) (routing.DataClient, error))
if !ok {
return nil, fmt.Errorf("plugin %s's InitDataClient function has wrong signature", path)
}
spec, err := fn(args)
if err != nil {
return nil, fmt.Errorf("module %s returned: %s", path, err)
}
return spec, nil
}
func readPluginConfig(plugin string) (conf []string, err error) {
data, err := os.ReadFile(plugin[:len(plugin)-3] + ".conf")
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line != "" && line[0] != '#' {
conf = append(conf, line)
}
}
return conf, nil
}