Skip to content

Commit

Permalink
Add support for volume capabilities endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed Jun 24, 2016
1 parent b6b7744 commit 7f7e1f8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
36 changes: 24 additions & 12 deletions volume/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@ const (
// DefaultDockerRootDirectory is the default directory where volumes will be created.
DefaultDockerRootDirectory = "/var/lib/docker-volumes"

manifest = `{"Implements": ["VolumeDriver"]}`
createPath = "/VolumeDriver.Create"
getPath = "/VolumeDriver.Get"
listPath = "/VolumeDriver.List"
removePath = "/VolumeDriver.Remove"
hostVirtualPath = "/VolumeDriver.Path"
mountPath = "/VolumeDriver.Mount"
unmountPath = "/VolumeDriver.Unmount"
manifest = `{"Implements": ["VolumeDriver"]}`
createPath = "/VolumeDriver.Create"
getPath = "/VolumeDriver.Get"
listPath = "/VolumeDriver.List"
removePath = "/VolumeDriver.Remove"
hostVirtualPath = "/VolumeDriver.Path"
mountPath = "/VolumeDriver.Mount"
unmountPath = "/VolumeDriver.Unmount"
capabilitiesPath = "/VolumeDriver.Capabilities"
)

// Request is the structure that docker's requests are deserialized to.
type Request struct {
Name string
Options map[string]string `json:"Opts,omitempty"`
MountID string `json:",omitempty"`
}

// Response is the strucutre that the plugin's responses are serialized to.
type Response struct {
Mountpoint string
Err string
Volumes []*Volume
Volume *Volume
Mountpoint string
Err string
Volumes []*Volume
Volume *Volume
Capabilities Capability
}

// Volume represents a volume object for use with `Get` and `List` requests
Expand All @@ -40,6 +43,11 @@ type Volume struct {
Mountpoint string
}

// Capability represents the list of capabilities a volume driver can return
type Capability struct {
Scope string
}

// Driver represent the interface a driver must fulfill.
type Driver interface {
Create(Request) Response
Expand All @@ -49,6 +57,7 @@ type Driver interface {
Path(Request) Response
Mount(Request) Response
Unmount(Request) Response
Capabilities(Request) Response
}

// Handler forwards requests and responses between the docker daemon and the plugin.
Expand Down Expand Up @@ -94,6 +103,9 @@ func (h *Handler) initMux() {
h.handle(unmountPath, func(req Request) Response {
return h.driver.Unmount(req)
})
h.handle(capabilitiesPath, func(req Request) Response {
return h.driver.Capabilities(req)
})
}

func (h *Handler) handle(name string, actionCall actionHandler) {
Expand Down
34 changes: 26 additions & 8 deletions volume/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ func TestHandler(t *testing.T) {
if p.remove != 1 {
t.Fatalf("expected remove 1, got %d", p.remove)
}

// Capabilities
resp, err = pluginRequest(client, capabilitiesPath, Request{})
if err != nil {
t.Fatal(err)
}
if resp.Err != "" {
t.Fatalf("got error removing volume: %s", resp.Err)
}
if p.capabilities != 1 {
t.Fatalf("expected remove 1, got %d", p.capabilities)
}
}

func pluginRequest(client *http.Client, method string, req Request) (*Response, error) {
Expand All @@ -121,14 +133,15 @@ func pluginRequest(client *http.Client, method string, req Request) (*Response,
}

type testPlugin struct {
volumes []string
create int
get int
list int
path int
mount int
unmount int
remove int
volumes []string
create int
get int
list int
path int
mount int
unmount int
remove int
capabilities int
}

func (p *testPlugin) Create(req Request) Response {
Expand Down Expand Up @@ -196,3 +209,8 @@ func (p *testPlugin) Unmount(req Request) Response {
}
return Response{Err: "no such volume"}
}

func (p *testPlugin) Capabilities(req Request) Response {
p.capabilities++
return Response{Capabilities: Capability{Scope: "local"}}
}
14 changes: 10 additions & 4 deletions volume/shim/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func (d *shimDriver) List(req volumeplugin.Request) volumeplugin.Response {
}
vols := make([]*volumeplugin.Volume, len(ls))

for _, v := range ls {
for i, v := range ls {
vol := &volumeplugin.Volume{
Name: v.Name(),
Mountpoint: v.Path(),
}
vols = append(vols, vol)
vols[i] = vol
}
res.Volumes = vols
return res
Expand Down Expand Up @@ -92,7 +92,7 @@ func (d *shimDriver) Mount(req volumeplugin.Request) volumeplugin.Response {
res.Err = err.Error()
return res
}
pth, err := v.Mount()
pth, err := v.Mount(req.MountID)
if err != nil {
res.Err = err.Error()
}
Expand All @@ -107,8 +107,14 @@ func (d *shimDriver) Unmount(req volumeplugin.Request) volumeplugin.Response {
res.Err = err.Error()
return res
}
if err := v.Unmount(); err != nil {
if err := v.Unmount(req.MountID); err != nil {
res.Err = err.Error()
}
return res
}

func (d *shimDriver) Capabilities(req volumeplugin.Request) volumeplugin.Response {
var res volumeplugin.Response
res.Capabilities = volumeplugin.Capability{Scope: d.d.Scope()}
return res
}
1 change: 1 addition & 0 deletions volume/shim/shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (testVolumeDriver) Create(string, map[string]string) (volume.Volume, error)
func (testVolumeDriver) Remove(volume.Volume) error { return nil }
func (testVolumeDriver) List() ([]volume.Volume, error) { return nil, nil }
func (testVolumeDriver) Get(name string) (volume.Volume, error) { return nil, nil }
func (testVolumeDriver) Scope() string { return "local" }

func TestVolumeDriver(t *testing.T) {
h := NewHandlerFromVolumeDriver(testVolumeDriver{})
Expand Down

0 comments on commit 7f7e1f8

Please sign in to comment.