Skip to content

Commit 50df7df

Browse files
committed
Merge pull request docker-archive#1196 from vieux/initial_volume_support
add support for docker volume ls & inspect
2 parents e7d4f88 + 18e8e95 commit 50df7df

File tree

16 files changed

+204
-1
lines changed

16 files changed

+204
-1
lines changed

Godeps/Godeps.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/samalba/dockerclient/dockerclient.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/samalba/dockerclient/interface.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/samalba/dockerclient/mockclient/mock.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/samalba/dockerclient/nopclient/nop.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/samalba/dockerclient/types.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/handlers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
168168
json.NewEncoder(w).Encode(images)
169169
}
170170

171+
// GET /volumes
172+
func getVolumes(c *context, w http.ResponseWriter, r *http.Request) {
173+
volumes := struct {
174+
Volumes []*cluster.Volume
175+
}{c.cluster.Volumes()}
176+
177+
w.Header().Set("Content-Type", "application/json")
178+
json.NewEncoder(w).Encode(volumes)
179+
}
180+
171181
// GET /containers/ps
172182
// GET /containers/json
173183
func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
@@ -555,6 +565,16 @@ func ping(c *context, w http.ResponseWriter, r *http.Request) {
555565
w.Write([]byte{'O', 'K'})
556566
}
557567

568+
// Proxy a request to the right node
569+
func proxyVolume(c *context, w http.ResponseWriter, r *http.Request) {
570+
var name = mux.Vars(r)["volumename"]
571+
if volume := c.cluster.Volume(name); volume != nil {
572+
proxy(c.tlsConfig, volume.Engine.Addr, w, r)
573+
return
574+
}
575+
httpError(w, fmt.Sprintf("No such volume: %s", name), http.StatusNotFound)
576+
}
577+
558578
// Proxy a request to the right node
559579
func proxyContainer(c *context, w http.ResponseWriter, r *http.Request) {
560580
name, container, err := getContainerFromVars(c, mux.Vars(r))

api/primary.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ var routes = map[string]map[string]handler{
4747
"/containers/{name:.*}/stats": proxyContainer,
4848
"/containers/{name:.*}/attach/ws": proxyHijack,
4949
"/exec/{execid:.*}/json": proxyContainer,
50+
"/volumes": getVolumes,
51+
"/volumes/{volumename:.*}": proxyVolume,
5052
},
5153
"POST": {
5254
"/auth": proxyRandom,

cluster/cluster.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ type Cluster interface {
3131
// cluster.Containers().Get(IDOrName)
3232
Container(IDOrName string) *Container
3333

34+
// Return all volumes
35+
Volumes() []*Volume
36+
37+
// Return one volume from the cluster
38+
Volume(name string) *Volume
39+
3440
// Pull images
3541
// `callback` can be called multiple time
3642
// `where` is where it is being pulled

cluster/engine.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Engine struct {
5757
stopCh chan struct{}
5858
containers map[string]*Container
5959
images []*Image
60+
volumes []*Volume
6061
client dockerclient.Client
6162
eventHandler EventHandler
6263
healthy bool
@@ -103,6 +104,9 @@ func (e *Engine) ConnectWithClient(client dockerclient.Client) error {
103104
return err
104105
}
105106

107+
// Do not check error as older daemon don't support this call
108+
e.RefreshVolumes()
109+
106110
// Start the update loop.
107111
go e.refreshLoop()
108112

@@ -198,6 +202,21 @@ func (e *Engine) RefreshImages() error {
198202
return nil
199203
}
200204

205+
// RefreshVolumes refreshes the list of volumes on the engine.
206+
func (e *Engine) RefreshVolumes() error {
207+
volumes, err := e.client.ListVolumes()
208+
if err != nil {
209+
return err
210+
}
211+
e.Lock()
212+
e.volumes = nil
213+
for _, volume := range volumes {
214+
e.volumes = append(e.volumes, &Volume{Volume: *volume, Engine: e})
215+
}
216+
e.Unlock()
217+
return nil
218+
}
219+
201220
// RefreshContainers will refresh the list and status of containers running on the engine. If `full` is
202221
// true, each container will be inspected.
203222
// FIXME: unexport this method after mesos scheduler stops using it directly
@@ -312,6 +331,8 @@ func (e *Engine) refreshLoop() {
312331

313332
err = e.RefreshContainers(false)
314333
if err == nil {
334+
// Do not check error as older daemon don't support this call
335+
e.RefreshVolumes()
315336
err = e.RefreshImages()
316337
}
317338

@@ -521,6 +542,18 @@ func (e *Engine) Images(all bool) []*Image {
521542
return images
522543
}
523544

545+
// Volumes returns all the volumes in the engine
546+
func (e *Engine) Volumes() []*Volume {
547+
e.RLock()
548+
549+
volumes := make([]*Volume, 0, len(e.volumes))
550+
for _, volume := range e.volumes {
551+
volumes = append(volumes, volume)
552+
}
553+
e.RUnlock()
554+
return volumes
555+
}
556+
524557
// Image returns the image with IDOrName in the engine
525558
func (e *Engine) Image(IDOrName string) *Image {
526559
e.RLock()
@@ -549,9 +582,11 @@ func (e *Engine) handler(ev *dockerclient.Event, _ chan error, args ...interface
549582
// If the container state changes, we have to do an inspect in
550583
// order to update container.Info and get the new NetworkSettings.
551584
e.refreshContainer(ev.Id, true)
585+
e.RefreshVolumes()
552586
default:
553587
// Otherwise, do a "soft" refresh of the container.
554588
e.refreshContainer(ev.Id, false)
589+
e.RefreshVolumes()
555590
}
556591

557592
// If there is no event handler registered, abort right now.

0 commit comments

Comments
 (0)