-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_csi_linux.go
77 lines (64 loc) · 1.6 KB
/
mod_csi_linux.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
package csi
import (
"context"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"plugin"
"strings"
"sync"
"github.com/thecodeteam/goioc"
)
var loadGoPluginsOnce sync.Once
func init() {
loadGoPluginsFunc = LoadGoPlugins
}
// LoadGoPlugins reads the value of the environment variable
// CSI_GOPLUGINS, a CSV with each element being the absolute path to
// a shared object file -- a Go plug-in.
func LoadGoPlugins(ctx context.Context, filePaths ...string) (err error) {
loadGoPluginsOnce.Do(func() { err = loadGoPlugins(ctx, filePaths...) })
return err
}
func loadGoPlugins(ctx context.Context, filePaths ...string) error {
if len(filePaths) == 0 {
var err error
// read the paths of the go plug-in files
rdr := csv.NewReader(strings.NewReader(os.Getenv("CSI_GOPLUGINS")))
if filePaths, err = rdr.Read(); err != nil && err != io.EOF {
return err
}
}
if len(filePaths) == 0 {
return nil
}
// iterate the shared object files and load them one at a time
for _, so := range filePaths {
// attempt to open the plug-in
p, err := plugin.Open(so)
if err != nil {
return err
}
log.Printf("loaded plug-in: %s\n", so)
spSym, err := p.Lookup("ServiceProviders")
if err != nil {
return err
}
sps, ok := spSym.(*map[string]func() interface{})
if !ok {
return fmt.Errorf(
"error: invalid ServiceProviders field: %T", spSym)
}
if sps == nil {
return fmt.Errorf("error: nil ServiceProviders")
}
// record the service provider names and constructors
for k, v := range *sps {
goioc.Register(k, v)
log.Printf("registered service provider: %s\n", k)
}
}
return nil
}