forked from nbryant42/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_csi.go
454 lines (384 loc) · 10.4 KB
/
mod_csi.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package csi
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
gofig "github.com/akutz/gofig/types"
dvol "github.com/docker/go-plugins-helpers/volume"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
"github.com/thecodeteam/gocsi"
"github.com/thecodeteam/gocsi/csi"
"github.com/rexray/rexray/agent"
"github.com/rexray/rexray/core"
apictx "github.com/rexray/rexray/libstorage/api/context"
"github.com/rexray/rexray/libstorage/api/registry"
apitypes "github.com/rexray/rexray/libstorage/api/types"
rrutils "github.com/rexray/rexray/util"
)
type csiServer interface {
csi.ControllerServer
csi.IdentityServer
csi.NodeServer
}
type mod struct {
lsc apitypes.Client
ctx apitypes.Context
config gofig.Config
name string
addr string
desc string
gs *grpc.Server
cs *csiService
lis net.Listener
cancel context.CancelFunc
waitForCancel sync.WaitGroup
}
var loadGoPluginsFunc func(context.Context, ...string) error
const configFormat = `
rexray:
modules:
default-csi:
type: csi
desc: The default CSI module.
host: %s
disabled: false
`
func init() {
agent.RegisterModule("csi", newModule)
agent.RegisterModule("docker", newModule)
registry.RegisterConfigReg(
"CSI",
func(ctx apitypes.Context, r gofig.ConfigRegistration) {
pathConfig := apictx.MustPathConfig(ctx)
csiEndpoint := os.Getenv("CSI_ENDPOINT")
if csiEndpoint == "" {
csiEndpoint = path.Join(
pathConfig.Home,
"/run/docker/plugins/rexray.sock")
}
if csiEndpoint != "" {
// Register the default CSI module.
r.SetYAML(fmt.Sprintf(configFormat, csiEndpoint))
ctx.WithField("CSI_ENDPOINT", csiEndpoint).Info(
"configured default CSI module")
}
// Register the CSI module's configuration properties.
r.Key(gofig.String, "", "", "", "csi.endpoint")
r.Key(gofig.String, "", "libstorage", "",
"csi.driver", "csiDriver", "X_CSI_DRIVER")
r.Key(gofig.String, "", "", "",
"csi.goplugins", "csiGoPlugins", "X_CSI_GO_PLUGINS")
r.Key(gofig.Bool, "", false, "",
"csi.nodocker", "csiNoDocker", "X_CSI_NO_DOCKER")
})
}
func newModule(
ctx apitypes.Context,
c *agent.Config) (agent.Module, error) {
host := strings.Trim(c.Address, " ")
if host == "" {
return nil, errors.New("error: host is required")
}
c.Address = host
config := c.Config
m := &mod{
ctx: ctx,
config: config,
lsc: c.Client,
name: c.Name,
desc: c.Description,
addr: host,
}
// Determine what kind of driver this CSI module uses.
csiDriver := config.GetString("csi.driver")
ctx.WithFields(map[string]interface{}{
"mod.name": c.Name,
"csi.driver": csiDriver,
}).Info("configuring csi module's driver")
// Create the CSI service that will answer incoming requests.
if m.cs = newService(ctx, c.Name, csiDriver); m.cs == nil {
return nil, fmt.Errorf("invalid csi driver: %s", csiDriver)
}
// Create a gRPC server used to advertise the CSI service.
m.gs = newGrpcServer(ctx)
csi.RegisterControllerServer(m.gs, m.cs)
csi.RegisterIdentityServer(m.gs, m.cs)
csi.RegisterNodeServer(m.gs, m.cs)
return m, nil
}
func newGrpcServer(ctx apitypes.Context) *grpc.Server {
lout := rrutils.NewWriterFor(ctx.Infof)
lerr := rrutils.NewWriterFor(ctx.Errorf)
return grpc.NewServer(grpc.UnaryInterceptor(gocsi.ChainUnaryServer(
gocsi.ServerRequestIDInjector,
gocsi.NewServerRequestLogger(lout, lerr),
gocsi.NewServerResponseLogger(lout, lerr),
gocsi.ServerRequestValidator)))
}
var loadGoPluginsFuncOnce sync.Once
func doLoadGoPluginsFuncOnce(
ctx apitypes.Context,
config gofig.Config) (err error) {
loadGoPluginsFuncOnce.Do(func() {
if loadGoPluginsFunc != nil {
err = loadGoPluginsFunc(
ctx,
config.GetStringSlice("csi.goplugins")...)
}
})
return
}
const protoUnix = "unix"
func (m *mod) Start() error {
// Create the cancellation context for this module.
m.ctx, m.cancel = apictx.WithCancel(m.ctx)
ctx := m.ctx
doLoadGoPluginsFuncOnce(ctx, m.config)
// Check to see if the provided address is the same as that of
// the default docker module's address. If so then multiplex
// both Docker *and* CSI connections.
isMultiplexed := !m.config.GetBool("csi.nodocker")
// Use the GoCSI package to parse the address since its parsing
// function will handle an implied UNIX sock file by virtue of a
// vanilla file path.
proto, addr, err := gocsi.ParseProtoAddr(m.Address())
if err != nil {
return err
}
if isMultiplexed {
ctx.WithField("sockFile", addr).Info("multiplexed csi+docker endpoint")
} else {
ctx.WithField("sockFile", addr).Info("csi endpoint")
}
// ensure the sock file directory is created & remove
// any stale sock files with the same path
if proto == protoUnix {
os.MkdirAll(filepath.Dir(addr), 0755)
os.RemoveAll(addr)
}
// create a listener
l, err := net.Listen(proto, addr)
if err != nil {
return err
}
m.lis = l
var (
tcpm cmux.CMux
httpl net.Listener
grpcl net.Listener
http2 net.Listener
)
// If multiplexing Docker+CSI then create the multiplexer and the routers.
if isMultiplexed {
// Create a cmux object.
tcpm = cmux.New(l)
// Declare the match for different services required.
httpl = tcpm.Match(cmux.HTTP1Fast())
grpcl = tcpm.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings(
"content-type", "application/grpc"))
http2 = tcpm.Match(cmux.HTTP2())
m.waitForCancel.Add(3)
go func() {
<-ctx.Done()
httpl.Close()
grpcl.Close()
http2.Close()
m.waitForCancel.Done()
m.waitForCancel.Done()
m.waitForCancel.Done()
}()
}
// Start the CSI endpoint
go func() {
go func() {
if err := m.cs.Serve(ctx, nil); err != nil {
if err.Error() != http.ErrServerClosed.Error() {
panic(err)
}
}
}()
// Alias the listener to use.
ll := l
// If multiplexing Docker+CSI then use the multiplexed router.
if isMultiplexed {
ll = grpcl
}
err := m.gs.Serve(ll)
if err != nil && err != grpc.ErrServerStopped {
// If not multiplexing Docker+CSI then panic on error,
// otherwise leave that to the multiplexer.
if !isMultiplexed {
if !strings.Contains(err.Error(),
"use of closed network connection") {
panic(err)
}
} else if !strings.Contains(err.Error(),
"mux: listener closed") {
ctx.WithError(err).Warn(
"failed to start csi grpc server")
}
}
}()
// If not multiplexing Docker+CSI then nothing below is required.
if !isMultiplexed {
return nil
}
// Determine the type of Docker driver to create: bridge or legacy.
var (
dockerVolDriver dvol.Driver
warmDockerBridgeCache func()
useDockerBridge = !core.DockerLegacyMode
)
// If bridge mode is enabled then check if it should be disabled for
// the following libStorage storage platforms:
//
// * EFS
// * Isilon
// * S3FS
if useDockerBridge {
// If the CSI service type is libStorage then check if legacy
// mode is required.
if strings.EqualFold(m.cs.serviceType, "libstorage") {
svcName, ok := apictx.ServiceName(ctx)
if !ok {
return errors.New("missing service name")
}
svcInfo, err := m.lsc.API().ServiceInspect(ctx, svcName)
if err != nil {
err = fmt.Errorf(
"mod-csi: docker mode: "+
"libStorage.ServiceInspect: %s: failed: %v",
svcName, err)
ctx.Error(err)
return err
}
ctx.Infof("mod-csi: docker mode: service=%s", svcName)
if svcInfo.Driver == nil {
err = fmt.Errorf(
"mod-csi: docker mode: service: %s: nil driver", svcName)
ctx.Error(err)
return err
}
ctx.Debugf("mod-csi: docker mode: driver=%s", svcInfo.Driver.Name)
// If the libStorage driver is EFS, Isilon, or S3FS then
// Docker Bridge mode is not supported.
if ok, _ := regexp.MatchString(
svcInfo.Driver.Name, `(?i)^efs|isilon|s3fs$`); ok {
useDockerBridge = false
}
}
}
// Create a Docker driver either with the Docker Bridge or the
// Docker Legacy driver.
{
var err error
if useDockerBridge {
ctx.Info("mod-csi: docker mode: bridge")
dockerVolDriver, err = newDockerBridge(ctx, m.config, m.cs)
if err != nil {
return err
}
// Add one for the docker cache list call.
m.waitForCancel.Add(1)
// Define the function used to warm the Docker Bridge's
// volume cache.
warmDockerBridgeCache = func() {
defer m.waitForCancel.Done()
// Loop every one second until a successful attempt
// at listing the volumes using the bridge. This caches
// the volume name-to-ID mappings.
for {
if _, err := dockerVolDriver.List(); err == nil {
ctx.Info("mod-csi: docker bridge: warmed cache")
break
}
select {
case <-ctx.Done():
return
case <-time.After(time.Duration(1) * time.Second):
}
}
}
} else {
ctx.Info("mod-csi: docker mode: legacy")
dockerVolDriver, err = newDockerLegacy(ctx, m.config, m.lsc)
if err != nil {
return err
}
}
}
// Start the Docker Volume API
go func() {
var warnMsgDType string
if useDockerBridge {
go warmDockerBridgeCache()
warnMsgDType = "bridge"
} else {
warnMsgDType = "legacy"
}
dh := dvol.NewHandler(dockerVolDriver)
go func() {
if err := dh.Serve(httpl); err != nil {
if !strings.Contains(err.Error(),
"mux: listener closed") {
ctx.Warnf("mod-csi: failed to start http1 %s: %v",
warnMsgDType, err)
}
}
}()
go func() {
if err := dh.Serve(http2); err != nil {
if !strings.Contains(err.Error(),
"mux: listener closed") {
ctx.Warnf("mod-csi: failed to start http2 %s: %v",
warnMsgDType, err)
}
}
}()
}()
// Start multiplexing connections to either the CSI endpoint or
// Docker Volume API
go func() {
// Start cmux serving.
err := tcpm.Serve()
if err != nil && !strings.Contains(err.Error(),
"use of closed network connection") {
panic(err)
}
}()
return nil
}
func (m *mod) Stop() error {
// Invoke the module's context cancellation function and wait
// for its participants to finish their business.
m.cancel()
m.waitForCancel.Wait()
m.gs.GracefulStop()
m.cs.GracefulStop(m.ctx)
if m.lis != nil {
addr := m.lis.Addr()
if addr.Network() == protoUnix {
os.RemoveAll(addr.String())
}
}
return nil
}
func (m *mod) Name() string {
return m.name
}
func (m *mod) Description() string {
return m.desc
}
func (m *mod) Address() string {
return m.addr
}