Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support referrers for oci image manifests #43

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
shizhMSFT marked this conversation as resolved.
Show resolved Hide resolved
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
}