mirror of https://github.com/docker/docs.git
merge two image match func
Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
parent
064e91cd23
commit
e6ce59794e
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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()))
|
||||
|
|
Loading…
Reference in New Issue