merge two image match func

Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
Xian Chaobo 2015-05-19 21:39:10 -04:00
parent 064e91cd23
commit e6ce59794e
5 changed files with 36 additions and 54 deletions

View File

@ -67,8 +67,8 @@ func getImage(c *context, w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"] name := mux.Vars(r)["name"]
for _, image := range c.cluster.Images() { for _, image := range c.cluster.Images() {
if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name) || if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name, true) ||
len(strings.SplitN(name, ":", 2)) == 1 && image.MatchWithoutTag(name) { len(strings.SplitN(name, ":", 2)) == 1 && image.Match(name, false) {
proxy(c.tlsConfig, image.Engine.Addr, w, r) proxy(c.tlsConfig, image.Engine.Addr, w, r)
return return
} }
@ -97,8 +97,8 @@ func getImages(c *context, w http.ResponseWriter, r *http.Request) {
// Count how many images we need it has. // Count how many images we need it has.
for _, name := range names { for _, name := range names {
for _, image := range images { for _, image := range images {
if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name) || if len(strings.SplitN(name, ":", 2)) == 2 && image.Match(name, true) ||
len(strings.SplitN(name, ":", 2)) == 1 && image.MatchWithoutTag(name) { len(strings.SplitN(name, ":", 2)) == 1 && image.Match(name, false) {
matchedImages = matchedImages + 1 matchedImages = matchedImages + 1
break break
} }

View File

@ -502,7 +502,7 @@ func (e *Engine) Image(IDOrName string) *Image {
defer e.RUnlock() defer e.RUnlock()
for _, image := range e.images { for _, image := range e.images {
if image.Match(IDOrName) { if image.Match(IDOrName, true) {
return image return image
} }
} }

View File

@ -14,38 +14,27 @@ type Image struct {
} }
// Match is exported // Match is exported
func (image *Image) Match(IDOrName string) bool { func (image *Image) Match(IDOrName string, matchTag bool) bool {
size := len(IDOrName) size := len(IDOrName)
if image.Id == IDOrName || (size > 2 && strings.HasPrefix(image.Id, IDOrName)) { if image.Id == IDOrName || (size > 2 && strings.HasPrefix(image.Id, IDOrName)) {
return true return true
} }
if len(strings.SplitN(IDOrName, ":", 2)) == 1 { name := IDOrName
IDOrName = IDOrName + ":latest" if matchTag {
if len(strings.SplitN(IDOrName, ":", 2)) == 1 {
name = IDOrName + ":latest"
}
} else {
name = strings.SplitN(IDOrName, ":", 2)[0]
} }
for _, repoTag := range image.RepoTags { for _, repoTag := range image.RepoTags {
if repoTag == IDOrName { if matchTag == false {
return true repoTag = strings.SplitN(repoTag, ":", 2)[0]
} }
} if repoTag == name {
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 {
return true return true
} }
} }

View File

@ -12,30 +12,23 @@ func TestMatch(t *testing.T) {
img.Id = "378954456789" img.Id = "378954456789"
img.RepoTags = []string{"name:latest"} img.RepoTags = []string{"name:latest"}
assert.True(t, img.Match("378954456789")) assert.True(t, img.Match("378954456789", true))
assert.True(t, img.Match("3789")) assert.True(t, img.Match("3789", true))
assert.True(t, img.Match("378")) assert.True(t, img.Match("378", true))
assert.False(t, img.Match("37")) assert.False(t, img.Match("37", true))
assert.True(t, img.Match("name:latest")) assert.True(t, img.Match("name:latest", true))
assert.True(t, img.Match("name")) assert.True(t, img.Match("name", true))
assert.False(t, img.Match("nam")) assert.False(t, img.Match("nam", true))
assert.False(t, img.Match("na")) assert.False(t, img.Match("na", true))
}
assert.True(t, img.Match("378954456789", false))
func TestMatchWithoutTag(t *testing.T) { assert.True(t, img.Match("3789", false))
img := Image{} assert.True(t, img.Match("378", false))
assert.False(t, img.Match("37", false))
img.Id = "378954456789"
img.RepoTags = []string{"name:latest"} assert.True(t, img.Match("name:latest", false))
assert.True(t, img.Match("name", false))
assert.True(t, img.MatchWithoutTag("378954456789")) assert.False(t, img.Match("nam", false))
assert.True(t, img.MatchWithoutTag("3789")) assert.False(t, img.Match("na", false))
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"))
} }

View File

@ -252,7 +252,7 @@ func (c *Cluster) RemoveImages(name string) ([]*dockerclient.ImageDelete, error)
var err error var err error
for _, n := range c.engines { for _, n := range c.engines {
for _, image := range n.Images() { for _, image := range n.Images() {
if image.Match(name) { if image.Match(name, true) {
content, err := image.Engine.RemoveImage(image, name) content, err := image.Engine.RemoveImage(image, name)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("%s: %s", image.Engine.Name, err.Error())) errs = append(errs, fmt.Sprintf("%s: %s", image.Engine.Name, err.Error()))