-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
247 lines (197 loc) · 5.94 KB
/
driver.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/docker/go-plugins-helpers/volume"
)
const (
driverScope = "global"
driverName = "voletc"
)
type DriverConfig struct {
MountBaseDir string
BackendType string
BackendAddr string
Prefix string
EncryptionKey string
}
func NewDriverConfig(backendUri, basedir, prefix string) *DriverConfig {
idx := strings.Index(backendUri, "://")
d := &DriverConfig{
MountBaseDir: filepath.Join(basedir, prefix),
BackendType: backendUri[:idx],
BackendAddr: backendUri[idx+3:],
Prefix: prefix,
}
if !strings.HasSuffix(d.MountBaseDir, "/") {
d.MountBaseDir = d.MountBaseDir + "/"
}
return d
}
type MyVolumeDriver struct {
cfg *DriverConfig
ve *VolEtc
be Backend
}
func NewVolumeDriver(cfg *DriverConfig) (*MyVolumeDriver, error) {
d := &MyVolumeDriver{cfg: cfg}
os.MkdirAll(d.cfg.MountBaseDir, 0777)
be, err := NewBackend(cfg)
if err == nil {
d.be = be
d.ve = &VolEtc{be}
}
return d, err
}
// Instruct the plugin that the user wants to create a volume, given a user specified
// volume name. The plugin does not need to actually manifest the volume on the
// filesystem yet (until Mount is called). Opts is a map of driver specific options
// passed through from the user request.
func (m *MyVolumeDriver) Create(req volume.Request) volume.Response {
log.Printf("[Create] Request: %+v\n", req)
// Create kv structure on backend.
_, err := m.ve.Get(req.Name)
if err == nil {
return volume.Response{Err: "exists: " + req.Name}
}
c, err := NewAppConfigFromName(req.Name, m.be)
if err != nil {
return volume.Response{Err: err.Error()}
}
mp, err := parseCreateReqOptions(req.Options)
if err != nil {
return volume.Response{Err: err.Error()}
}
c.Set(mp)
if err = c.Commit(); err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{}
}
// Get the list of volumes registered with the plugin.
func (m *MyVolumeDriver) List(req volume.Request) volume.Response {
log.Printf("[List] Request: %+v\n", req)
ls, err := m.ve.List()
if err != nil {
return volume.Response{Err: err.Error()}
}
resp := volume.Response{Capabilities: volume.Capability{Scope: driverScope}}
resp.Volumes = make([]*volume.Volume, len(ls))
i := 0
for _, v := range ls {
resp.Volumes[i] = &volume.Volume{
Name: v.QualifiedName(),
Mountpoint: m.cfg.MountBaseDir + v.getOpaque(v.Env),
}
i++
}
log.Printf("[List] Response: %+v", resp)
return resp
}
// Get the volume info.
func (m *MyVolumeDriver) Get(req volume.Request) volume.Response {
log.Printf("[Get] Request: %+v\n", req)
c, err := m.ve.Get(req.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
resp := volume.Response{
Capabilities: volume.Capability{Scope: driverScope},
}
resp.Volume = &volume.Volume{
Name: req.Name,
Mountpoint: m.cfg.MountBaseDir + c.getOpaque(c.Env),
Status: c.Metadata(),
}
log.Printf("[Get] Response: %+v\n", resp)
return resp
}
// Delete the specified volume from disk. This request is issued when a user invokes
// docker rm -v to remove volumes associated with a container.
func (m *MyVolumeDriver) Remove(req volume.Request) volume.Response {
log.Printf("[Remove] Request: %+v\n", req)
c, err := m.ve.Get(req.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
resp := volume.Response{}
if err = c.Destroy(); err != nil {
resp.Err = err.Error()
}
log.Printf("[Remove] Response: %+v\n", resp)
return resp
}
// Respond with the path on the host filesystem where the volume has been made available,
// and/or a string error if an error occurred. Mountpoint is optional, however the plugin
// may be queried again later if one is not provided.
func (m *MyVolumeDriver) Path(req volume.Request) volume.Response {
log.Printf("[Path] Request: %+v\n", req)
c, err := m.ve.Get(req.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
resp := volume.Response{Mountpoint: m.cfg.MountBaseDir + c.getOpaque(c.Env)}
log.Printf("[Path] Response: %+v\n", resp)
return resp
}
func (m *MyVolumeDriver) Mount(req volume.MountRequest) volume.Response {
log.Printf("Mount: %+v\n", req)
c, err := m.ve.Get(req.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
dpath := m.cfg.MountBaseDir + c.getOpaque(c.Env)
if err = os.MkdirAll(dpath, 0777); err == nil {
err = c.Generate(dpath)
}
if err != nil {
return volume.Response{Err: err.Error()}
}
return volume.Response{Mountpoint: dpath}
}
// Indication that Docker no longer is using the named volume. This is called
// once per container stop. Plugin may deduce that it is safe to deprovision it at this point.
func (m *MyVolumeDriver) Unmount(req volume.UnmountRequest) volume.Response {
log.Printf("[Unmount] Request: %+v\n", req)
c, err := m.ve.Get(req.Name)
if err != nil {
return volume.Response{Err: err.Error()}
}
dpath := m.cfg.MountBaseDir + c.getOpaque(c.Env)
os.RemoveAll(dpath)
return volume.Response{}
}
// Get the list of capabilities the driver supports. The driver is not required
// to implement this endpoint, however in such cases the default values will be taken.
func (m *MyVolumeDriver) Capabilities(req volume.Request) volume.Response {
return volume.Response{Capabilities: volume.Capability{Scope: driverScope}}
}
// convert template:<name> to templates/<name> for storage
func parseCreateReqOptions(m map[string]string) (map[string][]byte, error) {
out := map[string][]byte{}
for k, v := range m {
if strings.HasPrefix(k, "template:") {
l := strings.Index(k, ":") + 1
var val []byte
if strings.HasPrefix(v, "/") || strings.HasPrefix(v, "./") {
b, err := ioutil.ReadFile(v)
if err != nil {
return nil, err
}
val = b
} else {
val = []byte(v)
}
out["templates/"+k[l:]] = val
} else if strings.HasPrefix(k, "templates/") {
return nil, fmt.Errorf("reserved prefix: 'templates/' in '%s'", k)
} else {
out[k] = []byte(v)
}
}
return out, nil
}