Skip to content

Commit

Permalink
feat: support referrers for oci image manifests (#43)
Browse files Browse the repository at this point in the history
Signed-off-by: wangxiaoxuan273 <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Dec 2, 2022
1 parent 9ac1115 commit 8c6c680
Showing 1 changed file with 67 additions and 17 deletions.
84 changes: 67 additions & 17 deletions registry/handlers/referrers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/distribution/distribution/v3"
dcontext "github.com/distribution/distribution/v3/context"
"github.com/distribution/distribution/v3/manifest/ociartifact"
"github.com/distribution/distribution/v3/manifest/ocischema"
"github.com/distribution/distribution/v3/registry/api/errcode"
v2 "github.com/distribution/distribution/v3/registry/api/v2"
"github.com/distribution/distribution/v3/registry/storage"
Expand Down Expand Up @@ -109,29 +110,28 @@ func (h *referrersHandler) generateReferrersList(ctx context.Context, subjectDig
if err != nil {
return err
}
artifactManifest, ok := man.(*ociartifact.DeserializedManifest)
if !ok {
// The PUT handler would guard against this situation. Skip this manifest.
switch manifest := man.(type) {
case *ocischema.DeserializedManifest:
referrer, toAppend, err := generateReferrerFromImage(ctx, blobStatter, referrerDigest, manifest, artifactType)
if err != nil {
return err
}
if toAppend {
referrers = append(referrers, referrer)
}
return nil
}
extractedArtifactType := artifactManifest.ArtifactType
// filtering by artifact type or bypass if no artifact type specified
if artifactType == "" || extractedArtifactType == artifactType {
desc, err := blobStatter.Stat(ctx, referrerDigest)
case *ociartifact.DeserializedManifest:
referrer, toAppend, err := generateReferrerFromArtifact(ctx, blobStatter, referrerDigest, manifest, artifactType)
if err != nil {
return err
}
desc.MediaType, _, _ = man.Payload()
artifactDesc := v1.Descriptor{
MediaType: desc.MediaType,
Size: desc.Size,
Digest: desc.Digest,
ArtifactType: extractedArtifactType,
Annotations: artifactManifest.Annotations,
if toAppend {
referrers = append(referrers, referrer)
}
referrers = append(referrers, artifactDesc)
return nil
default:
return nil
}
return nil
})
if err != nil {
switch err.(type) {
Expand Down Expand Up @@ -190,3 +190,53 @@ func readlink(ctx context.Context, path string, stDriver driver.StorageDriver) (

return digest.Parse(string(content))
}

func generateReferrerFromArtifact(ctx context.Context,
blobStatter distribution.BlobStatter,
referrerDigest digest.Digest,
man *ociartifact.DeserializedManifest,
artifactType string) (v1.Descriptor, bool, error) {
extractedArtifactType := man.ArtifactType
// filtering by artifact type or bypass if no artifact type specified
if artifactType == "" || extractedArtifactType == artifactType {
desc, err := blobStatter.Stat(ctx, referrerDigest)
if err != nil {
return v1.Descriptor{}, false, err
}
desc.MediaType, _, _ = man.Payload()
artifactDesc := v1.Descriptor{
MediaType: desc.MediaType,
Size: desc.Size,
Digest: desc.Digest,
ArtifactType: extractedArtifactType,
Annotations: man.Annotations,
}
return artifactDesc, true, nil
}
return v1.Descriptor{}, false, nil
}

func generateReferrerFromImage(ctx context.Context,
blobStatter distribution.BlobStatter,
referrerDigest digest.Digest,
man *ocischema.DeserializedManifest,
configMediaType string) (v1.Descriptor, bool, error) {
extractedConfigMediaType := man.Config.MediaType
// filtering by artifact type or bypass if no artifact type specified
if configMediaType == "" || extractedConfigMediaType == configMediaType {
desc, err := blobStatter.Stat(ctx, referrerDigest)
if err != nil {
return v1.Descriptor{}, false, err
}
desc.MediaType, _, _ = man.Payload()
imageDesc := v1.Descriptor{
MediaType: desc.MediaType,
Size: desc.Size,
Digest: desc.Digest,
ArtifactType: extractedConfigMediaType,
Annotations: man.Annotations,
}
return imageDesc, true, nil
}
return v1.Descriptor{}, false, nil
}

0 comments on commit 8c6c680

Please sign in to comment.