Skip to content

Commit

Permalink
add protoype filter/sort; add registry wide discover api; update disc…
Browse files Browse the repository at this point in the history
…over response

Signed-off-by: Akash Singhal <[email protected]>
  • Loading branch information
akashsinghal committed Apr 25, 2022
1 parent 75a7a29 commit 94a8dc6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 27 deletions.
4 changes: 2 additions & 2 deletions registry/api/v2/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func ExtendRoute(ns, ext, component string, template RouteDescriptor, nameRequir
path := routeDescriptorsMap[RouteNameBase].Path
if nameRequired {
name = RouteNameExtensionsRepository
path += "{name:" + reference.NameRegexp.String() + "}"
path += "{name:" + reference.NameRegexp.String() + "}/"
}
name = fmt.Sprintf("%s-%s-%s-%s", name, ns, ext, component)
path = fmt.Sprintf("%s/_%s/%s/%s", path, ns, ext, component)
path = fmt.Sprintf("%s_%s/%s/%s", path, ns, ext, component)

desc := template
desc.Name = name
Expand Down
2 changes: 0 additions & 2 deletions registry/extension/oci/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

type discoverGetAPIResponse struct {
Name string `json:"name"`
Extensions []extension.EnumerateExtension `json:"extensions"`
}

Expand All @@ -37,7 +36,6 @@ func (th *extensionHandler) getExtensions(w http.ResponseWriter, r *http.Request

enc := json.NewEncoder(w)
if err := enc.Encode(discoverGetAPIResponse{
Name: th.Repository.Named().Name(),
Extensions: enumeratedExtensions,
}); err != nil {
th.Errors = append(th.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
Expand Down
23 changes: 21 additions & 2 deletions registry/extension/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,28 @@ func (d *ociNamespace) GetRepositoryRoutes() []extension.Route {
}

// GetRegistryRoutes returns a list of extension routes scoped at a registry level
// There are no registry scoped routes exposed by this namespace
func (d *ociNamespace) GetRegistryRoutes() []extension.Route {
return nil
var routes []extension.Route

if d.discoverEnabled {
routes = append(routes, extension.Route{
Namespace: namespaceName,
Extension: extensionName,
Component: discoverComponentName,
Descriptor: v2.RouteDescriptor{
Entity: "Extension",
Methods: []v2.MethodDescriptor{
{
Method: "GET",
Description: "Get all extensions enabled for a registry.",
},
},
},
Dispatcher: d.discoverDispatcher,
})
}

return routes
}

// GetNamespaceName returns the name associated with the namespace
Expand Down
10 changes: 9 additions & 1 deletion registry/extension/oras/artifactmanifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (a Manifest) Annotations() map[string]string {
return a.inner.Annotations
}

// MediaType returns the media type of this ORAS artifact.
func (a Manifest) MediaType() string {
return a.inner.MediaType
}

// References returns the distribution descriptors for the referenced blobs.
func (a Manifest) References() []distribution.Descriptor {
blobs := make([]distribution.Descriptor, len(a.inner.Blobs))
Expand Down Expand Up @@ -84,7 +89,10 @@ func (d *DeserializedManifest) UnmarshalJSON(b []byte) error {
if man.ArtifactType == "" {
return errors.New("artifactType cannot be empty")
}
// TODO: add media type enforcement?
if man.MediaType == "" {
return errors.New("mediaType cannot be empty")
}

d.inner = man

return nil
Expand Down
14 changes: 13 additions & 1 deletion registry/extension/oras/artifactmanifesthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"path"
"time"

"github.com/distribution/distribution/v3"
dcontext "github.com/distribution/distribution/v3/context"
Expand Down Expand Up @@ -77,7 +78,18 @@ func (amh *artifactManifestHandler) verifyManifest(ctx context.Context, dm Deser
if dm.ArtifactType() == "" {
errs = append(errs, distribution.ErrManifestVerification{errors.New("artifactType invalid")})
}
// TODO: check that media type exists

if dm.MediaType() == "" {
errs = append(errs, distribution.ErrManifestVerification{errors.New("mediaType invalid")})
}

if createdAt, ok := dm.Annotations()[createAnnotationName]; ok {
_, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
errs = append(errs, distribution.ErrManifestVerification{errors.New("failed to parse created time: " + err.Error())})
}
}
// TODO: check that created annotation has correct format
if !skipDependencyVerification {
bs := amh.repository.Blobs(ctx)

Expand Down
29 changes: 10 additions & 19 deletions registry/extension/oras/artifactservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ type referrersHandler struct {
Digest digest.Digest
}

type referrers []artifactv1.Descriptor

const createAnnotationName = "io.cncf.oras.artifact.created"

func (h *referrersHandler) Referrers(ctx context.Context, revision digest.Digest, referrerType string) ([]artifactv1.Descriptor, error) {
dcontext.GetLogger(ctx).Debug("(*manifestStore).Referrers")

var referrersUnsorted referrers
var referrersSorted referrers
var referrersUnsorted []artifactv1.Descriptor
var referrersSorted []artifactv1.Descriptor

repo := h.extContext.Repository
manifests, err := repo.Manifests(ctx)
Expand Down Expand Up @@ -89,7 +87,14 @@ func (h *referrersHandler) Referrers(ctx context.Context, revision digest.Digest
return nil, err
}

sort.Sort(referrersSorted)
// sort the list of descriptors that contain the created annotation
sort.Slice(referrersSorted, func(i, j int) bool {
firstElem, _ := time.Parse(time.RFC3339, referrersSorted[i].Annotations[createAnnotationName])
secondElem, _ := time.Parse(time.RFC3339, referrersSorted[j].Annotations[createAnnotationName])
// most recent artifact first
return firstElem.After(secondElem)
})
// append the descriptors, which don't have a created annotation, to the end
referrersSorted = append(referrersSorted, referrersUnsorted...)
return referrersSorted, nil
}
Expand Down Expand Up @@ -147,17 +152,3 @@ func (h *referrersHandler) readlink(ctx context.Context, path string) (digest.Di

return linked, nil
}

func (s referrers) Len() int {
return len(s)
}

func (s referrers) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s referrers) Less(i, j int) bool {
firstElem, _ := time.Parse(time.RFC3339, s[i].Annotations[createAnnotationName])
secondElem, _ := time.Parse(time.RFC3339, s[j].Annotations[createAnnotationName])
return firstElem.After(secondElem)
}

0 comments on commit 94a8dc6

Please sign in to comment.