-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathserver.go
352 lines (290 loc) · 8.98 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"net"
"path"
"time"
log "github.com/Sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
"fmt"
"github.com/davecgh/go-spew/spew"
"flag"
"strings"
"os"
)
const (
ResourceNamePrefix = "hostdev.k8s.io/"
serverSock = pluginapi.DevicePluginPath + "hostdev.sock"
)
func init() {
spew.Config.DisablePointerAddresses = true
spew.Config.DisableCapacities = true
spew.Config.MaxDepth = 2
}
type DevConfig struct {
DevName string
// must be "rwm" or it's subsets
Permissions string
}
type HostDevicePluginConfig struct {
DevList []*DevConfig
}
// HostDevicePlugin represents 1 host device
type HostDevicePlugin struct {
// device node on host, for example: /dev/mem, /dev/cuse
DevName string
// must be "rwm" or it's subsets
Permissions string
// for example: dev_mem, dev_cuse
NormalizedName string
// Resource name to register with kubelet, for example: hostdev/dev_mem
ResourceName string
// DevicePluginPath ("/var/lib/kubelet/device-plugins/") + dev_mem.sock
UnixSockPath string
UnixSock net.Listener
GrpcServer *grpc.Server
// pre-setup Device
Dev []*pluginapi.Device
IsRigistered bool
StopChan chan interface{}
}
type HostDevicePluginManager struct {
Config *HostDevicePluginConfig
Plugins []*HostDevicePlugin
}
func NewHostDevicePluginManager(cfg *HostDevicePluginConfig) (*HostDevicePluginManager, error) {
mgr := HostDevicePluginManager{
Config: cfg,
Plugins: make([]*HostDevicePlugin, 0, 8),
}
for _, devCfg := range cfg.DevList {
plugin, err := NewHostDevicePlugin(devCfg)
if err != nil {
log.Fatal(err)
}
mgr.Plugins = append(mgr.Plugins, plugin)
}
return &mgr, nil
}
func (mgr *HostDevicePluginManager) Stop() {
for _, plugin := range mgr.Plugins {
plugin.Stop()
}
}
func (mgr *HostDevicePluginManager) Start() error {
for _, plugin := range mgr.Plugins {
err := plugin.Start()
if err != nil {
return err
}
}
return nil
}
func (mgr *HostDevicePluginManager) RegisterToKubelet() error {
for _, plugin := range mgr.Plugins {
if plugin.IsRigistered {
continue
}
err := plugin.RegisterToKubelet()
if err != nil {
return err
}
}
return nil
}
var flagDevList = flag.String("devs", "",
"The list of devices seperated by comma. For example: /dev/mem:rwm,/dev/ecryptfs:r")
func ParseDevConfig(dev string)(*DevConfig, error) {
if dev == "" {
return nil, fmt.Errorf("Must have arg --devs , for example, --devs /dev/mem:rwm")
}
devCfg := DevConfig{}
s := strings.Split(dev, ":")
if len(s) != 2 {
return nil, fmt.Errorf("ParseDevConfig failed for: %s. Must have 1 [:], for example, /dev/mem:rwm", dev)
}
devCfg.DevName = s[0]
devCfg.Permissions = s[1]
fileInfo, err := os.Stat(devCfg.DevName)
if err != nil {
return nil, fmt.Errorf("ParseDevConfig failed for: %s. stat of %s failed: %v",
dev, devCfg.DevName, err)
}
if (fileInfo.Mode() & os.ModeDevice) == 0 {
return nil, fmt.Errorf("ParseDevConfig failed for: %s. %s is not a device file",
dev, devCfg.DevName)
}
if len(devCfg.Permissions) > 3 || len(devCfg.Permissions) == 0 {
return nil, fmt.Errorf("ParseDevConfig failed for: %s. Invalid permission string: %s. length must 1,2,3",
dev, devCfg.Permissions)
}
for _, c := range "rwm" {
if strings.Index(devCfg.Permissions, string(c)) != strings.LastIndex(devCfg.Permissions, string(c)) {
return nil, fmt.Errorf("ParseDevConfig failed for: %s. Invalid permission string: %s. dup of %c",
dev, devCfg.Permissions, c)
}
}
for _, c := range devCfg.Permissions {
if !strings.Contains("rwm", string(c)) {
return nil, fmt.Errorf("ParseDevConfig failed for: %s. Invalid permission string: %s. Must be subset of rwm",
dev, devCfg.Permissions)
}
}
return &devCfg, nil
}
// for unit test
func LoadConfigImpl(arguments []string) (*HostDevicePluginConfig, error) {
// Parse command-line arguments
//flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flag.CommandLine.Parse(arguments)
cfg := HostDevicePluginConfig{
DevList: make([]*DevConfig, 0, 2),
}
devs := strings.Split(*flagDevList, ",")
for _, dev := range devs {
devCfg, err := ParseDevConfig(dev)
if err != nil {
return nil, err
}
cfg.DevList = append(cfg.DevList, devCfg)
}
return &cfg, nil
}
func loadConfig() (*HostDevicePluginConfig, error) {
return LoadConfigImpl(os.Args[1:])
}
func NomalizeDevName(devName string) (string,error) {
if devName[0] != '/' {
return "", fmt.Errorf("Invalid dev name, must start with // ")
}
return strings.Replace(devName[1:], "/", "_", -1), nil
}
// NewHostDevicePlugin returns an initialized HostDevicePlugin
func NewHostDevicePlugin(devCfg *DevConfig) (*HostDevicePlugin, error) {
normalizedName, err := NomalizeDevName(devCfg.DevName)
if err != nil {
return nil, err
}
devs := []*pluginapi.Device {
&pluginapi.Device{ID: devCfg.DevName, Health: pluginapi.Healthy},
}
return &HostDevicePlugin{
DevName: devCfg.DevName,
Permissions: devCfg.Permissions,
NormalizedName: normalizedName,
ResourceName: ResourceNamePrefix + normalizedName,
UnixSockPath: pluginapi.DevicePluginPath + normalizedName,
Dev: devs,
StopChan: make(chan interface{}),
IsRigistered: false,
}, nil
}
// dial establishes the gRPC communication with the registered device plugin.
func dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, error) {
c, err := grpc.Dial(unixSocketPath, grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithTimeout(timeout),
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
}),
)
if err != nil {
fmt.Errorf("dial error: %v\n", err)
return nil, err
}
return c, nil
}
// Start starts the gRPC server of the device plugin
func (plugin *HostDevicePlugin) Start() error {
sock, err := net.Listen("unix", plugin.UnixSockPath)
if err != nil {
return err
}
plugin.UnixSock = sock
plugin.GrpcServer = grpc.NewServer([]grpc.ServerOption{}...)
pluginapi.RegisterDevicePluginServer(plugin.GrpcServer, plugin)
go plugin.GrpcServer.Serve(plugin.UnixSock)
// Wait for server to start by launching a blocking connection
conn, err := dial(plugin.UnixSockPath, 5*time.Second)
if err != nil {
return err
}
conn.Close()
return nil
}
// Stop stops the gRPC server
func (plugin *HostDevicePlugin) Stop() error {
if plugin.GrpcServer == nil {
return nil
}
plugin.GrpcServer.Stop()
plugin.GrpcServer = nil
close(plugin.StopChan)
return nil
}
// Register registers the device plugin for the given resourceName with Kubelet.
func (plugin *HostDevicePlugin) RegisterToKubelet() error {
conn, err := dial(pluginapi.KubeletSocket, 5*time.Second)
if err != nil {
log.Errorf("fail to dial %s: %v\n", pluginapi.KubeletSocket, err)
return err
}
defer conn.Close()
client := pluginapi.NewRegistrationClient(conn)
reqt := &pluginapi.RegisterRequest{
Version: pluginapi.Version,
Endpoint: path.Base(plugin.UnixSockPath),
ResourceName: plugin.ResourceName,
}
_, err = client.Register(context.Background(), reqt)
if err != nil {
plugin.IsRigistered = false
log.Errorf(" Register %s error: %v\n", plugin.DevName, err)
return err
}
plugin.IsRigistered = true
log.Infof(" Register %s success\n%s\n", plugin.DevName, spew.Sprint(plugin))
return nil
}
// ListAndWatch lists devices and update that list according to the health status
func (plugin *HostDevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
s.Send(&pluginapi.ListAndWatchResponse{Devices: plugin.Dev})
ticker := time.NewTicker(time.Second * 10)
for {
select {
case <-plugin.StopChan:
return nil
case <-ticker.C:
s.Send(&pluginapi.ListAndWatchResponse{Devices: plugin.Dev})
}
}
return nil
}
// Allocate which return list of devices.
func (plugin *HostDevicePlugin) Allocate(ctx context.Context, r *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) {
//spew.Printf("Context: %#v\n", ctx)
spew.Printf("AllocateRequest: %#v\n", *r)
response := pluginapi.AllocateResponse{}
devSpec := pluginapi.DeviceSpec {
HostPath: plugin.DevName,
ContainerPath: plugin.DevName,
Permissions: plugin.Permissions,
}
//log.Debugf("Request IDs: %v", r)
var devicesList []*pluginapi.ContainerAllocateResponse
devicesList = append(devicesList, &pluginapi.ContainerAllocateResponse{
Envs: make(map[string]string),
Annotations: make(map[string]string),
Devices: []*pluginapi.DeviceSpec{&devSpec},
Mounts: nil,
})
response.ContainerResponses = devicesList
spew.Printf("AllocateResponse: %#v\n", devicesList)
return &response, nil
}
func (plugin *HostDevicePlugin) GetDevicePluginOptions(context.Context, *pluginapi.Empty) (*pluginapi.DevicePluginOptions, error) {
return &pluginapi.DevicePluginOptions{PreStartRequired: false}, nil
}
func (plugin *HostDevicePlugin) PreStartContainer(context.Context, *pluginapi.PreStartContainerRequest) (*pluginapi.PreStartContainerResponse, error) {
return &pluginapi.PreStartContainerResponse{}, nil
}