Skip to content

Commit

Permalink
Merge pull request moby#6 from ndeloof/img_virtual_size
Browse files Browse the repository at this point in the history
Compute image VirtualSize
  • Loading branch information
ndeloof committed Jul 6, 2022
2 parents 5d0eaf6 + 7a5da86 commit 3c8e385
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion daemon/containerd/service.go
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/containerd/containerd"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/snapshots"
"github.com/docker/distribution"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/identity"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -68,19 +70,25 @@ func (cs *containerdStore) Images(ctx context.Context, opts types.ImageListOptio
}

var ret []*types.ImageSummary
snapshotter := cs.client.SnapshotService(containerd.DefaultSnapshotter)
for _, image := range images {
size, err := image.Size(ctx)
if err != nil {
return nil, err
}

virtualSize, err := computeVirtualSize(ctx, image, snapshotter)
if err != nil {
return nil, err
}

ret = append(ret, &types.ImageSummary{
RepoDigests: []string{image.Name() + "@" + image.Target().Digest.String()}, // "hello-world@sha256:bfea6278a0a267fad2634554f4f0c6f31981eea41c553fdf5a83e95a41d40c38"},
RepoTags: []string{image.Name()},
Containers: -1,
ParentID: "",
SharedSize: -1,
VirtualSize: 10,
VirtualSize: virtualSize,
ID: image.Target().Digest.String(),
Created: image.Metadata().CreatedAt.Unix(),
Size: size,
Expand All @@ -90,6 +98,22 @@ func (cs *containerdStore) Images(ctx context.Context, opts types.ImageListOptio
return ret, nil
}

func computeVirtualSize(ctx context.Context, image containerd.Image, snapshotter snapshots.Snapshotter) (int64, error) {
var virtualSize int64
diffIDs, err := image.RootFS(ctx)
if err != nil {
return virtualSize, err
}
for _, chainID := range identity.ChainIDs(diffIDs) {
usage, err := snapshotter.Usage(ctx, chainID.String())
if err != nil {
return virtualSize, err
}
virtualSize += usage.Size
}
return virtualSize, nil
}

func (cs *containerdStore) LogImageEvent(imageID, refName, action string) {
panic("not implemented")
}
Expand Down

0 comments on commit 3c8e385

Please sign in to comment.