-
Notifications
You must be signed in to change notification settings - Fork 53
/
plugin.go
108 lines (98 loc) · 2.72 KB
/
plugin.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
package test
import (
"context"
"runtime"
"github.com/hashicorp/go-plugin"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/plugins"
"github.com/rancher/opni/pkg/plugins/apis/apiextensions"
managementext "github.com/rancher/opni/pkg/plugins/apis/apiextensions/management"
"github.com/rancher/opni/pkg/plugins/meta"
"github.com/rancher/opni/plugins/cortex/pkg/cortex"
"github.com/rancher/opni/plugins/example/pkg/example"
"go.uber.org/zap"
"google.golang.org/grpc"
)
type apiextensionTestPlugin struct {
plugin.NetRPCUnsupportedPlugin
server apiextensions.ManagementAPIExtensionServer
svcDesc *grpc.ServiceDesc
impl interface{}
}
func (p *apiextensionTestPlugin) GRPCServer(
broker *plugin.GRPCBroker,
s *grpc.Server,
) error {
s.RegisterService(p.svcDesc, p.impl)
apiextensions.RegisterManagementAPIExtensionServer(s, p.server)
return nil
}
func (p *apiextensionTestPlugin) GRPCClient(
ctx context.Context,
broker *plugin.GRPCBroker,
c *grpc.ClientConn,
) (interface{}, error) {
return apiextensions.NewManagementAPIExtensionClient(c), nil
}
func NewApiExtensionTestPlugin(
srv apiextensions.ManagementAPIExtensionServer,
svcDesc *grpc.ServiceDesc,
impl interface{},
) *plugin.ClientConfig {
p := &apiextensionTestPlugin{
server: srv,
svcDesc: svcDesc,
impl: impl,
}
scheme := meta.NewScheme()
scheme.Add(managementext.ManagementAPIExtensionPluginID, p)
cfg := plugins.ServeConfig(scheme)
ch := make(chan *plugin.ReattachConfig)
cfg.Test = &plugin.ServeTestConfig{
ReattachConfigCh: ch,
}
go plugin.Serve(cfg)
return &plugin.ClientConfig{
HandshakeConfig: plugins.Handshake,
Plugins: scheme.PluginMap(),
Reattach: <-ch,
Managed: true,
Logger: logger.NewHCLogger(logger.New(logger.WithLogLevel(zap.WarnLevel))).Named("plugin"),
}
}
type testPlugin struct {
Scheme meta.Scheme
Metadata meta.PluginMeta
}
func LoadPlugins(loader *plugins.PluginLoader) int {
testPlugins := []testPlugin{
{
Scheme: cortex.Scheme(context.Background()),
Metadata: meta.PluginMeta{
BinaryPath: "plugin_cortex",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/cortex",
},
},
{
Scheme: example.Scheme(),
Metadata: meta.PluginMeta{
BinaryPath: "plugin_example",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/example",
},
},
}
for _, p := range testPlugins {
sc := plugins.ServeConfig(p.Scheme)
ch := make(chan *plugin.ReattachConfig, 1)
sc.Test = &plugin.ServeTestConfig{
ReattachConfigCh: ch,
}
go plugin.Serve(sc)
rc := <-ch
cc := plugins.ClientConfig(p.Metadata, plugins.ClientScheme, rc)
loader.Load(p.Metadata, cc)
}
return len(testPlugins)
}