-
Notifications
You must be signed in to change notification settings - Fork 2k
/
server.go
89 lines (72 loc) · 2 KB
/
server.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package base
import (
"context"
"fmt"
plugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/plugins/base/proto"
)
// basePluginServer wraps a base plugin and exposes it via gRPC.
type basePluginServer struct {
broker *plugin.GRPCBroker
impl BasePlugin
}
func (b *basePluginServer) PluginInfo(context.Context, *proto.PluginInfoRequest) (*proto.PluginInfoResponse, error) {
resp, err := b.impl.PluginInfo()
if err != nil {
return nil, err
}
var ptype proto.PluginType
switch resp.Type {
case PluginTypeDriver:
ptype = proto.PluginType_DRIVER
case PluginTypeDevice:
ptype = proto.PluginType_DEVICE
default:
return nil, fmt.Errorf("plugin is of unknown type: %q", resp.Type)
}
presp := &proto.PluginInfoResponse{
Type: ptype,
PluginApiVersions: resp.PluginApiVersions,
PluginVersion: resp.PluginVersion,
Name: resp.Name,
}
return presp, nil
}
func (b *basePluginServer) ConfigSchema(context.Context, *proto.ConfigSchemaRequest) (*proto.ConfigSchemaResponse, error) {
spec, err := b.impl.ConfigSchema()
if err != nil {
return nil, err
}
presp := &proto.ConfigSchemaResponse{
Spec: spec,
}
return presp, nil
}
func (b *basePluginServer) SetConfig(ctx context.Context, req *proto.SetConfigRequest) (*proto.SetConfigResponse, error) {
info, err := b.impl.PluginInfo()
if err != nil {
return nil, err
}
// Client configuration is filtered based on plugin type
cfg := nomadConfigFromProto(req.GetNomadConfig())
filteredCfg := new(AgentConfig)
if cfg != nil {
switch info.Type {
case PluginTypeDriver:
filteredCfg.Driver = cfg.Driver
}
}
// Build the config request
c := &Config{
ApiVersion: req.GetPluginApiVersion(),
PluginConfig: req.GetMsgpackConfig(),
AgentConfig: filteredCfg,
}
// Set the config
if err := b.impl.SetConfig(c); err != nil {
return nil, fmt.Errorf("SetConfig failed: %v", err)
}
return &proto.SetConfigResponse{}, nil
}