forked from calavera/dkvolume
/
api.go
158 lines (130 loc) · 3.8 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
package dkvolume
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
)
const (
// DefaultDockerRootDirectory is the default directory where volumes will be created.
DefaultDockerRootDirectory = "/var/lib/docker-volumes"
defaultContentTypeV1_1 = "application/vnd.docker.plugins.v1.1+json"
defaultImplementationManifest = `{"Implements": ["VolumeDriver"]}`
activatePath = "/Plugin.Activate"
createPath = "/VolumeDriver.Create"
remotePath = "/VolumeDriver.Remove"
hostVirtualPath = "/VolumeDriver.Path"
mountPath = "/VolumeDriver.Mount"
unmountPath = "/VolumeDriver.Unmount"
)
// Request is the structure that docker's requests are deserialized to.
type Request struct {
Name string
Options map[string]string `json:"Opts,omitempty"`
}
// Response is the strucutre that the plugin's responses are serialized to.
type Response struct {
Mountpoint string
Err string
}
// Driver represent the interface a driver must fulfill.
type Driver interface {
Create(Request) Response
Remove(Request) Response
Path(Request) Response
Mount(Request) Response
Unmount(Request) Response
}
// Handler forwards requests and responses between the docker daemon and the plugin.
type Handler struct {
driver Driver
mux *http.ServeMux
}
type actionHandler func(Request) Response
// NewHandler initializes the request handler with a driver implementation.
func NewHandler(driver Driver) *Handler {
h := &Handler{driver, http.NewServeMux()}
h.initMux()
return h
}
func (h *Handler) initMux() {
h.mux.HandleFunc(activatePath, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", defaultContentTypeV1_1)
fmt.Fprintln(w, defaultImplementationManifest)
})
h.handle(createPath, func(req Request) Response {
return h.driver.Create(req)
})
h.handle(remotePath, func(req Request) Response {
return h.driver.Remove(req)
})
h.handle(hostVirtualPath, func(req Request) Response {
return h.driver.Path(req)
})
h.handle(mountPath, func(req Request) Response {
return h.driver.Mount(req)
})
h.handle(unmountPath, func(req Request) Response {
return h.driver.Unmount(req)
})
}
func (h *Handler) handle(name string, actionCall actionHandler) {
h.mux.HandleFunc(name, func(w http.ResponseWriter, r *http.Request) {
req, err := decodeRequest(w, r)
if err != nil {
return
}
res := actionCall(req)
encodeResponse(w, res)
})
}
// ServeTCP makes the handler to listen for request in a given TCP address.
// It also writes the spec file on the right directory for docker to read.
func (h *Handler) ServeTCP(pluginName, addr string) error {
return h.listenAndServe("tcp", addr, pluginName)
}
// ServeUnix makes the handler to listen for requests in a unix socket.
// It also creates the socket file on the right directory for docker to read.
func (h *Handler) ServeUnix(systemGroup, addr string) error {
return h.listenAndServe("unix", addr, systemGroup)
}
func (h *Handler) listenAndServe(proto, addr, group string) error {
var (
start = make(chan struct{})
l net.Listener
err error
spec string
)
server := http.Server{
Addr: addr,
Handler: h.mux,
}
switch proto {
case "tcp":
l, spec, err = newTCPListener(group, addr, start)
case "unix":
l, spec, err = newUnixListener(addr, group, start)
}
if spec != "" {
defer os.Remove(spec)
}
if err != nil {
return err
}
close(start)
return server.Serve(l)
}
func decodeRequest(w http.ResponseWriter, r *http.Request) (req Request, err error) {
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
return
}
func encodeResponse(w http.ResponseWriter, res Response) {
w.Header().Set("Content-Type", defaultContentTypeV1_1)
if res.Err != "" {
w.WriteHeader(http.StatusInternalServerError)
}
json.NewEncoder(w).Encode(res)
}