Skip to content

Commit

Permalink
Fix fetching referrers error handling (#1648)
Browse files Browse the repository at this point in the history
* Add a test that tries to get the referrers for the image that does not have a referrer

* Fix the error handling when fetching referrers in an OCI registry that does not support for referrers API
  • Loading branch information
otms61 authored Apr 17, 2023
1 parent ed5c185 commit bc990d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 6 additions & 5 deletions pkg/v1/remote/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@ func (f *fetcher) fetchReferrers(ctx context.Context, filter map[string]string,
// The registry doesn't support the Referrers API endpoint, so we'll use the fallback tag scheme.
b, _, err := f.fetchManifest(ctx, fallbackTag(d), []types.MediaType{types.OCIImageIndex})
if err != nil {
var terr *transport.Error
if ok := errors.As(err, &terr); ok && terr.StatusCode == http.StatusNotFound {
// Not found just means there are no attachments yet. Start with an empty manifest.
return &v1.IndexManifest{MediaType: types.OCIImageIndex}, nil
}

return nil, err
}
var terr *transport.Error
if ok := errors.As(err, &terr); ok && terr.StatusCode == http.StatusNotFound {
// Not found just means there are no attachments yet. Start with an empty manifest.
return &v1.IndexManifest{MediaType: types.OCIImageIndex}, nil
}

var im v1.IndexManifest
if err := json.Unmarshal(b, &im); err != nil {
Expand Down
13 changes: 11 additions & 2 deletions pkg/v1/remote/referrers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func TestReferrers(t *testing.T) {
rootDesc := descriptor(rootImg)
t.Logf("root image is %s", rootDesc.Digest)

// Before pushing referrers, try to get the referrers of the root image.
rootRefDigest := rootRef.Context().Digest(rootDesc.Digest.String())
index, err := remote.Referrers(rootRefDigest)
if err != nil {
t.Fatal(err)
}
if numManifests := len(index.Manifests); numManifests != 0 {
t.Fatalf("expected index to contain 0 manifests, but had %d", numManifests)
}

// Push an image that refers to the root image as its subject.
leafRef, err := name.ParseReference(fmt.Sprintf("%s/repo:leaf", u.Host))
if err != nil {
Expand All @@ -112,8 +122,7 @@ func TestReferrers(t *testing.T) {
t.Logf("leaf image is %s", leafDesc.Digest)

// Get the referrers of the root image, by digest.
rootRefDigest := rootRef.Context().Digest(rootDesc.Digest.String())
index, err := remote.Referrers(rootRefDigest)
index, err = remote.Referrers(rootRefDigest)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit bc990d6

Please sign in to comment.