-
Notifications
You must be signed in to change notification settings - Fork 53
/
plugin.go
86 lines (73 loc) · 2.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
package managementext
import (
"context"
"github.com/hashicorp/go-plugin"
"github.com/jhump/protoreflect/grpcreflect"
"github.com/kralicky/ragu/compat"
"github.com/rancher/opni/pkg/plugins"
"github.com/rancher/opni/pkg/plugins/apis/apiextensions"
"github.com/rancher/opni/pkg/util"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
const (
ManagementAPIExtensionPluginID = "opni.apiextensions.ManagementAPIExtension"
ServiceID = "apiextensions.ManagementAPIExtension"
)
type managementApiExtensionPlugin struct {
plugin.NetRPCUnsupportedPlugin
extensionSrv *mgmtExtensionServerImpl
}
var _ plugin.GRPCPlugin = (*managementApiExtensionPlugin)(nil)
func (p *managementApiExtensionPlugin) GRPCServer(
_ *plugin.GRPCBroker,
s *grpc.Server,
) error {
apiextensions.RegisterManagementAPIExtensionServer(s, p.extensionSrv)
for _, sp := range p.extensionSrv.services {
s.RegisterService(sp.Unpack())
}
return nil
}
func (p *managementApiExtensionPlugin) GRPCClient(
ctx context.Context,
_ *plugin.GRPCBroker,
c *grpc.ClientConn,
) (interface{}, error) {
if err := plugins.CheckAvailability(ctx, c, ServiceID); err != nil {
return nil, err
}
return apiextensions.NewManagementAPIExtensionClient(c), nil
}
func NewPlugin(services ...util.ServicePackInterface) plugin.Plugin {
return &managementApiExtensionPlugin{
extensionSrv: &mgmtExtensionServerImpl{
services: services,
},
}
}
type mgmtExtensionServerImpl struct {
apiextensions.UnimplementedManagementAPIExtensionServer
services []util.ServicePackInterface
}
func (e *mgmtExtensionServerImpl) Descriptors(_ context.Context, _ *emptypb.Empty) (*apiextensions.ServiceDescriptorProtoList, error) {
list := &apiextensions.ServiceDescriptorProtoList{}
for _, s := range e.services {
rawDesc, _ := s.Unpack()
desc, err := grpcreflect.LoadServiceDescriptor(rawDesc)
fqn := desc.GetFullyQualifiedName()
sd := util.ProtoClone(desc.AsServiceDescriptorProto())
sd.Name = &fqn
if err != nil {
return nil, err
}
list.Items = append(list.Items, sd)
}
return list, nil
}
var _ apiextensions.ManagementAPIExtensionServer = (*mgmtExtensionServerImpl)(nil)
func init() {
compat.LoadGogoFileDescriptor("k8s.io/api/core/v1/generated.proto")
compat.LoadGogoFileDescriptor("k8s.io/apimachinery/pkg/api/resource/generated.proto")
plugins.GatewayScheme.Add(ManagementAPIExtensionPluginID, NewPlugin())
}