-
Notifications
You must be signed in to change notification settings - Fork 53
/
plugin.go
204 lines (190 loc) · 5.39 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
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
package test
import (
"context"
"crypto/tls"
"os"
"runtime"
"sync"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/rancher/opni/pkg/config/v1beta1"
"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/pkg/util"
"github.com/rancher/opni/plugins/alerting/pkg/alerting"
"github.com/rancher/opni/plugins/example/pkg/example"
metrics_agent "github.com/rancher/opni/plugins/metrics/pkg/agent"
metrics_gateway "github.com/rancher/opni/plugins/metrics/pkg/gateway"
"github.com/rancher/opni/plugins/slo/pkg/slo"
topology_agent "github.com/rancher/opni/plugins/topology/pkg/topology/agent"
topology_gateway "github.com/rancher/opni/plugins/topology/pkg/topology/gateway"
"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)
cert, caPool, err := util.LoadServingCertBundle(v1beta1.CertsSpec{
CACertData: TestData("root_ca.crt"),
ServingCertData: TestData("localhost.crt"),
ServingKeyData: TestData("localhost.key"),
})
if err != nil {
panic(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{*cert},
RootCAs: caPool,
ServerName: "localhost",
}
cfg := plugins.ServeConfig(scheme)
ch := make(chan *plugin.ReattachConfig)
cfg.Test = &plugin.ServeTestConfig{
ReattachConfigCh: ch,
}
cfg.TLSProvider = func() (*tls.Config, error) {
return tlsConfig, nil
}
go plugin.Serve(cfg)
return &plugin.ClientConfig{
HandshakeConfig: plugins.Handshake,
Plugins: scheme.PluginMap(),
Reattach: <-ch,
Managed: true,
Logger: hclog.New(&hclog.LoggerOptions{
Level: hclog.Error,
}),
Stderr: os.Stderr,
TLSConfig: tlsConfig,
}
}
type testPlugin struct {
Scheme meta.Scheme
Metadata meta.PluginMeta
}
func LoadPlugins(loader *plugins.PluginLoader, mode meta.PluginMode) int {
var metricsPluginScheme meta.Scheme
var topologyPluginScheme meta.Scheme
var scheme meta.Scheme
switch mode {
case meta.ModeGateway:
scheme = plugins.GatewayScheme
metricsPluginScheme = metrics_gateway.Scheme(context.Background())
topologyPluginScheme = topology_gateway.Scheme(context.Background())
case meta.ModeAgent:
scheme = plugins.AgentScheme
metricsPluginScheme = metrics_agent.Scheme(context.Background())
topologyPluginScheme = topology_agent.Scheme(context.Background())
default:
panic("unknown plugin mode: " + mode)
}
testPlugins := []testPlugin{
{
Scheme: metricsPluginScheme,
Metadata: meta.PluginMeta{
BinaryPath: "plugin_metrics",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/metrics",
},
},
{
Scheme: example.Scheme(context.Background()),
Metadata: meta.PluginMeta{
BinaryPath: "plugin_example",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/example",
},
},
{
Scheme: slo.Scheme(context.Background()),
Metadata: meta.PluginMeta{
BinaryPath: "plugin_slo",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/slo",
},
},
{
Scheme: alerting.Scheme(context.Background()),
Metadata: meta.PluginMeta{
BinaryPath: "plugin_alerting",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/alerting",
},
},
{
Scheme: topologyPluginScheme,
Metadata: meta.PluginMeta{
BinaryPath: "plugin_topology",
GoVersion: runtime.Version(),
Module: "github.com/rancher/opni/plugins/topology",
},
},
}
cert, caPool, err := util.LoadServingCertBundle(v1beta1.CertsSpec{
CACertData: TestData("root_ca.crt"),
ServingCertData: TestData("localhost.crt"),
ServingKeyData: TestData("localhost.key"),
})
if err != nil {
panic(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{*cert},
RootCAs: caPool,
ServerName: "localhost",
}
wg := &sync.WaitGroup{}
for _, p := range testPlugins {
p := p
sc := plugins.ServeConfig(p.Scheme)
ch := make(chan *plugin.ReattachConfig, 1)
sc.Test = &plugin.ServeTestConfig{
ReattachConfigCh: ch,
}
sc.TLSProvider = func() (*tls.Config, error) {
return tlsConfig, nil
}
go plugin.Serve(sc)
cc := plugins.ClientConfig(p.Metadata, scheme, plugins.WithReattachConfig(<-ch))
cc.TLSConfig = tlsConfig
wg.Add(1)
go func() {
defer wg.Done()
loader.LoadOne(context.Background(), p.Metadata, cc)
}()
}
wg.Wait()
loader.Complete()
return len(testPlugins)
}