Browse Source

/distribution/{name}/json returns full Descriptor object

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
Nishant Totla 8 năm trước cách đây
mục cha
commit
12e232ee35

+ 18 - 6
api/server/router/distribution/distribution_routes.go

@@ -58,6 +58,7 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
 	if err != nil {
 		return err
 	}
+	blobsrvc := distrepo.Blobs(ctx)
 
 	if canonicalRef, ok := namedRef.(reference.Canonical); !ok {
 		namedRef = reference.TagNameOnly(namedRef)
@@ -67,25 +68,37 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
 			return errors.Errorf("image reference not tagged: %s", image)
 		}
 
-		dscrptr, err := distrepo.Tags(ctx).Get(ctx, taggedRef.Tag())
+		distributionInspect.Descriptor, err = distrepo.Tags(ctx).Get(ctx, taggedRef.Tag())
 		if err != nil {
 			return err
 		}
-		distributionInspect.Digest = dscrptr.Digest
 	} else {
-		distributionInspect.Digest = canonicalRef.Digest()
+		// TODO(nishanttotla): Once manifests can be looked up as a blob, the
+		// descriptor should be set using blobsrvc.Stat(ctx, canonicalRef.Digest())
+		// instead of having to manually fill in the fields
+		distributionInspect.Descriptor.Digest = canonicalRef.Digest()
 	}
-	// at this point, we have a digest, so we can retrieve the manifest
 
+	// we have a digest, so we can retrieve the manifest
 	mnfstsrvc, err := distrepo.Manifests(ctx)
 	if err != nil {
 		return err
 	}
-	mnfst, err := mnfstsrvc.Get(ctx, distributionInspect.Digest)
+	mnfst, err := mnfstsrvc.Get(ctx, distributionInspect.Descriptor.Digest)
 	if err != nil {
 		return err
 	}
 
+	mediaType, payload, err := mnfst.Payload()
+	if err != nil {
+		return err
+	}
+	// update MediaType because registry might return something incorrect
+	distributionInspect.Descriptor.MediaType = mediaType
+	if distributionInspect.Descriptor.Size == 0 {
+		distributionInspect.Descriptor.Size = int64(len(payload))
+	}
+
 	// retrieve platform information depending on the type of manifest
 	switch mnfstObj := mnfst.(type) {
 	case *manifestlist.DeserializedManifestList:
@@ -93,7 +106,6 @@ func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.Res
 			distributionInspect.Platforms = append(distributionInspect.Platforms, m.Platform)
 		}
 	case *schema2.DeserializedManifest:
-		blobsrvc := distrepo.Blobs(ctx)
 		configJSON, err := blobsrvc.Get(ctx, mnfstObj.Config.Digest)
 		var platform manifestlist.PlatformSpec
 		if err == nil {

+ 16 - 5
api/swagger.yaml

@@ -8283,15 +8283,26 @@ paths:
         - "application/json"
       responses:
         200:
-          description: "digest and platform information"
+          description: "descriptor and platform information"
           schema:
             type: "object"
             x-go-name: DistributionInspect
-            required: [Digest, ID, Platforms]
+            required: [Descriptor, Platforms]
             properties:
-              Digest:
-                type: "string"
-                x-nullable: false
+              Descriptor:
+                type: "object"
+                properties:
+                  MediaType:
+                    type: "string"
+                  Size:
+                    type: "integer"
+                    format: "int64"
+                  Digest:
+                    type: "string"
+                  URLs:
+                    type: "array"
+                    items:
+                      type: "string"
               Platforms:
                 type: "array"
                 items:

+ 4 - 3
api/types/registry/registry.go

@@ -4,8 +4,8 @@ import (
 	"encoding/json"
 	"net"
 
+	"github.com/docker/distribution"
 	"github.com/docker/distribution/manifest/manifestlist"
-	digest "github.com/opencontainers/go-digest"
 )
 
 // ServiceConfig stores daemon registry services configuration.
@@ -109,8 +109,9 @@ type SearchResults struct {
 // DistributionInspect describes the result obtained from contacting the
 // registry to retrieve image metadata
 type DistributionInspect struct {
-	// Digest is the content addressable digest for the image on the registry
-	Digest digest.Digest
+	// Descriptor contains information about the manifest, including
+	// the content addressable digest
+	Descriptor distribution.Descriptor
 	// Platforms contains the list of platforms supported by the image,
 	// obtained by parsing the manifest
 	Platforms []manifestlist.PlatformSpec