Skip to content

Commit

Permalink
deprecate Versioned in favor of oci.Versioned
Browse files Browse the repository at this point in the history
Update the Manifest types to use the oci implementation of the Versioned
struct.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Jul 16, 2024
1 parent 54cf416 commit 8efd133
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 103 deletions.
6 changes: 3 additions & 3 deletions internal/client/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/internal/dcontext"
"github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/ocischema"
"github.com/distribution/distribution/v3/registry/api/errcode"
"github.com/distribution/distribution/v3/testutil"
"github.com/distribution/reference"
"github.com/google/uuid"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -928,10 +928,10 @@ func newRandomOCIManifest(t *testing.T, blobCount int) (*ocischema.Manifest, dig
}

m := ocischema.Manifest{
Versioned: manifest.Versioned{
Versioned: specs.Versioned{
SchemaVersion: 2,
MediaType: v1.MediaTypeImageManifest,
},
MediaType: v1.MediaTypeImageManifest,
Config: distribution.Descriptor{
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
Size: 123,
Expand Down
24 changes: 18 additions & 6 deletions manifest/manifestlist/manifestlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand All @@ -18,6 +19,9 @@ const (

// SchemaVersion provides a pre-initialized version structure for this
// packages version of the manifest.
//
// Deprecated: use [specs.Versioned] and set MediaType on the manifest
// to [MediaTypeManifestList].
var SchemaVersion = manifest.Versioned{
SchemaVersion: 2,
MediaType: MediaTypeManifestList,
Expand Down Expand Up @@ -85,7 +89,10 @@ type ManifestDescriptor struct {

// ManifestList references manifests for various platforms.
type ManifestList struct {
manifest.Versioned
specs.Versioned

// MediaType is the media type of this schema.
MediaType string `json:"mediaType,omitempty"`

// Manifests references a list of manifests
Manifests []ManifestDescriptor `json:"manifests"`
Expand Down Expand Up @@ -128,10 +135,8 @@ func FromDescriptors(descriptors []ManifestDescriptor) (*DeserializedManifestLis
// fromDescriptorsWithMediaType is for testing purposes, it's useful to be able to specify the media type explicitly
func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, mediaType string) (*DeserializedManifestList, error) {
m := ManifestList{
Versioned: manifest.Versioned{
SchemaVersion: SchemaVersion.SchemaVersion,
MediaType: mediaType,
},
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: mediaType,
}

m.Manifests = make([]ManifestDescriptor, len(descriptors))
Expand Down Expand Up @@ -176,7 +181,14 @@ func (m *DeserializedManifestList) MarshalJSON() ([]byte, error) {
// Payload returns the raw content of the manifest list. The contents can be
// used to calculate the content identifier.
func (m DeserializedManifestList) Payload() (string, []byte, error) {
return m.MediaType, m.canonical, nil
var mediaType string
if m.MediaType == "" {
mediaType = v1.MediaTypeImageIndex
} else {
mediaType = m.MediaType
}

return mediaType, m.canonical, nil
}

// validateManifestList returns an error if the byte slice is invalid JSON or if it
Expand Down
8 changes: 3 additions & 5 deletions manifest/ocischema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -58,10 +58,8 @@ func (mb *Builder) SetMediaType(mediaType string) error {
// Build produces a final manifest from the given references.
func (mb *Builder) Build(ctx context.Context) (distribution.Manifest, error) {
m := Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 2,
MediaType: mb.mediaType,
},
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: mb.mediaType,
Layers: make([]distribution.Descriptor, len(mb.layers)),
Annotations: mb.annotations,
}
Expand Down
15 changes: 10 additions & 5 deletions manifest/ocischema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (
"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

// IndexSchemaVersion provides a pre-initialized version structure for OCI Image
// Indices.
//
// Deprecated: use [specs.Versioned] and set MediaType on the manifest
// to [v1.MediaTypeImageIndex].
var IndexSchemaVersion = manifest.Versioned{
SchemaVersion: 2,
MediaType: v1.MediaTypeImageIndex,
Expand Down Expand Up @@ -52,7 +56,10 @@ func init() {

// ImageIndex references manifests for various platforms.
type ImageIndex struct {
manifest.Versioned
specs.Versioned

// MediaType is the media type of this schema.
MediaType string `json:"mediaType,omitempty"`

// Manifests references a list of manifests
Manifests []distribution.Descriptor `json:"manifests"`
Expand Down Expand Up @@ -88,10 +95,8 @@ func FromDescriptors(descriptors []distribution.Descriptor, annotations map[stri
// fromDescriptorsWithMediaType is for testing purposes, it's useful to be able to specify the media type explicitly
func fromDescriptorsWithMediaType(descriptors []distribution.Descriptor, annotations map[string]string, mediaType string) (_ *DeserializedImageIndex, err error) {
m := ImageIndex{
Versioned: manifest.Versioned{
SchemaVersion: IndexSchemaVersion.SchemaVersion,
MediaType: mediaType,
},
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: mediaType,
Annotations: annotations,
}

Expand Down
13 changes: 10 additions & 3 deletions manifest/ocischema/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (
"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

// SchemaVersion provides a pre-initialized version structure for OCI Image
// Manifests
// Manifests.
//
// Deprecated: use [specs.Versioned] and set MediaType on the manifest
// to [v1.MediaTypeImageManifest].
var SchemaVersion = manifest.Versioned{
SchemaVersion: 2,
MediaType: v1.MediaTypeImageManifest,
Expand Down Expand Up @@ -45,7 +49,10 @@ func init() {

// Manifest defines a ocischema manifest.
type Manifest struct {
manifest.Versioned
specs.Versioned

// MediaType is the media type of this schema.
MediaType string `json:"mediaType,omitempty"`

// Config references the image configuration as a blob.
Config distribution.Descriptor `json:"config"`
Expand Down Expand Up @@ -125,7 +132,7 @@ func (m *DeserializedManifest) MarshalJSON() ([]byte, error) {

// Payload returns the raw content of the manifest. The contents can be used to
// calculate the content identifier.
func (m DeserializedManifest) Payload() (string, []byte, error) {
func (m *DeserializedManifest) Payload() (string, []byte, error) {
return v1.MediaTypeImageManifest, m.canonical, nil
}

Expand Down
8 changes: 3 additions & 5 deletions manifest/ocischema/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/manifestlist"
"github.com/opencontainers/image-spec/specs-go"

"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -42,10 +42,8 @@ const expectedManifestSerialization = `{

func makeTestManifest(mediaType string) Manifest {
return Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 2,
MediaType: mediaType,
},
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: mediaType,
Config: distribution.Descriptor{
MediaType: v1.MediaTypeImageConfig,
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
Expand Down
4 changes: 3 additions & 1 deletion manifest/schema2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/distribution/distribution/v3"
"github.com/opencontainers/image-spec/specs-go"
)

// Builder is a type for constructing manifests.
Expand Down Expand Up @@ -35,7 +36,8 @@ func NewManifestBuilder(configDescriptor distribution.Descriptor, configJSON []b
// Build produces a final manifest from the given references.
func (mb *Builder) Build(ctx context.Context) (distribution.Manifest, error) {
m := Manifest{
Versioned: SchemaVersion,
Versioned: specs.Versioned{SchemaVersion: defaultSchemaVersion},
MediaType: defaultMediaType,
Layers: make([]distribution.Descriptor, len(mb.dependencies)),
}
copy(m.Layers, mb.dependencies)
Expand Down
26 changes: 19 additions & 7 deletions manifest/schema2/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
)

const (
Expand All @@ -33,11 +34,19 @@ const (
MediaTypeUncompressedLayer = "application/vnd.docker.image.rootfs.diff.tar"
)

const (
defaultSchemaVersion = 2
defaultMediaType = MediaTypeManifest
)

// SchemaVersion provides a pre-initialized version structure for this
// packages version of the manifest.
//
// Deprecated: use [specs.Versioned] and set MediaType on the manifest
// to [MediaTypeManifest].
var SchemaVersion = manifest.Versioned{
SchemaVersion: 2,
MediaType: MediaTypeManifest,
SchemaVersion: defaultSchemaVersion,
MediaType: defaultMediaType,
}

func init() {
Expand All @@ -49,17 +58,20 @@ func init() {
}

dgst := digest.FromBytes(b)
return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: MediaTypeManifest}, err
return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: defaultMediaType}, err
}
err := distribution.RegisterManifestSchema(MediaTypeManifest, schema2Func)
err := distribution.RegisterManifestSchema(defaultMediaType, schema2Func)
if err != nil {
panic(fmt.Sprintf("Unable to register manifest: %s", err))
}
}

// Manifest defines a schema2 manifest.
type Manifest struct {
manifest.Versioned
specs.Versioned

// MediaType is the media type of this schema.
MediaType string `json:"mediaType,omitempty"`

// Config references the image configuration as a blob.
Config distribution.Descriptor `json:"config"`
Expand Down Expand Up @@ -114,9 +126,9 @@ func (m *DeserializedManifest) UnmarshalJSON(b []byte) error {
return err
}

if mfst.MediaType != MediaTypeManifest {
if mfst.MediaType != defaultMediaType {
return fmt.Errorf("mediaType in manifest should be '%s' not '%s'",
MediaTypeManifest, mfst.MediaType)
defaultMediaType, mfst.MediaType)
}

m.Manifest = mfst
Expand Down
8 changes: 3 additions & 5 deletions manifest/schema2/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/opencontainers/image-spec/specs-go"
)

const expectedManifestSerialization = `{
Expand All @@ -29,10 +29,8 @@ const expectedManifestSerialization = `{

func makeTestManifest(mediaType string) Manifest {
return Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 2,
MediaType: mediaType,
},
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: mediaType,
Config: distribution.Descriptor{
MediaType: MediaTypeImageConfig,
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
Expand Down
2 changes: 2 additions & 0 deletions manifest/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package manifest
// Versioned provides a struct with the manifest schemaVersion and mediaType.
// Incoming content with unknown schema version can be decoded against this
// struct to check the version.
//
// Deprecated: use [specs.Versioned] and set MediaType on the Manifest itself.
type Versioned struct {
// SchemaVersion is the image manifest schema that this image follows
SchemaVersion int `json:"schemaVersion"`
Expand Down
14 changes: 6 additions & 8 deletions notifications/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"testing"

"github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/schema2"
v2 "github.com/distribution/distribution/v3/registry/api/v2"
"github.com/distribution/reference"
events "github.com/docker/go-events"
"github.com/google/uuid"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand All @@ -29,7 +29,6 @@ var (
}
request = RequestRecord{}
tag = "latest"
ociMediaType = v1.MediaTypeImageManifest
artifactType = "application/vnd.example.sbom.v1"
cfg = distribution.Descriptor{
MediaType: artifactType,
Expand Down Expand Up @@ -143,14 +142,13 @@ func TestEventBridgeRepoDeleted(t *testing.T) {
}

func createTestEnv(t *testing.T, fn testSinkFn) Listener {
manifest := schema2.Manifest{
Versioned: manifest.Versioned{
MediaType: ociMediaType,
},
Config: cfg,
mfst := schema2.Manifest{
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: v1.MediaTypeImageManifest,
Config: cfg,
}

deserializedManifest, err := schema2.FromStruct(manifest)
deserializedManifest, err := schema2.FromStruct(mfst)
if err != nil {
t.Fatalf("creating OCI manifest: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion notifications/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/distribution/distribution/v3/testutil"
"github.com/distribution/reference"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
)

func TestListener(t *testing.T) {
Expand Down Expand Up @@ -143,7 +144,8 @@ func checkTestRepository(t *testing.T, repository distribution.Repository, remov
}

m := schema2.Manifest{
Versioned: schema2.SchemaVersion,
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: schema2.MediaTypeManifest,
Config: distribution.Descriptor{
MediaType: "foo/bar",
Digest: configDgst,
Expand Down
Loading

0 comments on commit 8efd133

Please sign in to comment.