c8d/handlers: Handle error in walkPresentChildren

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
(cherry picked from commit 4295806736)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2023-06-02 10:23:03 +02:00
parent 087cf6f238
commit 961fe27408
No known key found for this signature in database
GPG key ID: B85EFCFE26DEF92A
4 changed files with 25 additions and 20 deletions

View file

@ -9,16 +9,13 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
) )
// walkPresentChildren is a simple wrapper for containerdimages.Walk with // walkPresentChildren is a simple wrapper for containerdimages.Walk with presentChildrenHandler.
// presentChildrenHandler wrapping a simple handler that only operates on
// walked Descriptor and doesn't return any errror.
// This is only a convenient helper to reduce boilerplate. // This is only a convenient helper to reduce boilerplate.
func (i *ImageService) walkPresentChildren(ctx context.Context, target ocispec.Descriptor, f func(context.Context, ocispec.Descriptor)) error { func (i *ImageService) walkPresentChildren(ctx context.Context, target ocispec.Descriptor, f func(context.Context, ocispec.Descriptor) error) error {
store := i.client.ContentStore() store := i.client.ContentStore()
return containerdimages.Walk(ctx, presentChildrenHandler(store, containerdimages.HandlerFunc( return containerdimages.Walk(ctx, presentChildrenHandler(store, containerdimages.HandlerFunc(
func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
f(ctx, desc) return nil, f(ctx, desc)
return nil, nil
})), target) })), target)
} }

View file

@ -124,10 +124,11 @@ func (i *ImageService) deleteAll(ctx context.Context, img images.Image, force, p
// Workaround for: https://github.com/moby/buildkit/issues/3797 // Workaround for: https://github.com/moby/buildkit/issues/3797
possiblyDeletedConfigs := map[digest.Digest]struct{}{} possiblyDeletedConfigs := map[digest.Digest]struct{}{}
err := i.walkPresentChildren(ctx, img.Target, func(_ context.Context, d ocispec.Descriptor) { err := i.walkPresentChildren(ctx, img.Target, func(_ context.Context, d ocispec.Descriptor) error {
if images.IsConfigType(d.MediaType) { if images.IsConfigType(d.MediaType) {
possiblyDeletedConfigs[d.Digest] = struct{}{} possiblyDeletedConfigs[d.Digest] = struct{}{}
} }
return nil
}) })
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -15,6 +15,11 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var (
errNotManifestOrIndex = errdefs.InvalidParameter(errors.New("descriptor is neither a manifest or index"))
errNotManifest = errdefs.InvalidParameter(errors.New("descriptor isn't a manifest"))
)
// walkImageManifests calls the handler for each locally present manifest in // walkImageManifests calls the handler for each locally present manifest in
// the image. The image implements the containerd.Image interface, but all // the image. The image implements the containerd.Image interface, but all
// operations act on the specific manifest instead of the index. // operations act on the specific manifest instead of the index.
@ -24,23 +29,23 @@ func (i *ImageService) walkImageManifests(ctx context.Context, img containerdima
handleManifest := func(ctx context.Context, d ocispec.Descriptor) error { handleManifest := func(ctx context.Context, d ocispec.Descriptor) error {
platformImg, err := i.NewImageManifest(ctx, img, d) platformImg, err := i.NewImageManifest(ctx, img, d)
if err != nil { if err != nil {
if err == errNotManifest {
return nil
}
return err return err
} }
return handler(platformImg) return handler(platformImg)
} }
if containerdimages.IsIndexType(desc.MediaType) { if containerdimages.IsManifestType(desc.MediaType) {
store := i.client.ContentStore() return handleManifest(ctx, desc)
return containerdimages.Walk(ctx, presentChildrenHandler(store, containerdimages.HandlerFunc(
func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if containerdimages.IsManifestType(desc.MediaType) {
return nil, handleManifest(ctx, desc)
}
return nil, nil
})), desc)
} }
return handleManifest(ctx, desc) if containerdimages.IsIndexType(desc.MediaType) {
return i.walkPresentChildren(ctx, desc, handleManifest)
}
return errNotManifestOrIndex
} }
type ImageManifest struct { type ImageManifest struct {
@ -54,7 +59,7 @@ type ImageManifest struct {
func (i *ImageService) NewImageManifest(ctx context.Context, img containerdimages.Image, manifestDesc ocispec.Descriptor) (*ImageManifest, error) { func (i *ImageService) NewImageManifest(ctx context.Context, img containerdimages.Image, manifestDesc ocispec.Descriptor) (*ImageManifest, error) {
if !containerdimages.IsManifestType(manifestDesc.MediaType) { if !containerdimages.IsManifestType(manifestDesc.MediaType) {
return nil, errdefs.InvalidParameter(errors.New("descriptor isn't a manifest")) return nil, errNotManifest
} }
parent := img.Target parent := img.Target

View file

@ -125,11 +125,12 @@ func (i *ImageService) pruneUnused(ctx context.Context, filterFunc imageFilterFu
blobs := []ocispec.Descriptor{} blobs := []ocispec.Descriptor{}
err := i.walkPresentChildren(ctx, img.Target, func(_ context.Context, desc ocispec.Descriptor) { err := i.walkPresentChildren(ctx, img.Target, func(_ context.Context, desc ocispec.Descriptor) error {
blobs = append(blobs, desc) blobs = append(blobs, desc)
if containerdimages.IsConfigType(desc.MediaType) { if containerdimages.IsConfigType(desc.MediaType) {
possiblyDeletedConfigs[desc.Digest] = struct{}{} possiblyDeletedConfigs[desc.Digest] = struct{}{}
} }
return nil
}) })
if err != nil { if err != nil {
errs = multierror.Append(errs, err) errs = multierror.Append(errs, err)
@ -186,10 +187,11 @@ func (i *ImageService) unleaseSnapshotsFromDeletedConfigs(ctx context.Context, p
var errs error var errs error
for _, img := range all { for _, img := range all {
err := i.walkPresentChildren(ctx, img.Target, func(_ context.Context, desc ocispec.Descriptor) { err := i.walkPresentChildren(ctx, img.Target, func(_ context.Context, desc ocispec.Descriptor) error {
if containerdimages.IsConfigType(desc.MediaType) { if containerdimages.IsConfigType(desc.MediaType) {
delete(possiblyDeletedConfigs, desc.Digest) delete(possiblyDeletedConfigs, desc.Digest)
} }
return nil
}) })
if err != nil { if err != nil {
errs = multierror.Append(errs, err) errs = multierror.Append(errs, err)