do not leak dockerclient

Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Victor Vieux 2015-03-18 13:46:48 -07:00
parent af05bfd0ba
commit e6b3b04150
8 changed files with 26 additions and 26 deletions

View File

@ -353,7 +353,7 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
out := []*dockerclient.ImageDelete{} out := []*dockerclient.ImageDelete{}
errs := []string{} errs := []string{}
for _, image := range matchedImages { for _, image := range matchedImages {
content, err := image.Node.DockerClient().RemoveImage(name) content, err := c.cluster.RemoveImage(image)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("%s: %s", image.Node.Name(), err.Error())) errs = append(errs, fmt.Sprintf("%s: %s", image.Node.Name(), err.Error()))
continue continue

View File

@ -5,7 +5,6 @@ import (
"testing" "testing"
"github.com/docker/swarm/cluster" "github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -20,7 +19,6 @@ func (fw *FakeWriter) Write(p []byte) (n int, err error) {
type FakeNode struct{} type FakeNode struct{}
func (fn *FakeNode) DockerClient() dockerclient.Client { return nil }
func (fn *FakeNode) ID() string { return "node_id" } func (fn *FakeNode) ID() string { return "node_id" }
func (fn *FakeNode) Name() string { return "node_name" } func (fn *FakeNode) Name() string { return "node_name" }
func (fn *FakeNode) IP() string { return "node_ip" } func (fn *FakeNode) IP() string { return "node_ip" }

View File

@ -15,6 +15,9 @@ type Cluster interface {
// Return one image matching `IdOrName` // Return one image matching `IdOrName`
Image(IdOrName string) *Image Image(IdOrName string) *Image
// Remove an image from the cluster
RemoveImage(image *Image) ([]*dockerclient.ImageDelete, error)
// Return all containers // Return all containers
Containers() []*Container Containers() []*Container

View File

@ -1,14 +1,8 @@
package cluster package cluster
import ( import "fmt"
"fmt"
"github.com/samalba/dockerclient"
)
type Node interface { type Node interface {
DockerClient() dockerclient.Client
ID() string ID() string
Name() string Name() string

View File

@ -186,6 +186,16 @@ func (c *Cluster) Image(IdOrName string) *cluster.Image {
return nil return nil
} }
// RemoveImage removes an image from the cluster
func (c *Cluster) RemoveImage(image *cluster.Image) ([]*dockerclient.ImageDelete, error) {
c.Lock()
defer c.Unlock()
if n, ok := image.Node.(*node); ok {
return n.removeImage(image)
}
return nil, nil
}
func (c *Cluster) Pull(name string, callback func(what, status string)) { func (c *Cluster) Pull(name string, callback func(what, status string)) {
size := len(c.nodes) size := len(c.nodes)
done := make(chan bool, size) done := make(chan bool, size)

View File

@ -54,10 +54,6 @@ type node struct {
overcommitRatio int64 overcommitRatio int64
} }
func (n *node) DockerClient() dockerclient.Client {
return n.client
}
func (n *node) ID() string { func (n *node) ID() string {
return n.id return n.id
} }
@ -167,6 +163,11 @@ func (n *node) updateSpecs() error {
return nil return nil
} }
// Delete an image from the node.
func (n *node) removeImage(image *cluster.Image) ([]*dockerclient.ImageDelete, error) {
return n.client.RemoveImage(image.Id)
}
// Refresh the list of images on the node. // Refresh the list of images on the node.
func (n *node) refreshImages() error { func (n *node) refreshImages() error {
images, err := n.client.ListImages() images, err := n.client.ListImages()

View File

@ -1,9 +1,6 @@
package filter package filter
import ( import "github.com/docker/swarm/cluster"
"github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
)
type FakeNode struct { type FakeNode struct {
id string id string
@ -14,7 +11,6 @@ type FakeNode struct {
labels map[string]string labels map[string]string
} }
func (fn *FakeNode) DockerClient() dockerclient.Client { return nil }
func (fn *FakeNode) ID() string { return fn.id } func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name } func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" } func (fn *FakeNode) IP() string { return "" }

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"github.com/docker/swarm/cluster" "github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
) )
type FakeNode struct { type FakeNode struct {
@ -18,7 +17,6 @@ type FakeNode struct {
containers []*cluster.Container containers []*cluster.Container
} }
func (fn *FakeNode) DockerClient() dockerclient.Client { return nil }
func (fn *FakeNode) ID() string { return fn.id } func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name } func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" } func (fn *FakeNode) IP() string { return "" }