Merge pull request #1166 from chanwit/image-dedup

Implement grouping list of images by Id
This commit is contained in:
Victor Vieux 2015-09-08 11:52:43 -07:00
commit 0579920c0d
4 changed files with 40 additions and 13 deletions

View File

@ -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
@ -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)
}

View File

@ -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 ]
@ -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 ]
}

View File

@ -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

View File

@ -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 ]]
}