diff --git a/api/handlers.go b/api/handlers.go index be60f7f4bd..0162224c5a 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -67,8 +67,8 @@ func getImage(c *context, w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] for _, image := range c.cluster.Images() { - if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name) || - len(strings.SplitN(name, ":", 2)) == 1 && image.MatchWithoutTag(name) { + if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name, true) || + len(strings.SplitN(name, ":", 2)) == 1 && image.Match(name, false) { proxy(c.tlsConfig, image.Engine.Addr, w, r) return } @@ -97,8 +97,8 @@ func getImages(c *context, w http.ResponseWriter, r *http.Request) { // Count how many images we need it has. for _, name := range names { for _, image := range images { - if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name) || - len(strings.SplitN(name, ":", 2)) == 1 && image.MatchWithoutTag(name) { + if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name, true) || + len(strings.SplitN(name, ":", 2)) == 1 && image.Match(name, false) { matchedImages = matchedImages + 1 break } diff --git a/cluster/engine.go b/cluster/engine.go index 3036c7d706..bb85eba9be 100644 --- a/cluster/engine.go +++ b/cluster/engine.go @@ -502,7 +502,7 @@ func (e *Engine) Image(IDOrName string) *Image { defer e.RUnlock() for _, image := range e.images { - if image.Match(IDOrName) { + if image.Match(IDOrName, true) { return image } } diff --git a/cluster/image.go b/cluster/image.go index bf83bc5026..fe1f1ffd35 100644 --- a/cluster/image.go +++ b/cluster/image.go @@ -14,38 +14,27 @@ type Image struct { } // Match is exported -func (image *Image) Match(IDOrName string) bool { +func (image *Image) Match(IDOrName string, matchTag bool) bool { size := len(IDOrName) if image.Id == IDOrName || (size > 2 && strings.HasPrefix(image.Id, IDOrName)) { return true } - if len(strings.SplitN(IDOrName, ":", 2)) == 1 { - IDOrName = IDOrName + ":latest" + name := IDOrName + if matchTag { + if len(strings.SplitN(IDOrName, ":", 2)) == 1 { + name = IDOrName + ":latest" + } + } else { + name = strings.SplitN(IDOrName, ":", 2)[0] } for _, repoTag := range image.RepoTags { - if repoTag == IDOrName { - return true - } - } - return false -} - -// MatchWithoutTag is exported -func (image *Image) MatchWithoutTag(IDOrName string) bool { - size := len(IDOrName) - - if image.Id == IDOrName || (size > 2 && strings.HasPrefix(image.Id, IDOrName)) { - return true - } - - name := strings.SplitN(IDOrName, ":", 2)[0] - - for _, repoTag := range image.RepoTags { - repoName := strings.SplitN(repoTag, ":", 2)[0] - if repoName == name { + if matchTag == false { + repoTag = strings.SplitN(repoTag, ":", 2)[0] + } + if repoTag == name { return true } } diff --git a/cluster/image_test.go b/cluster/image_test.go index af9c80bbe6..39ab1f89b8 100644 --- a/cluster/image_test.go +++ b/cluster/image_test.go @@ -12,30 +12,23 @@ func TestMatch(t *testing.T) { img.Id = "378954456789" img.RepoTags = []string{"name:latest"} - assert.True(t, img.Match("378954456789")) - assert.True(t, img.Match("3789")) - assert.True(t, img.Match("378")) - assert.False(t, img.Match("37")) + assert.True(t, img.Match("378954456789", true)) + assert.True(t, img.Match("3789", true)) + assert.True(t, img.Match("378", true)) + assert.False(t, img.Match("37", true)) - assert.True(t, img.Match("name:latest")) - assert.True(t, img.Match("name")) - assert.False(t, img.Match("nam")) - assert.False(t, img.Match("na")) -} - -func TestMatchWithoutTag(t *testing.T) { - img := Image{} - - img.Id = "378954456789" - img.RepoTags = []string{"name:latest"} - - assert.True(t, img.MatchWithoutTag("378954456789")) - assert.True(t, img.MatchWithoutTag("3789")) - assert.True(t, img.MatchWithoutTag("378")) - assert.False(t, img.MatchWithoutTag("37")) - - assert.True(t, img.MatchWithoutTag("name:latest")) - assert.True(t, img.MatchWithoutTag("name")) - assert.False(t, img.MatchWithoutTag("nam")) - assert.False(t, img.MatchWithoutTag("na")) + assert.True(t, img.Match("name:latest", true)) + assert.True(t, img.Match("name", true)) + assert.False(t, img.Match("nam", true)) + assert.False(t, img.Match("na", true)) + + assert.True(t, img.Match("378954456789", false)) + assert.True(t, img.Match("3789", false)) + assert.True(t, img.Match("378", false)) + assert.False(t, img.Match("37", false)) + + assert.True(t, img.Match("name:latest", false)) + assert.True(t, img.Match("name", false)) + assert.False(t, img.Match("nam", false)) + assert.False(t, img.Match("na", false)) } diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 42e7617155..030433fd1b 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -252,7 +252,7 @@ func (c *Cluster) RemoveImages(name string) ([]*dockerclient.ImageDelete, error) var err error for _, n := range c.engines { for _, image := range n.Images() { - if image.Match(name) { + if image.Match(name, true) { content, err := image.Engine.RemoveImage(image, name) if err != nil { errs = append(errs, fmt.Sprintf("%s: %s", image.Engine.Name, err.Error()))