From 8867e8fac375246f57cea8fdcca285424384af87 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Wed, 14 Dec 2016 16:17:20 -0800 Subject: [PATCH] Update schema2 builder to take media type Modify manifest builder so it can be used to build manifests with different configuration media types. Rename config media type const to image config. Signed-off-by: Tonis Tiigi Signed-off-by: Derek McGowan --- manifest/schema2/builder.go | 30 +++++++++++-------- manifest/schema2/builder_test.go | 4 +-- manifest/schema2/manifest.go | 4 +-- manifest/schema2/manifest_test.go | 4 +-- registry/handlers/api_test.go | 2 +- registry/handlers/images.go | 2 +- .../storage/schema2manifesthandler_test.go | 2 +- testutil/manifests.go | 2 +- 8 files changed, 27 insertions(+), 23 deletions(-) diff --git a/manifest/schema2/builder.go b/manifest/schema2/builder.go index ec0bf858..8fffc80d 100644 --- a/manifest/schema2/builder.go +++ b/manifest/schema2/builder.go @@ -11,21 +11,25 @@ type builder struct { // bs is a BlobService used to publish the configuration blob. bs distribution.BlobService + // configMediaType is media type used to describe configuration + configMediaType string + // configJSON references configJSON []byte - // layers is a list of layer descriptors that gets built by successive - // calls to AppendReference. - layers []distribution.Descriptor + // dependencies is a list of descriptors that gets built by successive + // calls to AppendReference. In case of image configuration these are layers. + dependencies []distribution.Descriptor } // NewManifestBuilder is used to build new manifests for the current schema // version. It takes a BlobService so it can publish the configuration blob // as part of the Build process. -func NewManifestBuilder(bs distribution.BlobService, configJSON []byte) distribution.ManifestBuilder { +func NewManifestBuilder(bs distribution.BlobService, configMediaType string, configJSON []byte) distribution.ManifestBuilder { mb := &builder{ - bs: bs, - configJSON: make([]byte, len(configJSON)), + bs: bs, + configMediaType: configMediaType, + configJSON: make([]byte, len(configJSON)), } copy(mb.configJSON, configJSON) @@ -36,9 +40,9 @@ func NewManifestBuilder(bs distribution.BlobService, configJSON []byte) distribu func (mb *builder) Build(ctx context.Context) (distribution.Manifest, error) { m := Manifest{ Versioned: SchemaVersion, - Layers: make([]distribution.Descriptor, len(mb.layers)), + Layers: make([]distribution.Descriptor, len(mb.dependencies)), } - copy(m.Layers, mb.layers) + copy(m.Layers, mb.dependencies) configDigest := digest.FromBytes(mb.configJSON) @@ -48,7 +52,7 @@ func (mb *builder) Build(ctx context.Context) (distribution.Manifest, error) { case nil: // Override MediaType, since Put always replaces the specified media // type with application/octet-stream in the descriptor it returns. - m.Config.MediaType = MediaTypeConfig + m.Config.MediaType = mb.configMediaType return FromStruct(m) case distribution.ErrBlobUnknown: // nop @@ -57,10 +61,10 @@ func (mb *builder) Build(ctx context.Context) (distribution.Manifest, error) { } // Add config to the blob store - m.Config, err = mb.bs.Put(ctx, MediaTypeConfig, mb.configJSON) + m.Config, err = mb.bs.Put(ctx, mb.configMediaType, mb.configJSON) // Override MediaType, since Put always replaces the specified media // type with application/octet-stream in the descriptor it returns. - m.Config.MediaType = MediaTypeConfig + m.Config.MediaType = mb.configMediaType if err != nil { return nil, err } @@ -70,11 +74,11 @@ func (mb *builder) Build(ctx context.Context) (distribution.Manifest, error) { // AppendReference adds a reference to the current ManifestBuilder. func (mb *builder) AppendReference(d distribution.Describable) error { - mb.layers = append(mb.layers, d.Descriptor()) + mb.dependencies = append(mb.dependencies, d.Descriptor()) return nil } // References returns the current references added to this builder. func (mb *builder) References() []distribution.Descriptor { - return mb.layers + return mb.dependencies } diff --git a/manifest/schema2/builder_test.go b/manifest/schema2/builder_test.go index 851f917c..9dfa5fc6 100644 --- a/manifest/schema2/builder_test.go +++ b/manifest/schema2/builder_test.go @@ -166,7 +166,7 @@ func TestBuilder(t *testing.T) { } bs := &mockBlobService{descriptors: make(map[digest.Digest]distribution.Descriptor)} - builder := NewManifestBuilder(bs, imgJSON) + builder := NewManifestBuilder(bs, MediaTypeImageConfig, imgJSON) for _, d := range descriptors { if err := builder.AppendReference(d); err != nil { @@ -195,7 +195,7 @@ func TestBuilder(t *testing.T) { if target.Digest != configDigest { t.Fatalf("unexpected digest in target: %s", target.Digest.String()) } - if target.MediaType != MediaTypeConfig { + if target.MediaType != MediaTypeImageConfig { t.Fatalf("unexpected media type in target: %s", target.MediaType) } if target.Size != 3153 { diff --git a/manifest/schema2/manifest.go b/manifest/schema2/manifest.go index 741998d0..485f38a7 100644 --- a/manifest/schema2/manifest.go +++ b/manifest/schema2/manifest.go @@ -14,8 +14,8 @@ const ( // MediaTypeManifest specifies the mediaType for the current version. MediaTypeManifest = "application/vnd.docker.distribution.manifest.v2+json" - // MediaTypeConfig specifies the mediaType for the image configuration. - MediaTypeConfig = "application/vnd.docker.container.image.v1+json" + // MediaTypeImageConfig specifies the mediaType for the image configuration. + MediaTypeImageConfig = "application/vnd.docker.container.image.v1+json" // MediaTypePluginConfig specifies the mediaType for plugin configuration. MediaTypePluginConfig = "application/vnd.docker.plugin.v1+json" diff --git a/manifest/schema2/manifest_test.go b/manifest/schema2/manifest_test.go index f0003d18..86226606 100644 --- a/manifest/schema2/manifest_test.go +++ b/manifest/schema2/manifest_test.go @@ -32,7 +32,7 @@ func TestManifest(t *testing.T) { Config: distribution.Descriptor{ Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b", Size: 985, - MediaType: MediaTypeConfig, + MediaType: MediaTypeImageConfig, }, Layers: []distribution.Descriptor{ { @@ -82,7 +82,7 @@ func TestManifest(t *testing.T) { if target.Digest != "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b" { t.Fatalf("unexpected digest in target: %s", target.Digest.String()) } - if target.MediaType != MediaTypeConfig { + if target.MediaType != MediaTypeImageConfig { t.Fatalf("unexpected media type in target: %s", target.MediaType) } if target.Size != 985 { diff --git a/registry/handlers/api_test.go b/registry/handlers/api_test.go index 9d64fbbf..d8e93053 100644 --- a/registry/handlers/api_test.go +++ b/registry/handlers/api_test.go @@ -1218,7 +1218,7 @@ func testManifestAPISchema2(t *testing.T, env *testEnv, imageName reference.Name Config: distribution.Descriptor{ Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b", Size: 3253, - MediaType: schema2.MediaTypeConfig, + MediaType: schema2.MediaTypeImageConfig, }, Layers: []distribution.Descriptor{ { diff --git a/registry/handlers/images.go b/registry/handlers/images.go index 9518f685..c51bbddb 100644 --- a/registry/handlers/images.go +++ b/registry/handlers/images.go @@ -360,7 +360,7 @@ func (imh *imageManifestHandler) applyResourcePolicy(manifest distribution.Manif class = "image" case *schema2.DeserializedManifest: switch m.Config.MediaType { - case schema2.MediaTypeConfig: + case schema2.MediaTypeImageConfig: class = "image" case schema2.MediaTypePluginConfig: class = "plugin" diff --git a/registry/storage/schema2manifesthandler_test.go b/registry/storage/schema2manifesthandler_test.go index 5051fa3d..6536f9d3 100644 --- a/registry/storage/schema2manifesthandler_test.go +++ b/registry/storage/schema2manifesthandler_test.go @@ -20,7 +20,7 @@ func TestVerifyManifestForeignLayer(t *testing.T) { repo := makeRepository(t, registry, "test") manifestService := makeManifestService(t, repo) - config, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeConfig, nil) + config, err := repo.Blobs(ctx).Put(ctx, schema2.MediaTypeImageConfig, nil) if err != nil { t.Fatal(err) } diff --git a/testutil/manifests.go b/testutil/manifests.go index c4f9fef5..a77067d7 100644 --- a/testutil/manifests.go +++ b/testutil/manifests.go @@ -73,7 +73,7 @@ func MakeSchema1Manifest(digests []digest.Digest) (distribution.Manifest, error) func MakeSchema2Manifest(repository distribution.Repository, digests []digest.Digest) (distribution.Manifest, error) { ctx := context.Background() blobStore := repository.Blobs(ctx) - builder := schema2.NewManifestBuilder(blobStore, []byte{}) + builder := schema2.NewManifestBuilder(blobStore, schema2.MediaTypeImageConfig, []byte{}) for _, digest := range digests { builder.AppendReference(distribution.Descriptor{Digest: digest}) }