-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
149 lines (127 loc) · 3.3 KB
/
manager.go
File metadata and controls
149 lines (127 loc) · 3.3 KB
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
package extension
import (
"fmt"
"net"
"os/exec"
"path/filepath"
"sync"
"github.com/hashicorp/go-plugin"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type Manager interface {
Load(name string, opts PluginLoadOptions) (err error)
Get(name string) (client plugin.ClientProtocol, err error)
Stop()
}
var pluginMap = map[string]plugin.Plugin{
"foo": &FooPlugin{},
"bar": &BarPlugin{},
"bar_grpc": &BarPlugin{},
}
var pluginScheme = schema.GroupResource{
Group: "plugin",
Resource: "plugin",
}
var HandshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
type PluginLoadOptions struct {
Config *plugin.ClientConfig
NetworkPluginAddress string
networkAddress net.Addr
LocalPluginPath string
}
func (opts PluginLoadOptions) Copy() PluginLoadOptions {
return opts
}
func (opts PluginLoadOptions) Validate() (res PluginLoadOptions, err error) {
if opts.Config == nil {
err = errors.NewBadRequest("Config is empty")
return
}
if opts.NetworkPluginAddress == "" && opts.LocalPluginPath == "" && opts.Config.Cmd == nil && opts.Config.Reattach == nil {
err = errors.NewBadRequest("No local or network plugin configuration provided")
}
if opts.LocalPluginPath != "" {
if _, err = filepath.EvalSymlinks(opts.LocalPluginPath); err != nil {
err = errors.NewInternalError(err)
}
} else if opts.NetworkPluginAddress != "" {
opts.networkAddress, err = net.ResolveTCPAddr("tcp", opts.NetworkPluginAddress)
}
res = opts.init()
return
}
func (opts PluginLoadOptions) init() PluginLoadOptions {
opts.Config.Plugins = pluginMap
if opts.Config.HandshakeConfig.MagicCookieKey == "" {
opts.Config.HandshakeConfig = HandshakeConfig
}
if opts.LocalPluginPath != "" {
opts.Config.Cmd = exec.Command(opts.LocalPluginPath)
opts.Config.Reattach = nil
} else if opts.NetworkPluginAddress != "" {
opts.Config.Cmd = nil
opts.Config.Reattach = &plugin.ReattachConfig{
Protocol: plugin.ProtocolNetRPC,
Addr: opts.networkAddress,
}
}
fmt.Println("opts", opts, "reatach", opts.Config.Reattach, "cmd", opts.Config.Cmd, "netAddr", opts.networkAddress)
return opts
}
type clientOptions struct {
Client *plugin.Client
Options PluginLoadOptions
}
func NewManager() Manager {
mgr := &manager{}
mgr.init()
return mgr
}
type manager struct {
clients map[string]*clientOptions
initOnce sync.Once
}
func (m *manager) init() {
m.initOnce.Do(func() {
m.clients = make(map[string]*clientOptions)
})
}
func (m *manager) Load(name string, opts PluginLoadOptions) (err error) {
m.init()
// if m.clients[name]
if opts, err = opts.Validate(); err != nil {
return
}
clt := plugin.NewClient(opts.Config)
if _, err = clt.Client(); err != nil {
return
}
m.clients[name] = &clientOptions{Client: clt, Options: opts}
return
}
func (m *manager) Get(name string) (client plugin.ClientProtocol, err error) {
m.init()
c, ok := m.clients[name]
if !ok {
err = errors.NewNotFound(pluginScheme, name)
// err = fmt.Errorf("No %s plugin found", name)
return
}
if client, err = c.Client.Client(); err != nil {
err = errors.NewInternalError(err)
}
return
}
func (m *manager) Stop() {
m.init()
for _, c := range m.clients {
if c.Options.Config.Cmd != nil {
c.Client.Kill()
}
}
}