Skip to content

Commit

Permalink
Merge pull request docker-archive#1166 from chanwit/image-dedup
Browse files Browse the repository at this point in the history
Implement grouping list of images by Id
  • Loading branch information
vieux committed Sep 8, 2015
2 parents 2d78f6a + 45e091b commit 0579920
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
29 changes: 26 additions & 3 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
}

accepteds, _ := filters["node"]
images := []*cluster.Image{}

// this struct helps grouping images
// but still keeps their Engine infos as an array.
groupImages := make(map[string]dockerclient.Image)
for _, image := range c.cluster.Images(all) {
if len(accepteds) != 0 {
found := false
Expand All @@ -138,9 +139,31 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
continue
}
}
images = append(images, image)

// grouping images by Id, and concat their RepoTags
if entry, existed := groupImages[image.Id]; existed {
entry.RepoTags = append(entry.RepoTags, image.RepoTags...)
groupImages[image.Id] = entry
} else {
groupImages[image.Id] = image.Image
}
}

images := []dockerclient.Image{}

for _, image := range groupImages {
// de-duplicate RepoTags
result := []string{}
seen := map[string]bool{}
for _, val := range image.RepoTags {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = true
}
}
image.RepoTags = result
images = append(images, image)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
Expand Down
10 changes: 5 additions & 5 deletions test/integration/api/images.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ function teardown() {
start_docker_with_busybox 2
swarm_manage


# we should get 2 busyboxes, plus the header.
# With grouping, we should get 1 busybox, plus the header.
run docker_swarm images
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${#lines[@]}" -eq 2 ]
# Every line should contain "busybox" except for the header
for((i=1; i<${#lines[@]}; i++)); do
[[ "${lines[i]}" == *"busybox"* ]]
done

# Try with --filter.
run docker_swarm images --filter node=node-0
[ "$status" -eq 0 ]
Expand All @@ -40,6 +39,7 @@ function teardown() {
[[ "${lines[1]}" == *"busybox"* ]]

# Try images -a
# lines are: header, busybox, <none>, <none>
run docker_swarm images -a
[ "${#lines[@]}" -ge 5 ]
[ "${#lines[@]}" -ge 4 ]
}
6 changes: 3 additions & 3 deletions test/integration/api/pull.bats
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ function teardown() {

docker_swarm pull busybox

# we should get 2 busyboxes, plus the header.
# with grouping, we should get 1 busybox, plus the header.
run docker_swarm images
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
# every line should contain "busybox" exclude the first head line
[ "${#lines[@]}" -eq 2 ]
# every line should contain "busybox" exclude the first head line
for((i=1; i<${#lines[@]}; i++)); do
[[ "${lines[i]}" == *"busybox"* ]]
done
Expand Down
8 changes: 6 additions & 2 deletions test/integration/api/tag.bats
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function teardown() {
docker_swarm tag busybox tag_busybox:test

# verify
run docker_swarm images
[[ $(echo ${output} | grep -o "tag_busybox" | wc -l) == 2 ]]
# change the way to verify tagged image on each node after image deduplication
run docker_swarm images --filter node=node-0
[[ $(echo ${output} | grep -o "tag_busybox" | wc -l) == 1 ]]

run docker_swarm images --filter node=node-1
[[ $(echo ${output} | grep -o "tag_busybox" | wc -l) == 1 ]]
}

0 comments on commit 0579920

Please sign in to comment.