fix images affinity

Signed-off-by: 易嘉 <changhai.ych@alibaba-inc.com>
This commit is contained in:
易嘉 2016-01-24 23:46:09 +08:00
parent cc4eea83da
commit 686f743856
3 changed files with 49 additions and 1 deletions

View File

@ -50,7 +50,8 @@ func (f *AffinityFilter) Filter(config *cluster.ContainerConfig, nodes []*node.N
images = append(images, image.Id) images = append(images, image.Id)
images = append(images, image.RepoTags...) images = append(images, image.RepoTags...)
for _, tag := range image.RepoTags { for _, tag := range image.RepoTags {
images = append(images, strings.Split(tag, ":")[0]) repo, _ := parseRepositoryTag(tag)
images = append(images, repo)
} }
} }
if affinity.Match(images...) { if affinity.Match(images...) {

View File

@ -0,0 +1,23 @@
package filter
import "strings"
// Get a repos name and returns the right reposName + tag|digest
// The tag can be confusing because of a port in a repository name.
// Ex: localhost.localdomain:5000/samalba/hipache:latest
// Digest ex: localhost:5000/foo/bar@sha256:bc8813ea7b3603864987522f02a76101c17ad122e1c46d790efc0fca78ca7bfb
func parseRepositoryTag(repos string) (string, string) {
n := strings.Index(repos, "@")
if n >= 0 {
parts := strings.Split(repos, "@")
return parts[0], parts[1]
}
n = strings.LastIndex(repos, ":")
if n < 0 {
return repos, ""
}
if tag := repos[n+1:]; !strings.Contains(tag, "/") {
return repos[:n], tag
}
return repos, ""
}

View File

@ -0,0 +1,24 @@
package filter
import "testing"
func TestParseRepositoryTag(t *testing.T) {
repo, tag := parseRepositoryTag("localhost.localdomain:5000/samalba/hipache:latest")
if tag != "latest" {
t.Errorf("repo=%s tag=%s", repo, tag)
}
repo, tag = parseRepositoryTag("localhost:5000/foo/bar@sha256:bc8813ea7b3603864987522f02a76101c17ad122e1c46d790efc0fca78ca7bfb")
if tag != "sha256:bc8813ea7b3603864987522f02a76101c17ad122e1c46d790efc0fca78ca7bfb" {
t.Logf("repo=%s tag=%s", repo, tag)
}
repo, tag = parseRepositoryTag("localhost:5000/foo/bar")
if tag != "" {
t.Logf("repo=%s tag=%s", repo, tag)
}
repo, tag = parseRepositoryTag("localhost:5000/foo/bar:latest")
t.Logf("repo=%s tag=%s", repo, tag)
if tag != "latest" {
t.Logf("repo=%s tag=%s", repo, tag)
}
}