This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
170 lines (144 loc) · 3.92 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
package main
import (
"os"
"syscall"
"github.com/avagin/ploop-flexvol/volume"
"github.com/jaxxstorm/flexvolume"
"github.com/kolyshkin/goploop-cli"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "ploop flexvolume"
app.Usage = "Mount ploop volumes in kubernetes using the flexvolume driver"
app.Commands = flexvolume.Commands(Ploop{})
app.Authors = []cli.Author{
cli.Author{
Name: "Lee Briggs",
},
}
app.Version = "0.1a"
app.Run(os.Args)
}
type Ploop struct{}
func (p Ploop) Init() flexvolume.Response {
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Ploop is available",
}
}
func (p Ploop) Attach(options map[string]string) flexvolume.Response {
if options["volumePath"] == "" {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Must specify a volume path",
}
}
if options["volumeId"] == "" {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Must specify a volume id",
}
}
if _, err := os.Stat(options["volumePath"] + "/" + options["volumeId"] + "/" + "DiskDescriptor.xml"); err == nil {
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Volume already exists",
Device: options["volumePath"] + "/" + options["volumeId"],
}
}
if err := volume.Create(options); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully attached the ploop volume",
Device: options["volumePath"] + "/" + options["volumeId"] + "/" + options["volumeId"],
}
}
func (p Ploop) Detach(device string) flexvolume.Response {
if err := ploop.UmountByDevice(device); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Unable to detach ploop volume",
Device: device,
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully detached the ploop volume",
Device: device,
}
}
func (p Ploop) Mount(target, device string, options map[string]string) flexvolume.Response {
// make the target directory we're going to mount to
err := os.MkdirAll(target, 0755)
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
Device: device,
}
}
// open the disk descriptor first
volume, err := ploop.Open(options["volumePath"] + "/" + options["volumeId"] + "/" + "DiskDescriptor.xml")
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
Device: device,
}
}
defer volume.Close()
if m, _ := volume.IsMounted(); !m {
// If it's mounted, let's mount it!
readonly := false
if options["kubernetes.io/readwrite"] == "ro" {
readonly = true
}
mp := ploop.MountParam{Target: target, Readonly: readonly}
dev, err := volume.Mount(&mp)
if err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: err.Error(),
Device: dev,
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully mounted the ploop volume",
Device: device,
}
} else {
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Ploop volume already mounted",
Device: device,
}
}
}
func (p Ploop) Unmount(mount string) flexvolume.Response {
if err := syscall.Unmount(mount, 0x2); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Unable to unmount ploop volume from pod",
Device: mount,
}
}
if err := os.Remove(mount); err != nil {
return flexvolume.Response{
Status: flexvolume.StatusFailure,
Message: "Unable to remove stale directory from pod",
Device: mount,
}
}
return flexvolume.Response{
Status: flexvolume.StatusSuccess,
Message: "Successfully unmounted the ploop volume",
Device: mount,
}
}