forked from kolyshkin/ploop-flexvol
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
371 lines (311 loc) · 9.26 KB
/
main.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
package main
import (
"crypto/md5"
"encoding/base64"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/jaxxstorm/flexvolume"
"github.com/kolyshkin/goploop-cli"
"github.com/urfave/cli"
"github.com/virtuozzo/ploop-flexvol/vstorage"
"github.com/golang/glog"
)
func setupJournld() ([]string, *exec.Cmd, error) {
fd, err := syscall.Dup(syscall.Stdout)
if err != nil {
return nil, nil, err
}
syscall.CloseOnExec(fd)
flexvolume.SetRespFile(os.NewFile((uintptr)(fd), "RespFile"))
if err := flag.CommandLine.Parse([]string{"-logtostderr"}); err != nil {
return nil, nil, err
}
cmd := exec.Command("systemd-cat", "--identifier", "ploop-flexvol")
if err != nil {
return nil, nil, err
}
pr, pw, err := os.Pipe()
if err != nil {
return nil, nil, fmt.Errorf("Unable to create a pipe: %v", err)
}
cmd.Stdin = pr
defer pr.Close()
defer pw.Close()
if err := syscall.Dup2(int(pw.Fd()), syscall.Stdout); err != nil {
return nil, nil, fmt.Errorf("Unable to redirect stdout: %v", err)
}
if err := syscall.Dup2(syscall.Stdout, syscall.Stderr); err != nil {
return nil, nil, fmt.Errorf("Unable to redirect stderr: %v", err)
}
if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("Unable to start systemd-cat: %v", err)
}
return os.Args, cmd, nil
}
func setupWrapperLogging() ([]string, *exec.Cmd, error) {
syscall.CloseOnExec(3)
flexvolume.SetRespFile(os.NewFile((uintptr)(3), "RespFile"))
if err := flag.CommandLine.Parse(os.Args[2:]); err != nil {
return nil, nil, err
}
return flag.CommandLine.Args(), nil, nil
}
func setupLogging() ([]string, *exec.Cmd, error) {
if os.Args[1] == "wrapper" {
return setupWrapperLogging()
}
return setupJournld()
}
func setupEnvironment() error {
// prefer local ploop binary
bin := filepath.Dir(os.Args[0]) + "/bin"
pathEnv, _ := os.LookupEnv("PATH")
pathEnv = bin + ":" + pathEnv
err := os.Setenv("PATH", pathEnv)
if err != nil {
return err
}
return os.Setenv("LD_LIBRARY_PATH", bin)
}
func main() {
args, cmd, err := setupLogging()
if err != nil {
panic(err)
}
if cmd != nil {
defer func() {
syscall.Close(syscall.Stdout)
syscall.Close(syscall.Stderr)
cmd.Wait()
}()
}
err = setupEnvironment()
if err != nil {
panic(err)
}
app := cli.NewApp()
app.Name = "ploop flexvolume"
app.Usage = "Mount ploop volumes in kubernetes using the flexvolume driver"
app.Commands = flexvolume.Commands(Ploop{})
app.CommandNotFound = flexvolume.CommandNotFound
app.Authors = []cli.Author{
cli.Author{
Name: "Lee Briggs",
},
cli.Author{
Name: "Virtuozzo",
},
}
app.Version = "0.2a"
if glog.V(4) {
glog.Infof("Request: %v", args)
}
app.Run(args)
}
type Ploop struct{}
const workingDir = "/var/run/ploop-flexvol/"
func (p Ploop) Init() (*flexvolume.Response, error) {
return &flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Ploop is available",
}, nil
}
func (p Ploop) realWorkingDir(options map[string]string) string {
realDir := workingDir
if options["optionsFromSystem"] == "true" {
realDir = os.Getenv("kubeVstorageWorkingDir")
}
return realDir
}
func (p Ploop) path(options map[string]string) string {
path := "/"
if options["volumePath"] != "" {
path += options["volumePath"] + "/"
}
path += options["volumeID"]
return path
}
func (p Ploop) GetVolumeName(options map[string]string) (*flexvolume.Response, error) {
return &flexvolume.Response{
Status: flexvolume.StatusNotSupported,
}, nil
}
func prepareVstorage(clusterName, clusterPasswd string, mount string) error {
mounted, _ := vstorage.IsVstorage(mount)
if mounted {
return nil
}
// not mounted in proper place, prepare mount place and check other
// mounts
if err := os.MkdirAll(mount, 0700); err != nil {
return err
}
v := vstorage.Vstorage{clusterName}
p, _ := v.Mountpoint()
if p != "" {
return syscall.Mount(p, mount, "", syscall.MS_BIND, "")
}
if clusterPasswd == "" {
return errors.New("Please provide vstorage credentials")
}
if err := v.Auth(clusterPasswd); err != nil {
return err
}
if err := v.Mount(mount); err != nil {
return err
}
return nil
}
func (p Ploop) mountPloop(target, path string, volume *ploop.Ploop, readonly bool) (string, error) {
target = filepath.Clean(target)
path = filepath.Clean(path)
statePath := fmt.Sprintf("%s/mounts/ploop-%x", workingDir, md5.Sum([]byte(path)))
mntPath := fmt.Sprintf("%s/mnt", statePath)
if err := os.MkdirAll(mntPath, 0700); err != nil {
return "", err
}
mp := ploop.MountParam{Target: mntPath, Readonly: readonly}
_, err := volume.Mount(&mp)
if err != nil {
os.Remove(mntPath)
os.Remove(statePath)
return "", err
}
return statePath, nil
}
func (p Ploop) umountPloop(statePath string) error {
mountPath := fmt.Sprintf("%s/mnt", statePath)
if err := ploop.UmountByMount(mountPath); err != nil {
return err
}
if err := os.Remove(mountPath); err != nil {
return fmt.Errorf("Unable to remove %s: %v", mountPath, err)
}
if err := os.Remove(statePath); err != nil {
return fmt.Errorf("Unable to remove %s: %v", statePath, err)
}
return nil
}
func (p Ploop) Mount(target string, options map[string]string) (*flexvolume.Response, error) {
var clusterName, clusterPassword string
path := p.path(options)
readonly := false
if options["kubernetes.io/readwrite"] == "ro" {
readonly = true
}
if options["optionsFromSystem"] == "true" {
clusterName = os.Getenv("kubeVstorageClusterName")
clusterPassword = os.Getenv("kubeVstorageClusterPassword")
}else{
clusterName = options["kubernetes.io/secret/clusterName"]
clusterPassword = options["kubernetes.io/secret/clusterPassword"]
}
if clusterName != "" {
_cluster, err := base64.StdEncoding.DecodeString(clusterName)
if err != nil {
return nil, fmt.Errorf("Unable to decode a cluster name: %v", err.Error())
}
cluster := string(_cluster)
_passwd, err := base64.StdEncoding.DecodeString(clusterPassword)
if err != nil {
return nil, fmt.Errorf("Unable to decode a cluster password: %v", err.Error())
}
passwd := string(_passwd)
mount := filepath.Join(p.realWorkingDir(options),cluster)
if err := prepareVstorage(cluster, passwd, mount); err != nil {
return nil, err
}
path = mount + path
if !readonly {
// Node denial may lead to vstorage freezes. vstorage revoke operation before writing
// data will prevent this cases. Detach method is more suitable for it, but currently
// volume name is auto generated and does not include all neccessary credentials to
// perform volume revoke. It should be fixed when k8s community fixed getvolumename call
v := vstorage.Vstorage{cluster}
if err := v.Revoke(path); err != nil {
return nil, err
}
}
}
// open the disk descriptor first
volume, err := ploop.Open(path + "/" + "DiskDescriptor.xml")
if err != nil {
return nil, err
}
defer volume.Close()
if m, _ := volume.IsMounted(); !m {
stateDir := fmt.Sprintf("%s/mounts", workingDir)
if err := os.MkdirAll(stateDir, 0700); err != nil {
return nil, err
}
statePath, err := p.mountPloop(target, path, &volume, readonly)
if err != nil {
return nil, err
}
target = filepath.Clean(target)
// We need to know a mount point to make snapshots, so
// we create our mount point and then bind-mount it to "target"
// If it's mounted, let's mount it!
mntLink := fmt.Sprintf("%s/kube-%x", stateDir, md5.Sum([]byte(target)))
glog.Infof("Create symlink %s %s", statePath, mntLink)
if err := os.Symlink(statePath, mntLink); err != nil {
p.umountPloop(statePath)
return nil, err
}
mntPath := fmt.Sprintf("%s/mnt", statePath)
if err := syscall.Mount(mntPath, target, "", syscall.MS_BIND, ""); err != nil {
p.umountPloop(statePath)
os.Remove(mntLink)
return nil, fmt.Errorf("Unable to bind mount %s -> %s: %v", mntPath, target, err)
}
return &flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully mounted the ploop volume",
}, nil
} else {
return nil, fmt.Errorf("Ploop volume already mounted")
}
}
func (p Ploop) Unmount(mount string) (*flexvolume.Response, error) {
err := syscall.Unmount(mount, 0)
if err == syscall.EINVAL {
//This isn't a mount point, continue and allow the ploop volume to be cleaned up on retry
glog.Infof("%s isn't a mount point",mount)
} else if err != nil {
return nil, err
}
mount = filepath.Clean(mount)
mntLink := fmt.Sprintf("%s/mounts/kube-%x", workingDir, md5.Sum([]byte(mount)))
statePath, err := os.Readlink(mntLink)
if err != nil {
return nil, err
}
glog.Infof("Umount %s(%s)", statePath, mntLink)
if err := p.umountPloop(statePath); err != nil {
return nil, err
}
if err := os.Remove(mntLink); err != nil {
return nil, err
}
return &flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully unmounted the ploop volume",
}, nil
}
func (p Ploop) Attach(nodename string, options map[string]string) (*flexvolume.Response, error) {
return &flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: fmt.Sprintf("Successfully attached the ploop volume to node %s", nodename),
}, nil
}
func (p Ploop) Detach(device string, nodename string) (*flexvolume.Response, error) {
return &flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: fmt.Sprintf("Successfully detached the ploop volume %s from node %s", device, nodename),
}, nil
}