-
Notifications
You must be signed in to change notification settings - Fork 53
/
core.go
57 lines (49 loc) · 1.19 KB
/
core.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
package plugins
import (
"context"
"errors"
"os"
"os/signal"
"github.com/hashicorp/go-plugin"
"github.com/rancher/opni/pkg/plugins/meta"
"google.golang.org/grpc"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)
var ErrNotImplemented = errors.New("not implemented")
var ClientScheme = meta.NewScheme()
var Handshake = plugin.HandshakeConfig{
ProtocolVersion: plugin.CoreProtocolVersion,
MagicCookieKey: "OPNI_MONITORING_MAGIC_COOKIE",
MagicCookieValue: "opni-monitoring",
}
func init() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt)
go func() {
<-c
plugin.CleanupClients()
os.Exit(0)
}()
}
func CheckAvailability(ctx context.Context, cc *grpc.ClientConn, id string) error {
ref := rpb.NewServerReflectionClient(cc)
stream, err := ref.ServerReflectionInfo(ctx)
if err != nil {
return err
}
if err := stream.Send(&rpb.ServerReflectionRequest{
MessageRequest: &rpb.ServerReflectionRequest_ListServices{},
}); err != nil {
return err
}
response, err := stream.Recv()
if err != nil {
return err
}
for _, svc := range response.GetListServicesResponse().GetService() {
if svc.Name == id {
return nil
}
}
return ErrNotImplemented
}