From 686f74385638d826b7b80d9f6a02b86f318fa0a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E5=98=89?= Date: Sun, 24 Jan 2016 23:46:09 +0800 Subject: [PATCH 1/3] fix images affinity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 易嘉 --- scheduler/filter/affinity.go | 3 ++- scheduler/filter/wrapper.go | 23 +++++++++++++++++++++++ scheduler/filter/wrapper_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 scheduler/filter/wrapper.go create mode 100644 scheduler/filter/wrapper_test.go diff --git a/scheduler/filter/affinity.go b/scheduler/filter/affinity.go index 342984bfb5..70ca420102 100644 --- a/scheduler/filter/affinity.go +++ b/scheduler/filter/affinity.go @@ -50,7 +50,8 @@ func (f *AffinityFilter) Filter(config *cluster.ContainerConfig, nodes []*node.N images = append(images, image.Id) images = append(images, 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...) { diff --git a/scheduler/filter/wrapper.go b/scheduler/filter/wrapper.go new file mode 100644 index 0000000000..e3a9b6605d --- /dev/null +++ b/scheduler/filter/wrapper.go @@ -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, "" +} \ No newline at end of file diff --git a/scheduler/filter/wrapper_test.go b/scheduler/filter/wrapper_test.go new file mode 100644 index 0000000000..c71c9cea6e --- /dev/null +++ b/scheduler/filter/wrapper_test.go @@ -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) + } +} \ No newline at end of file From a59f3578ee5876463e185cf344ddecb47ce9b0b2 Mon Sep 17 00:00:00 2001 From: ChangHai Yan Date: Thu, 28 Jan 2016 19:50:09 +0800 Subject: [PATCH 2/3] use ParseRepositoryTag in image Signed-off-by: ChangHai Yan --- cluster/image_test.go | 21 +++++++++++++++++++++ scheduler/filter/affinity.go | 2 +- scheduler/filter/wrapper.go | 23 ----------------------- scheduler/filter/wrapper_test.go | 24 ------------------------ 4 files changed, 22 insertions(+), 48 deletions(-) delete mode 100644 scheduler/filter/wrapper.go delete mode 100644 scheduler/filter/wrapper_test.go diff --git a/cluster/image_test.go b/cluster/image_test.go index 410fba1203..2648664a4d 100644 --- a/cluster/image_test.go +++ b/cluster/image_test.go @@ -123,3 +123,24 @@ func TestImagesFilterWithNameFilterWithTag(t *testing.T) { }) assert.Equal(t, len(result), 2) } + +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) + } +} diff --git a/scheduler/filter/affinity.go b/scheduler/filter/affinity.go index 70ca420102..6bc458d586 100644 --- a/scheduler/filter/affinity.go +++ b/scheduler/filter/affinity.go @@ -50,7 +50,7 @@ func (f *AffinityFilter) Filter(config *cluster.ContainerConfig, nodes []*node.N images = append(images, image.Id) images = append(images, image.RepoTags...) for _, tag := range image.RepoTags { - repo, _ := parseRepositoryTag(tag) + repo, _ := cluster.ParseRepositoryTag(tag) images = append(images, repo) } } diff --git a/scheduler/filter/wrapper.go b/scheduler/filter/wrapper.go deleted file mode 100644 index e3a9b6605d..0000000000 --- a/scheduler/filter/wrapper.go +++ /dev/null @@ -1,23 +0,0 @@ -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, "" -} \ No newline at end of file diff --git a/scheduler/filter/wrapper_test.go b/scheduler/filter/wrapper_test.go deleted file mode 100644 index c71c9cea6e..0000000000 --- a/scheduler/filter/wrapper_test.go +++ /dev/null @@ -1,24 +0,0 @@ -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) - } -} \ No newline at end of file From 967a98d40fcc37f5db972f913e256590fb8e84e3 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 28 Jan 2016 13:07:25 -0800 Subject: [PATCH 3/3] fix unit test and add integration test Signed-off-by: Victor Vieux --- cluster/image_test.go | 8 ++++---- test/integration/affinities.bats | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cluster/image_test.go b/cluster/image_test.go index 2648664a4d..a07185734c 100644 --- a/cluster/image_test.go +++ b/cluster/image_test.go @@ -126,19 +126,19 @@ func TestImagesFilterWithNameFilterWithTag(t *testing.T) { func TestParseRepositoryTag(t *testing.T) { - repo, tag := parseRepositoryTag("localhost.localdomain:5000/samalba/hipache:latest") + 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") + 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") + 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") + 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) diff --git a/test/integration/affinities.bats b/test/integration/affinities.bats index 055a73c1fe..d647a9d58c 100644 --- a/test/integration/affinities.bats +++ b/test/integration/affinities.bats @@ -68,8 +68,6 @@ function teardown() { [ "$status" -eq 0 ] run docker_swarm inspect c1 - # FIXME: This will help debugging the failing test. - echo $output [ "$status" -eq 0 ] [[ "${output}" == *'"Name": "node-1"'* ]] @@ -86,6 +84,26 @@ function teardown() { [[ "${output}" != *'"Name": "node-1"'* ]] } +@test "images affinity - local registry" { + start_docker_with_busybox 2 + swarm_manage + + # Create a new image just on the second host. + run docker -H ${HOSTS[1]} tag busybox localhost:5000/test + + # pull busybox to force the refresh images + # FIXME: this is slow. + run docker_swarm pull busybox + [ "$status" -eq 0 ] + + run docker_swarm run --name c1 -e affinity:image==localhost:5000/test -d busybox:latest sh + [ "$status" -eq 0 ] + + run docker_swarm inspect c1 + [ "$status" -eq 0 ] + [[ "${output}" == *'"Name": "node-1"'* ]] +} + @test "label affinity" { start_docker_with_busybox 2 swarm_manage