Add match in images.go

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-03-10 16:39:22 -07:00 committed by Victor Vieux
parent 649b6cf577
commit b4a88ad622
10 changed files with 52 additions and 45 deletions

View File

@ -90,7 +90,7 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
accepteds, _ := filters["node"]
images := []*cluster.Image{}
for _, image := range c.cluster.Images("") {
for _, image := range c.cluster.Images() {
if len(accepteds) != 0 {
found := false
for _, accepted := range accepteds {
@ -342,14 +342,23 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
client, scheme := newClientAndScheme(c.tlsConfig)
defer closeIdleConnections(client)
images := c.cluster.Images(name)
size := len(images)
matchedImages := []*cluster.Image{}
for _, image := range c.cluster.Images() {
if image.Match(name) {
fmt.Println("matched", image)
matchedImages = append(matchedImages, image)
}
}
size := len(matchedImages)
if size == 0 {
httpError(w, fmt.Sprintf("No such image %s", name), http.StatusNotFound)
return
}
wf := NewWriteFlusher(w)
for i, image := range images {
for i, image := range matchedImages {
fmt.Println("delete", image)
req, err := http.NewRequest("DELETE", scheme+"://"+image.Node.Addr()+"/images/"+name, nil)
if err != nil {
if size == 1 {
@ -364,7 +373,6 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
}
continue
}
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
@ -387,10 +395,10 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
sdata = strings.Replace(sdata, "[", ",", -1)
}
if i != len(images)-1 {
if i != size-1 {
sdata = strings.Replace(sdata, "]", "", -1)
}
fmt.Fprintf(w, sdata)
fmt.Fprintf(wf, sdata)
}
}

View File

@ -23,7 +23,7 @@ func (fn *FakeNode) ID() string { return "node_id" }
func (fn *FakeNode) Name() string { return "node_name" }
func (fn *FakeNode) IP() string { return "node_ip" }
func (fn *FakeNode) Addr() string { return "node_addr" }
func (fn *FakeNode) Images(_ string) []*cluster.Image { return nil }
func (fn *FakeNode) Images() []*cluster.Image { return nil }
func (fn *FakeNode) Image(_ string) *cluster.Image { return nil }
func (fn *FakeNode) Containers() []*cluster.Container { return nil }
func (fn *FakeNode) Container(_ string) *cluster.Container { return nil }

View File

@ -9,9 +9,8 @@ type Cluster interface {
// Remove a container
RemoveContainer(container *Container, force bool) error
// Return all images matching `name`
// if `name` == "" return all images in the cluster
Images(name string) []*Image
// Return all images
Images() []*Image
// Return one image matching `IdOrName`
Image(IdOrName string) *Image

View File

@ -1,9 +1,27 @@
package cluster
import "github.com/samalba/dockerclient"
import (
"strings"
"github.com/samalba/dockerclient"
)
type Image struct {
dockerclient.Image
Node Node
}
func (image *Image) Match(IdOrName string) bool {
size := len(IdOrName)
if image.Id == IdOrName || (size > 2 && strings.HasPrefix(image.Id, IdOrName)) {
return true
}
for _, repoTag := range image.RepoTags {
if repoTag == IdOrName || (size > 2 && strings.HasPrefix(repoTag, IdOrName)) {
return true
}
}
return false
}

View File

@ -9,7 +9,7 @@ type Node interface {
IP() string //to inject the actual IP of the machine in docker ps (hostname:port or ip:port)
Addr() string //to know where to connect with the proxy
Images(name string) []*Image //used by the API
Images() []*Image //used by the API
Image(IdOrName string) *Image //used by the filters
Containers() []*Container //used by the filters
Container(IdOrName string) *Container //used by the filters

View File

@ -155,15 +155,14 @@ func (c *Cluster) getNode(addr string) *node {
return nil
}
// Containers returns all the images in the cluster.
// If `name` is empty, return all the images
func (c *Cluster) Images(name string) []*cluster.Image {
// Images returns all the images in the cluster.
func (c *Cluster) Images() []*cluster.Image {
c.RLock()
defer c.RUnlock()
out := []*cluster.Image{}
for _, n := range c.nodes {
out = append(out, n.Images(name)...)
out = append(out, n.Images()...)
}
return out

View File

@ -459,30 +459,13 @@ func (n *node) Container(IdOrName string) *cluster.Container {
return nil
}
func matchImage(image *cluster.Image, IdOrName string) bool {
size := len(IdOrName)
if image.Id == IdOrName || (size > 2 && strings.HasPrefix(image.Id, IdOrName)) {
return true
}
for _, repoTag := range image.RepoTags {
if repoTag == IdOrName || (size > 2 && strings.HasPrefix(repoTag, IdOrName)) {
return true
}
}
return false
}
// Images returns a list for images matching name in the node
// If `name` is empty, returns all the images
func (n *node) Images(name string) []*cluster.Image {
// Images returns all the images in the node
func (n *node) Images() []*cluster.Image {
images := []*cluster.Image{}
n.RLock()
for _, image := range n.images {
if name == "" || matchImage(image, name) {
images = append(images, image)
}
images = append(images, image)
}
n.RUnlock()
return images
@ -494,7 +477,7 @@ func (n *node) Image(IdOrName string) *cluster.Image {
defer n.RUnlock()
for _, image := range n.images {
if matchImage(image, IdOrName) {
if image.Match(IdOrName) {
return image
}
}

View File

@ -35,7 +35,7 @@ func (f *AffinityFilter) Filter(config *dockerclient.ContainerConfig, nodes []cl
}
case "image":
images := []string{}
for _, image := range node.Images("") {
for _, image := range node.Images() {
images = append(images, image.Id)
images = append(images, image.RepoTags...)
for _, tag := range image.RepoTags {

View File

@ -11,11 +11,11 @@ type FakeNode struct {
labels map[string]string
}
func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" }
func (fn *FakeNode) Addr() string { return fn.addr }
func (fn *FakeNode) Images(_ string) []*cluster.Image { return fn.images }
func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" }
func (fn *FakeNode) Addr() string { return fn.addr }
func (fn *FakeNode) Images() []*cluster.Image { return fn.images }
func (fn *FakeNode) Image(id string) *cluster.Image {
for _, image := range fn.images {
if image.Id == id {

View File

@ -21,7 +21,7 @@ func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" }
func (fn *FakeNode) Addr() string { return fn.addr }
func (fn *FakeNode) Images(_ string) []*cluster.Image { return nil }
func (fn *FakeNode) Images() []*cluster.Image { return nil }
func (fn *FakeNode) Image(_ string) *cluster.Image { return nil }
func (fn *FakeNode) Containers() []*cluster.Container { return fn.containers }
func (fn *FakeNode) Container(_ string) *cluster.Container { return nil }