-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_lister.go
143 lines (117 loc) · 4.41 KB
/
plugin_lister.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
/*
Copyright 2018, 2019 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package framework
import (
plugin "github.com/hashicorp/go-plugin"
"github.com/pkg/errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
proto "github.com/adi-bhardwaj/velero-modified/pkg/plugin/generated"
)
// PluginIdentifier uniquely identifies a plugin by command, kind, and name.
type PluginIdentifier struct {
Command string
Kind PluginKind
Name string
}
// PluginLister lists plugins.
type PluginLister interface {
ListPlugins() ([]PluginIdentifier, error)
}
// pluginLister implements PluginLister.
type pluginLister struct {
plugins []PluginIdentifier
}
// NewPluginLister returns a new PluginLister for plugins.
func NewPluginLister(plugins ...PluginIdentifier) PluginLister {
return &pluginLister{plugins: plugins}
}
// ListPlugins returns the pluginLister's plugins.
func (pl *pluginLister) ListPlugins() ([]PluginIdentifier, error) {
return pl.plugins, nil
}
// PluginListerPlugin is a go-plugin Plugin for a PluginLister.
type PluginListerPlugin struct {
plugin.NetRPCUnsupportedPlugin
impl PluginLister
}
// NewPluginListerPlugin creates a new PluginListerPlugin with impl as the server-side implementation.
func NewPluginListerPlugin(impl PluginLister) *PluginListerPlugin {
return &PluginListerPlugin{impl: impl}
}
//////////////////////////////////////////////////////////////////////////////
// client code
//////////////////////////////////////////////////////////////////////////////
// GRPCClient returns a PluginLister gRPC client.
func (p *PluginListerPlugin) GRPCClient(_ context.Context, _ *plugin.GRPCBroker, clientConn *grpc.ClientConn) (interface{}, error) {
return &PluginListerGRPCClient{grpcClient: proto.NewPluginListerClient(clientConn)}, nil
}
// PluginListerGRPCClient implements PluginLister and uses a gRPC client to make calls to the plugin server.
type PluginListerGRPCClient struct {
grpcClient proto.PluginListerClient
}
// ListPlugins uses the gRPC client to request the list of plugins from the server. It translates the protobuf response
// to []PluginIdentifier.
func (c *PluginListerGRPCClient) ListPlugins() ([]PluginIdentifier, error) {
resp, err := c.grpcClient.ListPlugins(context.Background(), &proto.Empty{})
if err != nil {
return nil, err
}
ret := make([]PluginIdentifier, len(resp.Plugins))
for i, id := range resp.Plugins {
if _, ok := AllPluginKinds()[id.Kind]; !ok {
return nil, errors.Errorf("invalid plugin kind: %s", id.Kind)
}
ret[i] = PluginIdentifier{
Command: id.Command,
Kind: PluginKind(id.Kind),
Name: id.Name,
}
}
return ret, nil
}
//////////////////////////////////////////////////////////////////////////////
// server code
//////////////////////////////////////////////////////////////////////////////
// GRPCServer registers a PluginLister gRPC server.
func (p *PluginListerPlugin) GRPCServer(_ *plugin.GRPCBroker, server *grpc.Server) error {
proto.RegisterPluginListerServer(server, &PluginListerGRPCServer{impl: p.impl})
return nil
}
// PluginListerGRPCServer implements the proto-generated PluginLister gRPC service interface. It accepts gRPC calls,
// forwards them to impl, and translates the responses to protobuf.
type PluginListerGRPCServer struct {
impl PluginLister
}
// ListPlugins returns a list of registered plugins, delegating to s.impl to perform the listing.
func (s *PluginListerGRPCServer) ListPlugins(ctx context.Context, req *proto.Empty) (*proto.ListPluginsResponse, error) {
list, err := s.impl.ListPlugins()
if err != nil {
return nil, err
}
plugins := make([]*proto.PluginIdentifier, len(list))
for i, id := range list {
if _, ok := AllPluginKinds()[id.Kind.String()]; !ok {
return nil, errors.Errorf("invalid plugin kind: %s", id.Kind)
}
plugins[i] = &proto.PluginIdentifier{
Command: id.Command,
Kind: id.Kind.String(),
Name: id.Name,
}
}
ret := &proto.ListPluginsResponse{
Plugins: plugins,
}
return ret, nil
}