diff --git a/libimage/layer_tree.go b/libimage/layer_tree.go index a7d2f8c58..c9de814a7 100644 --- a/libimage/layer_tree.go +++ b/libimage/layer_tree.go @@ -2,6 +2,7 @@ package libimage import ( "context" + "errors" "github.com/containers/storage" ociv1 "github.com/opencontainers/image-spec/specs-go/v1" @@ -31,6 +32,9 @@ func (t *layerTree) node(layerID string) *layerNode { } // toOCI returns an OCI image for the specified image. +// +// WARNING: callers are responsible for handling cases where the target image +// has been removed and need to check for `storage.ErrImageUnknown`. func (t *layerTree) toOCI(ctx context.Context, i *Image) (*ociv1.Image, error) { var err error oci, exists := t.ociCache[i.ID()] @@ -155,6 +159,9 @@ func (t *layerTree) children(ctx context.Context, parent *Image, all bool) ([]*I parentID := parent.ID() parentOCI, err := t.toOCI(ctx, parent) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + return nil, nil + } return nil, err } @@ -165,6 +172,9 @@ func (t *layerTree) children(ctx context.Context, parent *Image, all bool) ([]*I } childOCI, err := t.toOCI(ctx, child) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + return false, nil + } return false, err } // History check. @@ -255,6 +265,9 @@ func (t *layerTree) parent(ctx context.Context, child *Image) (*Image, error) { childID := child.ID() childOCI, err := t.toOCI(ctx, child) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + return nil, nil + } return nil, err } @@ -268,6 +281,9 @@ func (t *layerTree) parent(ctx context.Context, child *Image) (*Image, error) { } emptyOCI, err := t.toOCI(ctx, empty) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + return nil, nil + } return nil, err } // History check. @@ -300,6 +316,9 @@ func (t *layerTree) parent(ctx context.Context, child *Image) (*Image, error) { } parentOCI, err := t.toOCI(ctx, parent) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + return nil, nil + } return nil, err } // History check.