forked from docker/go-plugins-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
408 lines (357 loc) · 11.2 KB
/
api.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
package graphdriver
// See https://github.com/docker/docker/blob/master/experimental/plugins_graphdriver.md
import (
"io"
"net/http"
graphDriver "github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/pkg/containerfs"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/go-plugins-helpers/sdk"
)
const (
// DefaultDockerRootDirectory is the default directory where graph drivers will be created.
DefaultDockerRootDirectory = "/var/lib/docker/graph"
manifest = `{"Implements": ["GraphDriver"]}`
initPath = "/GraphDriver.Init"
createPath = "/GraphDriver.Create"
createRWPath = "/GraphDriver.CreateReadWrite"
removePath = "/GraphDriver.Remove"
getPath = "/GraphDriver.Get"
putPath = "/GraphDriver.Put"
existsPath = "/GraphDriver.Exists"
statusPath = "/GraphDriver.Status"
getMetadataPath = "/GraphDriver.GetMetadata"
cleanupPath = "/GraphDriver.Cleanup"
diffPath = "/GraphDriver.Diff"
changesPath = "/GraphDriver.Changes"
applyDiffPath = "/GraphDriver.ApplyDiff"
diffSizePath = "/GraphDriver.DiffSize"
capabilitiesPath = "/GraphDriver.Capabilities"
)
// Init
// InitRequest is the structure that docker's init requests are deserialized to.
type InitRequest struct {
Home string
Options []string `json:"Opts"`
UIDMaps []idtools.IDMap `json:"UIDMaps"`
GIDMaps []idtools.IDMap `json:"GIDMaps"`
}
// Create
// CreateRequest is the structure that docker's create requests are deserialized to.
type CreateRequest struct {
ID string
Parent string
MountLabel string
StorageOpt map[string]string
}
// Remove
// RemoveRequest is the structure that docker's remove requests are deserialized to.
type RemoveRequest struct {
ID string
}
// Get
// GetRequest is the structure that docker's get requests are deserialized to.
type GetRequest struct {
ID string
MountLabel string
}
// GetResponse is the strucutre that docker's remove responses are serialized to.
type GetResponse struct {
Dir string
}
// Put
// PutRequest is the structure that docker's put requests are deserialized to.
type PutRequest struct {
ID string
}
// Exists
// ExistsRequest is the structure that docker's exists requests are deserialized to.
type ExistsRequest struct {
ID string
}
// ExistsResponse is the structure that docker's exists responses are serialized to.
type ExistsResponse struct {
Exists bool
}
// Status
// StatusRequest is the structure that docker's status requests are deserialized to.
type StatusRequest struct{}
// StatusResponse is the structure that docker's status responses are serialized to.
type StatusResponse struct {
Status [][2]string
}
// GetMetadata
// GetMetadataRequest is the structure that docker's getMetadata requests are deserialized to.
type GetMetadataRequest struct {
ID string
}
// GetMetadataResponse is the structure that docker's getMetadata responses are serialized to.
type GetMetadataResponse struct {
Metadata map[string]string
}
// Cleanup
// CleanupRequest is the structure that docker's cleanup requests are deserialized to.
type CleanupRequest struct{}
// Diff
// DiffRequest is the structure that docker's diff requests are deserialized to.
type DiffRequest struct {
ID string
Parent string
}
// DiffResponse is the structure that docker's diff responses are serialized to.
type DiffResponse struct {
Stream io.ReadCloser // TAR STREAM
}
// Changes
// ChangesRequest is the structure that docker's changes requests are deserialized to.
type ChangesRequest struct {
ID string
Parent string
}
// ChangesResponse is the structure that docker's changes responses are serialized to.
type ChangesResponse struct {
Changes []Change
}
// ChangeKind represents the type of change mage
type ChangeKind int
const (
// Modified is a ChangeKind used when an item has been modified
Modified ChangeKind = iota
// Added is a ChangeKind used when an item has been added
Added
// Deleted is a ChangeKind used when an item has been deleted
Deleted
)
// Change is the structure that docker's individual changes are serialized to.
type Change struct {
Path string
Kind ChangeKind
}
// ApplyDiff
// ApplyDiffRequest is the structure that docker's applyDiff requests are deserialized to.
type ApplyDiffRequest struct {
Stream io.Reader // TAR STREAM
ID string
Parent string
}
// ApplyDiffResponse is the structure that docker's applyDiff responses are serialized to.
type ApplyDiffResponse struct {
Size int64
}
// DiffSize
// DiffSizeRequest is the structure that docker's diffSize requests are deserialized to.
type DiffSizeRequest struct {
ID string
Parent string
}
// DiffSizeResponse is the structure that docker's diffSize responses are serialized to.
type DiffSizeResponse struct {
Size int64
}
// CapabilitiesRequest is the structure that docker's capabilities requests are deserialized to.
type CapabilitiesRequest struct{}
// CapabilitiesResponse is the structure that docker's capabilities responses are serialized to.
type CapabilitiesResponse struct {
Capabilities graphDriver.Capabilities
}
// ErrorResponse is a formatted error message that docker can understand
type ErrorResponse struct {
Err string
}
// NewErrorResponse creates an ErrorResponse with the provided message
func NewErrorResponse(msg string) *ErrorResponse {
return &ErrorResponse{Err: msg}
}
// Driver represent the interface a driver must fulfill.
type Driver interface {
Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) error
Create(id, parent, mountlabel string, storageOpt map[string]string) error
CreateReadWrite(id, parent, mountlabel string, storageOpt map[string]string) error
Remove(id string) error
Get(id, mountLabel string) (containerfs.ContainerFS, error)
Put(id string) error
Exists(id string) bool
Status() [][2]string
GetMetadata(id string) (map[string]string, error)
Cleanup() error
Diff(id, parent string) io.ReadCloser
Changes(id, parent string) ([]Change, error)
ApplyDiff(id, parent string, archive io.Reader) (int64, error)
DiffSize(id, parent string) (int64, error)
Capabilities() graphDriver.Capabilities
}
// Handler forwards requests and responses between the docker daemon and the plugin.
type Handler struct {
driver Driver
sdk.Handler
}
// NewHandler initializes the request handler with a driver implementation.
func NewHandler(driver Driver) *Handler {
h := &Handler{driver, sdk.NewHandler(manifest)}
h.initMux()
return h
}
func (h *Handler) initMux() {
h.HandleFunc(initPath, func(w http.ResponseWriter, r *http.Request) {
req := InitRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
err = h.driver.Init(req.Home, req.Options, req.UIDMaps, req.GIDMaps)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, struct{}{}, false)
})
h.HandleFunc(createPath, func(w http.ResponseWriter, r *http.Request) {
req := CreateRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
err = h.driver.Create(req.ID, req.Parent, req.MountLabel, req.StorageOpt)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, struct{}{}, false)
})
h.HandleFunc(createRWPath, func(w http.ResponseWriter, r *http.Request) {
req := CreateRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
err = h.driver.CreateReadWrite(req.ID, req.Parent, req.MountLabel, req.StorageOpt)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, struct{}{}, false)
})
h.HandleFunc(removePath, func(w http.ResponseWriter, r *http.Request) {
req := RemoveRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
err = h.driver.Remove(req.ID)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, struct{}{}, false)
})
h.HandleFunc(getPath, func(w http.ResponseWriter, r *http.Request) {
req := GetRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
dir, err := h.driver.Get(req.ID, req.MountLabel)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, &GetResponse{Dir: dir.Path()}, false)
})
h.HandleFunc(putPath, func(w http.ResponseWriter, r *http.Request) {
req := PutRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
err = h.driver.Put(req.ID)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, struct{}{}, false)
})
h.HandleFunc(existsPath, func(w http.ResponseWriter, r *http.Request) {
req := ExistsRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
exists := h.driver.Exists(req.ID)
sdk.EncodeResponse(w, &ExistsResponse{Exists: exists}, false)
})
h.HandleFunc(statusPath, func(w http.ResponseWriter, r *http.Request) {
status := h.driver.Status()
sdk.EncodeResponse(w, &StatusResponse{Status: status}, false)
})
h.HandleFunc(getMetadataPath, func(w http.ResponseWriter, r *http.Request) {
req := GetMetadataRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
metadata, err := h.driver.GetMetadata(req.ID)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, &GetMetadataResponse{Metadata: metadata}, false)
})
h.HandleFunc(cleanupPath, func(w http.ResponseWriter, r *http.Request) {
err := h.driver.Cleanup()
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, struct{}{}, false)
})
h.HandleFunc(diffPath, func(w http.ResponseWriter, r *http.Request) {
req := DiffRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
stream := h.driver.Diff(req.ID, req.Parent)
sdk.StreamResponse(w, stream)
})
h.HandleFunc(changesPath, func(w http.ResponseWriter, r *http.Request) {
req := ChangesRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
changes, err := h.driver.Changes(req.ID, req.Parent)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, &ChangesResponse{Changes: changes}, false)
})
h.HandleFunc(applyDiffPath, func(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
id := params.Get("id")
parent := params.Get("parent")
size, err := h.driver.ApplyDiff(id, parent, r.Body)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, &ApplyDiffResponse{Size: size}, false)
})
h.HandleFunc(diffSizePath, func(w http.ResponseWriter, r *http.Request) {
req := DiffRequest{}
err := sdk.DecodeRequest(w, r, &req)
if err != nil {
return
}
size, err := h.driver.DiffSize(req.ID, req.Parent)
if err != nil {
sdk.EncodeResponse(w, NewErrorResponse(err.Error()), true)
return
}
sdk.EncodeResponse(w, &DiffSizeResponse{Size: size}, false)
})
h.HandleFunc(capabilitiesPath, func(w http.ResponseWriter, r *http.Request) {
caps := h.driver.Capabilities()
sdk.EncodeResponse(w, &CapabilitiesResponse{Capabilities: caps}, false)
})
}