Skip to content

Commit

Permalink
Add internal/imagesource/impl.NoSignatures, use it to reduce boilerplate
Browse files Browse the repository at this point in the history
Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Jul 2, 2022
1 parent a3a8da6 commit b283913
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 40 deletions.
12 changes: 1 addition & 11 deletions docker/internal/tarfile/src.go
Expand Up @@ -26,6 +26,7 @@ import (
// Source is a partial implementation of types.ImageSource for reading from tarPath.
type Source struct {
impl.PropertyMethodsInitialize
impl.NoSignatures
impl.DoesNotAffectLayerInfosForCopy
stubs.NoGetBlobAtInitialize

Expand Down Expand Up @@ -314,14 +315,3 @@ func (s *Source) GetBlob(ctx context.Context, info types.BlobInfo, cache types.B

return nil, 0, fmt.Errorf("Unknown blob %s", info.Digest)
}

// GetSignatures returns the image's signatures. It may use a remote (= slow) service.
// This source implementation does not support manifest lists, so the passed-in instanceDigest should always be nil,
// as there can be no secondary manifests.
func (s *Source) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
if instanceDigest != nil {
// How did we even get here? GetManifest(ctx, nil) has returned a manifest.DockerV2Schema2MediaType.
return nil, errors.New(`Manifest lists are not supported by "docker-daemon:"`)
}
return [][]byte{}, nil
}
18 changes: 18 additions & 0 deletions internal/imagesource/impl/signatures.go
@@ -0,0 +1,18 @@
package impl

import (
"context"

"github.com/opencontainers/go-digest"
)

// NoSignatures implements GetSignatures() that returns nothing.
type NoSignatures struct{}

// GetSignatures returns the image's signatures. It may use a remote (= slow) service.
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for
// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list
// (e.g. if the source never returns manifest lists).
func (stub NoSignatures) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
return nil, nil
}
9 changes: 1 addition & 8 deletions oci/layout/oci_src.go
Expand Up @@ -23,6 +23,7 @@ import (

type ociImageSource struct {
impl.PropertyMethodsInitialize
impl.NoSignatures
impl.DoesNotAffectLayerInfosForCopy
stubs.NoGetBlobAtInitialize

Expand Down Expand Up @@ -150,14 +151,6 @@ func (s *ociImageSource) GetBlob(ctx context.Context, info types.BlobInfo, cache
return r, fi.Size(), nil
}

// GetSignatures returns the image's signatures. It may use a remote (= slow) service.
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for
// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list
// (e.g. if the source never returns manifest lists).
func (s *ociImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
return [][]byte{}, nil
}

// getExternalBlob returns the reader of the first available blob URL from urls, which must not be empty.
// This function can return nil reader when no url is supported by this function. In this case, the caller
// should fallback to fetch the non-external blob (i.e. pull from the registry).
Expand Down
12 changes: 1 addition & 11 deletions sif/src.go
Expand Up @@ -23,6 +23,7 @@ import (

type sifImageSource struct {
impl.PropertyMethodsInitialize
impl.NoSignatures
impl.DoesNotAffectLayerInfosForCopy
stubs.NoGetBlobAtInitialize

Expand Down Expand Up @@ -198,14 +199,3 @@ func (s *sifImageSource) GetManifest(ctx context.Context, instanceDigest *digest
}
return s.manifest, imgspecv1.MediaTypeImageManifest, nil
}

// GetSignatures returns the image's signatures. It may use a remote (= slow) service.
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for
// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list
// (e.g. if the source never returns manifest lists).
func (s *sifImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
if instanceDigest != nil {
return nil, errors.New("manifest lists are not supported by the sif transport")
}
return nil, nil
}
11 changes: 1 addition & 10 deletions tarball/tarball_src.go
Expand Up @@ -22,6 +22,7 @@ import (

type tarballImageSource struct {
impl.PropertyMethodsInitialize
impl.NoSignatures
impl.DoesNotAffectLayerInfosForCopy
stubs.NoGetBlobAtInitialize

Expand Down Expand Up @@ -252,16 +253,6 @@ func (is *tarballImageSource) GetManifest(ctx context.Context, instanceDigest *d
return is.manifest, imgspecv1.MediaTypeImageManifest, nil
}

// GetSignatures returns the image's signatures. It may use a remote (= slow) service.
// This source implementation does not support manifest lists, so the passed-in instanceDigest should always be nil,
// as there can be no secondary manifests.
func (*tarballImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
if instanceDigest != nil {
return nil, fmt.Errorf("manifest lists are not supported by the %q transport", transportName)
}
return nil, nil
}

func (is *tarballImageSource) Reference() types.ImageReference {
return &is.reference
}

0 comments on commit b283913

Please sign in to comment.