From 973bfbb676963aacb6c114943e461a18b74dfe24 Mon Sep 17 00:00:00 2001 From: Bracken Dawson Date: Fri, 21 Apr 2023 14:43:21 +0100 Subject: [PATCH] Fix Go Idioms - DRY out SchemaVersion literals - Better name the predefined Versioned struct for the Image Index - Var names, declarations, else cases. Co-authored-by: Milos Gajdos Signed-off-by: Bracken Dawson --- manifest/manifestlist/manifestlist.go | 2 +- manifest/ocischema/index.go | 23 ++++++++++------------- manifest/ocischema/manifest.go | 6 +++--- registry/storage/manifestlisthandler.go | 8 +++++--- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/manifest/manifestlist/manifestlist.go b/manifest/manifestlist/manifestlist.go index 41f49775c..25ffbe525 100644 --- a/manifest/manifestlist/manifestlist.go +++ b/manifest/manifestlist/manifestlist.go @@ -129,7 +129,7 @@ func FromDescriptors(descriptors []ManifestDescriptor) (*DeserializedManifestLis func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, mediaType string) (*DeserializedManifestList, error) { m := ManifestList{ Versioned: manifest.Versioned{ - SchemaVersion: 2, + SchemaVersion: SchemaVersion.SchemaVersion, MediaType: mediaType, }, } diff --git a/manifest/ocischema/index.go b/manifest/ocischema/index.go index 16b7b6873..55daaa881 100644 --- a/manifest/ocischema/index.go +++ b/manifest/ocischema/index.go @@ -11,9 +11,9 @@ import ( v1 "github.com/opencontainers/image-spec/specs-go/v1" ) -// OCISchemaVersion provides a pre-initialized version structure for this -// packages OCIschema version of the manifest. -var OCISchemaVersion = manifest.Versioned{ +// IndexSchemaVersion provides a pre-initialized version structure for OCI Image +// Indices. +var IndexSchemaVersion = manifest.Versioned{ SchemaVersion: 2, MediaType: v1.MediaTypeImageIndex, } @@ -69,13 +69,13 @@ type ImageIndex struct { // References returns the distribution descriptors for the referenced image // manifests. func (ii ImageIndex) References() []distribution.Descriptor { - dependencies := make([]distribution.Descriptor, len(ii.Manifests)) + references := make([]distribution.Descriptor, len(ii.Manifests)) for i := range ii.Manifests { - dependencies[i] = ii.Manifests[i].Descriptor - dependencies[i].Platform = ii.Manifests[i].Platform + references[i] = ii.Manifests[i].Descriptor + references[i].Platform = ii.Manifests[i].Platform } - return dependencies + return references } // DeserializedImageIndex wraps ManifestList with a copy of the original @@ -96,10 +96,10 @@ func FromDescriptors(descriptors []ManifestDescriptor, annotations map[string]st } // fromDescriptorsWithMediaType is for testing purposes, it's useful to be able to specify the media type explicitly -func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations map[string]string, mediaType string) (*DeserializedImageIndex, error) { +func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations map[string]string, mediaType string) (_ *DeserializedImageIndex, err error) { m := ImageIndex{ Versioned: manifest.Versioned{ - SchemaVersion: 2, + SchemaVersion: IndexSchemaVersion.SchemaVersion, MediaType: mediaType, }, Annotations: annotations, @@ -112,7 +112,6 @@ func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations ImageIndex: m, } - var err error deserialized.canonical, err = json.MarshalIndent(&m, "", " ") return &deserialized, err } @@ -147,11 +146,9 @@ func (m *DeserializedImageIndex) MarshalJSON() ([]byte, error) { // Payload returns the raw content of the manifest list. The contents can be // used to calculate the content identifier. func (m DeserializedImageIndex) Payload() (string, []byte, error) { - var mediaType string + mediaType := m.MediaType if m.MediaType == "" { mediaType = v1.MediaTypeImageIndex - } else { - mediaType = m.MediaType } return mediaType, m.canonical, nil diff --git a/manifest/ocischema/manifest.go b/manifest/ocischema/manifest.go index b5133e84e..973cd64ae 100644 --- a/manifest/ocischema/manifest.go +++ b/manifest/ocischema/manifest.go @@ -11,10 +11,10 @@ import ( v1 "github.com/opencontainers/image-spec/specs-go/v1" ) -// SchemaVersion provides a pre-initialized version structure for this -// packages version of the manifest. +// SchemaVersion provides a pre-initialized version structure for OCI Image +// Manifests var SchemaVersion = manifest.Versioned{ - SchemaVersion: 2, // historical value here.. does not pertain to OCI or docker version + SchemaVersion: 2, MediaType: v1.MediaTypeImageManifest, } diff --git a/registry/storage/manifestlisthandler.go b/registry/storage/manifestlisthandler.go index 1c27bccfd..1fc7aac7a 100644 --- a/registry/storage/manifestlisthandler.go +++ b/registry/storage/manifestlisthandler.go @@ -34,17 +34,19 @@ func (ms *manifestListHandler) Unmarshal(ctx context.Context, dgst digest.Digest func (ms *manifestListHandler) Put(ctx context.Context, manifestList distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) { dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Put") - var schemaVersion int + var schemaVersion, expectedSchemaVersion int switch m := manifestList.(type) { case *manifestlist.DeserializedManifestList: + expectedSchemaVersion = manifestlist.SchemaVersion.SchemaVersion schemaVersion = m.SchemaVersion case *ocischema.DeserializedImageIndex: + expectedSchemaVersion = ocischema.IndexSchemaVersion.SchemaVersion schemaVersion = m.SchemaVersion default: return "", fmt.Errorf("wrong type put to manifestListHandler: %T", manifestList) } - if schemaVersion != 2 { - return "", fmt.Errorf("unrecognized manifest list schema version %d", schemaVersion) + if schemaVersion != expectedSchemaVersion { + return "", fmt.Errorf("unrecognized manifest list schema version %d, expected %d", schemaVersion, expectedSchemaVersion) } if err := ms.verifyManifest(ms.ctx, manifestList, skipDependencyVerification); err != nil {