Explorar o código

c8d: align "Size" and "VirtualSize" for images

In versions of Docker before v1.10, this field was calculated from
the image itself and all of its parent images. Images are now stored
self-contained, and no longer use a parent-chain, making this field
an equivalent of the Size field.

For the containerd integration, the Size should be the sum of the
image's compressed / packaged and unpacked (snapshots) layers.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn %!s(int64=2) %!d(string=hai) anos
pai
achega
e7980275c0
Modificáronse 2 ficheiros con 17 adicións e 10 borrados
  1. 14 8
      daemon/containerd/image_list.go
  2. 3 2
      daemon/containerd/service.go

+ 14 - 8
daemon/containerd/image_list.go

@@ -203,11 +203,15 @@ func (i *ImageService) singlePlatformImage(ctx context.Context, contentStore con
 		sizeCache[d] = usage.Size
 		sizeCache[d] = usage.Size
 		return usage.Size, nil
 		return usage.Size, nil
 	}
 	}
-	virtualSize, err := computeVirtualSize(chainIDs, snapshotSizeFn)
+	snapshotSize, err := computeSnapshotSize(chainIDs, snapshotSizeFn)
 	if err != nil {
 	if err != nil {
 		return nil, nil, err
 		return nil, nil, err
 	}
 	}
 
 
+	// totalSize is the size of the image's packed layers and snapshots
+	// (unpacked layers) combined.
+	totalSize := size + snapshotSize
+
 	var repoTags, repoDigests []string
 	var repoTags, repoDigests []string
 	rawImg := image.Metadata()
 	rawImg := image.Metadata()
 	target := rawImg.Target.Digest
 	target := rawImg.Target.Digest
@@ -245,8 +249,8 @@ func (i *ImageService) singlePlatformImage(ctx context.Context, contentStore con
 		Created:     rawImg.CreatedAt.Unix(),
 		Created:     rawImg.CreatedAt.Unix(),
 		RepoDigests: repoDigests,
 		RepoDigests: repoDigests,
 		RepoTags:    repoTags,
 		RepoTags:    repoTags,
-		Size:        size,
-		VirtualSize: virtualSize,
+		Size:        totalSize,
+		VirtualSize: totalSize,
 		// -1 indicates that the value has not been set (avoids ambiguity
 		// -1 indicates that the value has not been set (avoids ambiguity
 		// between 0 (default) and "not set". We cannot use a pointer (nil)
 		// between 0 (default) and "not set". We cannot use a pointer (nil)
 		// for this, as the JSON representation uses "omitempty", which would
 		// for this, as the JSON representation uses "omitempty", which would
@@ -476,16 +480,18 @@ func setupLabelFilter(store content.Store, fltrs filters.Args) (func(image image
 	}, nil
 	}, nil
 }
 }
 
 
-func computeVirtualSize(chainIDs []digest.Digest, sizeFn func(d digest.Digest) (int64, error)) (int64, error) {
-	var virtualSize int64
+// computeSnapshotSize calculates the total size consumed by the snapshots
+// for the given chainIDs.
+func computeSnapshotSize(chainIDs []digest.Digest, sizeFn func(d digest.Digest) (int64, error)) (int64, error) {
+	var totalSize int64
 	for _, chainID := range chainIDs {
 	for _, chainID := range chainIDs {
 		size, err := sizeFn(chainID)
 		size, err := sizeFn(chainID)
 		if err != nil {
 		if err != nil {
-			return virtualSize, err
+			return totalSize, err
 		}
 		}
-		virtualSize += size
+		totalSize += size
 	}
 	}
-	return virtualSize, nil
+	return totalSize, nil
 }
 }
 
 
 func computeSharedSize(chainIDs []digest.Digest, layers map[digest.Digest]int, sizeFn func(d digest.Digest) (int64, error)) (int64, error) {
 func computeSharedSize(chainIDs []digest.Digest, layers map[digest.Digest]int, sizeFn func(d digest.Digest) (int64, error)) (int64, error) {

+ 3 - 2
daemon/containerd/service.go

@@ -195,10 +195,11 @@ func (i *ImageService) GetContainerLayerSize(ctx context.Context, containerID st
 	}
 	}
 
 
 	chainIDs := identity.ChainIDs(img.RootFS.DiffIDs)
 	chainIDs := identity.ChainIDs(img.RootFS.DiffIDs)
-	virtualSize, err := computeVirtualSize(chainIDs, snapshotSizeFn)
+	snapShotSize, err := computeSnapshotSize(chainIDs, snapshotSizeFn)
 	if err != nil {
 	if err != nil {
 		return 0, 0, err
 		return 0, 0, err
 	}
 	}
 
 
-	return usage.Size, usage.Size + virtualSize, nil
+	// TODO(thaJeztah): include content-store size for the image (similar to "GET /images/json")
+	return usage.Size, usage.Size + snapShotSize, nil
 }
 }