forked from nbryant42/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_csi.go
333 lines (282 loc) · 7.12 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
package csi
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
gofig "github.com/akutz/gofig/types"
dvol "github.com/docker/go-plugins-helpers/volume"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
"github.com/codedellemc/gocsi"
"github.com/codedellemc/gocsi/csi"
"github.com/codedellemc/rexray/agent"
apictx "github.com/codedellemc/rexray/libstorage/api/context"
"github.com/codedellemc/rexray/libstorage/api/registry"
apitypes "github.com/codedellemc/rexray/libstorage/api/types"
)
const (
modName = "csi"
)
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
}
var (
loadGoPluginsFunc func(context.Context, ...string) error
separators = regexp.MustCompile(`[ &_=+:]`)
dashes = regexp.MustCompile(`[\-]+`)
illegalPath = regexp.MustCompile(`[^[:alnum:]\~\-\./]`)
)
const configFormat = `
rexray:
modules:
default-csi:
type: csi
desc: The default CSI module.
host: %s
disabled: false
`
func init() {
agent.RegisterModule(modName, newModule)
registry.RegisterConfigReg(
"CSI",
func(ctx apitypes.Context, r gofig.ConfigRegistration) {
if v := os.Getenv("CSI_ENDPOINT"); v != "" {
ctx.WithField("CSI_ENDPOINT", v).Info(
"configuring default CSI module")
r.SetYAML(fmt.Sprintf(configFormat, v))
}
r.Key(gofig.String, "", "libstorage", "",
"csi.driver", "csiDriver", "X_CSI_DRIVER")
r.Key(gofig.String, "", "", "",
"csi.goplugins", "csiGoPlugins", "X_CSI_GO_PLUGINS")
})
}
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 := newLogger(ctx.Infof)
lerr := newLogger(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 {
ctx := m.ctx
doLoadGoPluginsFuncOnce(ctx, m.config)
// Get the path to the sock file used by the default
// Docker module for comparison.
dockerSockFile := path.Join(
apictx.MustPathConfig(ctx).Home,
"/run/docker/plugins/rexray.sock")
// 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
}
// 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 := proto == protoUnix && addr == dockerSockFile
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())
}
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 {
ctx.WithError(err).Warn(
"failed to start csi grpc server")
}
}
}()
// If not multiplexing Docker+CSI then nothing below is required.
if !isMultiplexed {
return nil
}
go func() {
dh := dvol.NewHandler(&dockerVolDriver{cs: m.cs, ctx: ctx})
go func() {
if err := dh.Serve(httpl); err != nil {
ctx.WithError(err).Warn(
"failed to start http1 docker->csi proxy")
}
}()
go func() {
if err := dh.Serve(http2); err != nil {
ctx.WithError(err).Warn(
"failed to start http2 docker->csi proxy")
}
}()
}()
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 {
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
}
type logger struct {
f func(msg string, args ...interface{})
w io.Writer
}
func newLogger(f func(msg string, args ...interface{})) *logger {
l := &logger{f: f}
r, w := io.Pipe()
l.w = w
go func() {
scan := bufio.NewScanner(r)
for scan.Scan() {
f(scan.Text())
}
}()
return l
}
func (l *logger) Write(data []byte) (int, error) {
return l.w.Write(data)
}