c8d: Use the labels to get the children of an image
Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
This commit is contained in:
parent
dcc80204bc
commit
7e0cb4c46f
2 changed files with 4 additions and 110 deletions
|
@ -15,67 +15,16 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Children returns a slice of image ID which rootfs is a superset of the
|
||||
// rootfs of the given image ID, excluding images with exactly the same rootfs.
|
||||
// Called from list.go to filter containers.
|
||||
// Children returns a slice of image IDs that are children of the `id` image
|
||||
func (i *ImageService) Children(ctx context.Context, id image.ID) ([]image.ID, error) {
|
||||
target, err := i.resolveDescriptor(ctx, id.String())
|
||||
if err != nil {
|
||||
return []image.ID{}, errors.Wrap(err, "failed to get parent image")
|
||||
}
|
||||
|
||||
cs := i.client.ContentStore()
|
||||
|
||||
allPlatforms, err := containerdimages.Platforms(ctx, cs, target)
|
||||
if err != nil {
|
||||
return []image.ID{}, errdefs.System(errors.Wrap(err, "failed to list platforms supported by image"))
|
||||
}
|
||||
|
||||
parentRootFS := []ocispec.RootFS{}
|
||||
for _, platform := range allPlatforms {
|
||||
rootfs, err := platformRootfs(ctx, cs, target, platform)
|
||||
if err != nil {
|
||||
if !cerrdefs.IsNotFound(err) {
|
||||
log.G(ctx).WithFields(log.Fields{
|
||||
"error": err,
|
||||
"image": target.Digest,
|
||||
"platform": platform,
|
||||
}).Warning("failed to get platform-specific rootfs")
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
parentRootFS = append(parentRootFS, rootfs)
|
||||
}
|
||||
|
||||
imgs, err := i.client.ImageService().List(ctx)
|
||||
imgs, err := i.client.ImageService().List(ctx, "labels."+imageLabelClassicBuilderParent+"=="+string(id))
|
||||
if err != nil {
|
||||
return []image.ID{}, errdefs.System(errors.Wrap(err, "failed to list all images"))
|
||||
}
|
||||
|
||||
children := []image.ID{}
|
||||
var children []image.ID
|
||||
for _, img := range imgs {
|
||||
nextImage:
|
||||
for _, platform := range allPlatforms {
|
||||
rootfs, err := platformRootfs(ctx, cs, img.Target, platform)
|
||||
if err != nil {
|
||||
if !cerrdefs.IsNotFound(err) {
|
||||
log.G(ctx).WithFields(log.Fields{
|
||||
"error": err,
|
||||
"image": img.Target.Digest,
|
||||
"platform": platform,
|
||||
}).Warning("failed to get platform-specific rootfs")
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
for _, parentRoot := range parentRootFS {
|
||||
if isRootfsChildOf(rootfs, parentRoot) {
|
||||
children = append(children, image.ID(img.Target.Digest))
|
||||
break nextImage
|
||||
}
|
||||
}
|
||||
}
|
||||
children = append(children, image.ID(img.Target.Digest))
|
||||
}
|
||||
|
||||
return children, nil
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
package containerd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/opencontainers/go-digest"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
|
||||
func TestIsRootfsChildOf(t *testing.T) {
|
||||
// Each unique letter is one distinct DiffID
|
||||
ab := toRootfs("AB")
|
||||
abc := toRootfs("ABC")
|
||||
abd := toRootfs("ABD")
|
||||
xyz := toRootfs("XYZ")
|
||||
xyzab := toRootfs("XYZAB")
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
parent ocispec.RootFS
|
||||
child ocispec.RootFS
|
||||
out bool
|
||||
}{
|
||||
{parent: ab, child: abc, out: true, name: "one additional layer"},
|
||||
{parent: xyz, child: xyzab, out: true, name: "two additional layers"},
|
||||
{parent: xyz, child: xyz, out: false, name: "parent is not a child of itself"},
|
||||
{parent: abc, child: abd, out: false, name: "sibling"},
|
||||
{parent: abc, child: xyz, out: false, name: "completely different rootfs, but same length"},
|
||||
{parent: abc, child: ab, out: false, name: "child can't be shorter than parent"},
|
||||
{parent: ab, child: xyzab, out: false, name: "parent layers appended"},
|
||||
} {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out := isRootfsChildOf(tc.child, tc.parent)
|
||||
|
||||
assert.Check(t, is.Equal(out, tc.out))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func toRootfs(values string) ocispec.RootFS {
|
||||
dgsts := []digest.Digest{}
|
||||
|
||||
for _, v := range values {
|
||||
vd := digest.FromString(string(v))
|
||||
dgsts = append(dgsts, vd)
|
||||
}
|
||||
|
||||
return ocispec.RootFS{
|
||||
Type: "layers",
|
||||
DiffIDs: dgsts,
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue