-
Notifications
You must be signed in to change notification settings - Fork 53
/
client.go
71 lines (63 loc) · 1.82 KB
/
client.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
package plugins
import (
"fmt"
"os"
"os/exec"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/rancher/opni/pkg/plugins/meta"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
)
func ClientConfig(md meta.PluginMeta, scheme meta.Scheme, reattach ...*plugin.ReattachConfig) *plugin.ClientConfig {
//#nosec G204
cmd := exec.Command(md.BinaryPath)
ConfigureSysProcAttr(cmd)
switch mode := scheme.Mode(); mode {
case meta.ModeUnknown:
case meta.ModeGateway, meta.ModeAgent:
cmd.Env = append(cmd.Environ(), fmt.Sprintf("OPNI_PLUGIN_MODE=%s", mode))
default:
panic(fmt.Sprintf("unknown plugin mode: %s", mode))
}
var rc *plugin.ReattachConfig
if len(reattach) > 0 {
rc = reattach[0]
cmd = nil
}
return &plugin.ClientConfig{
Plugins: scheme.PluginMap(),
HandshakeConfig: Handshake,
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
Managed: true,
Cmd: cmd,
Logger: hclog.New(&hclog.LoggerOptions{
Level: hclog.Error,
}),
GRPCDialOptions: []grpc.DialOption{
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
},
Stderr: os.Stderr,
Reattach: rc,
}
}
func ServeConfig(scheme meta.Scheme) *plugin.ServeConfig {
return &plugin.ServeConfig{
HandshakeConfig: Handshake,
Plugins: scheme.PluginMap(),
GRPCServer: func(opts []grpc.ServerOption) *grpc.Server {
opts = append(opts,
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
)
return grpc.NewServer(opts...)
},
Logger: hclog.New(&hclog.LoggerOptions{
Level: hclog.Error,
}),
}
}
func Serve(scheme meta.Scheme) {
plugin.Serve(ServeConfig(scheme))
}