From 592371c8befe1221d6a6a257da6e9dee06593908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 2 Jul 2022 02:06:00 +0200 Subject: [PATCH] Implement private.ImageSource in non-forwarding transports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This sets up the precedent that all transports should primarily implement the private interface; that will allow us to make future changes to the private interface easier, because we can just change the public interface wrappers in a single place instead of modifying transports - especially as more stubs are added soonish. Should not change behavior. Signed-off-by: Miloslav Trmač --- directory/directory_src.go | 12 ++++++++++-- directory/directory_test.go | 1 + docker/archive/src.go | 3 ++- docker/archive/src_test.go | 5 +++++ docker/daemon/daemon_src.go | 3 ++- docker/daemon/daemon_src_test.go | 5 +++++ docker/internal/tarfile/src.go | 5 +++++ oci/layout/oci_src.go | 15 +++++++++++++-- oci/layout/oci_src_test.go | 3 +++ ostree/ostree_src.go | 14 ++++++++++++-- ostree/ostree_src_test.go | 8 ++++++++ sif/src.go | 8 +++++++- sif/src_test.go | 5 +++++ storage/storage_src.go | 5 +++++ storage/storage_test.go | 1 + tarball/tarball_src.go | 5 +++++ tarball/tarball_src_test.go | 5 +++++ 17 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 docker/archive/src_test.go create mode 100644 docker/daemon/daemon_src_test.go create mode 100644 ostree/ostree_src_test.go create mode 100644 sif/src_test.go create mode 100644 tarball/tarball_src_test.go diff --git a/directory/directory_src.go b/directory/directory_src.go index 8b509112..d680d04e 100644 --- a/directory/directory_src.go +++ b/directory/directory_src.go @@ -5,19 +5,27 @@ import ( "io" "os" + "github.com/containers/image/v5/internal/imagesource/stubs" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/types" "github.com/opencontainers/go-digest" ) type dirImageSource struct { + stubs.NoGetBlobAtInitialize + ref dirReference } // newImageSource returns an ImageSource reading from an existing directory. // The caller must call .Close() on the returned ImageSource. -func newImageSource(ref dirReference) types.ImageSource { - return &dirImageSource{ref} +func newImageSource(ref dirReference) private.ImageSource { + return &dirImageSource{ + NoGetBlobAtInitialize: stubs.NoGetBlobAt(ref), + + ref: ref, + } } // Reference returns the reference used to set up this source, _as specified by the user_ diff --git a/directory/directory_test.go b/directory/directory_test.go index c75ebf13..c9154561 100644 --- a/directory/directory_test.go +++ b/directory/directory_test.go @@ -17,6 +17,7 @@ import ( "github.com/stretchr/testify/require" ) +var _ private.ImageSource = (*dirImageSource)(nil) var _ private.ImageDestination = (*dirImageDestination)(nil) func TestDestinationReference(t *testing.T) { diff --git a/docker/archive/src.go b/docker/archive/src.go index 799d2470..5604a512 100644 --- a/docker/archive/src.go +++ b/docker/archive/src.go @@ -4,6 +4,7 @@ import ( "context" "github.com/containers/image/v5/docker/internal/tarfile" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/types" ) @@ -14,7 +15,7 @@ type archiveImageSource struct { // newImageSource returns a types.ImageSource for the specified image reference. // The caller must call .Close() on the returned ImageSource. -func newImageSource(ctx context.Context, sys *types.SystemContext, ref archiveReference) (types.ImageSource, error) { +func newImageSource(ctx context.Context, sys *types.SystemContext, ref archiveReference) (private.ImageSource, error) { var archive *tarfile.Reader var closeArchive bool if ref.archiveReader != nil { diff --git a/docker/archive/src_test.go b/docker/archive/src_test.go new file mode 100644 index 00000000..b98abea7 --- /dev/null +++ b/docker/archive/src_test.go @@ -0,0 +1,5 @@ +package archive + +import "github.com/containers/image/v5/internal/private" + +var _ private.ImageSource = (*archiveImageSource)(nil) diff --git a/docker/daemon/daemon_src.go b/docker/daemon/daemon_src.go index 7b46e03d..754e7ef2 100644 --- a/docker/daemon/daemon_src.go +++ b/docker/daemon/daemon_src.go @@ -4,6 +4,7 @@ import ( "context" "github.com/containers/image/v5/docker/internal/tarfile" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/types" perrors "github.com/pkg/errors" ) @@ -22,7 +23,7 @@ type daemonImageSource struct { // (We could, perhaps, expect an exact sequence, assume that the first plaintext file // is the config, and that the following len(RootFS) files are the layers, but that feels // way too brittle.) -func newImageSource(ctx context.Context, sys *types.SystemContext, ref daemonReference) (types.ImageSource, error) { +func newImageSource(ctx context.Context, sys *types.SystemContext, ref daemonReference) (private.ImageSource, error) { c, err := newDockerClient(sys) if err != nil { return nil, perrors.Wrap(err, "initializing docker engine client") diff --git a/docker/daemon/daemon_src_test.go b/docker/daemon/daemon_src_test.go new file mode 100644 index 00000000..7214ce70 --- /dev/null +++ b/docker/daemon/daemon_src_test.go @@ -0,0 +1,5 @@ +package daemon + +import "github.com/containers/image/v5/internal/private" + +var _ private.ImageSource = (*daemonImageSource)(nil) diff --git a/docker/internal/tarfile/src.go b/docker/internal/tarfile/src.go index 35ef3fbd..627f14c2 100644 --- a/docker/internal/tarfile/src.go +++ b/docker/internal/tarfile/src.go @@ -13,6 +13,7 @@ import ( "sync" "github.com/containers/image/v5/docker/reference" + "github.com/containers/image/v5/internal/imagesource/stubs" "github.com/containers/image/v5/internal/iolimits" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/pkg/compression" @@ -23,6 +24,8 @@ import ( // Source is a partial implementation of types.ImageSource for reading from tarPath. type Source struct { + stubs.NoGetBlobAtInitialize + archive *Reader closeArchive bool // .Close() the archive when the source is closed. // If ref is nil and sourceIndex is -1, indicates the only image in the archive. @@ -50,6 +53,8 @@ type layerInfo struct { // The archive will be closed if closeArchive func NewSource(archive *Reader, closeArchive bool, transportName string, ref reference.NamedTagged, sourceIndex int) *Source { return &Source{ + NoGetBlobAtInitialize: stubs.NoGetBlobAtRaw(transportName), + archive: archive, closeArchive: closeArchive, ref: ref, diff --git a/oci/layout/oci_src.go b/oci/layout/oci_src.go index b78c2356..526dfbc7 100644 --- a/oci/layout/oci_src.go +++ b/oci/layout/oci_src.go @@ -9,6 +9,8 @@ import ( "os" "strconv" + "github.com/containers/image/v5/internal/imagesource/stubs" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/pkg/tlsclientconfig" "github.com/containers/image/v5/types" @@ -19,6 +21,8 @@ import ( ) type ociImageSource struct { + stubs.NoGetBlobAtInitialize + ref ociReference index *imgspecv1.Index descriptor imgspecv1.Descriptor @@ -27,7 +31,7 @@ type ociImageSource struct { } // newImageSource returns an ImageSource for reading from an existing directory. -func newImageSource(sys *types.SystemContext, ref ociReference) (types.ImageSource, error) { +func newImageSource(sys *types.SystemContext, ref ociReference) (private.ImageSource, error) { tr := tlsclientconfig.NewTransport() tr.TLSClientConfig = tlsconfig.ServerDefault() @@ -48,7 +52,14 @@ func newImageSource(sys *types.SystemContext, ref ociReference) (types.ImageSour if err != nil { return nil, err } - d := &ociImageSource{ref: ref, index: index, descriptor: descriptor, client: client} + d := &ociImageSource{ + NoGetBlobAtInitialize: stubs.NoGetBlobAt(ref), + + ref: ref, + index: index, + descriptor: descriptor, + client: client, + } if sys != nil { // TODO(jonboulle): check dir existence? d.sharedBlobDir = sys.OCISharedBlobDirPath diff --git a/oci/layout/oci_src_test.go b/oci/layout/oci_src_test.go index 025d1555..b52e64e3 100644 --- a/oci/layout/oci_src_test.go +++ b/oci/layout/oci_src_test.go @@ -12,6 +12,7 @@ import ( "strings" "testing" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/pkg/blobinfocache/memory" "github.com/containers/image/v5/types" digest "github.com/opencontainers/go-digest" @@ -19,6 +20,8 @@ import ( "github.com/stretchr/testify/require" ) +var _ private.ImageSource = (*ociImageSource)(nil) + const RemoteLayerContent = "This is the remote layer content" var httpServerAddr string diff --git a/ostree/ostree_src.go b/ostree/ostree_src.go index 4b4e7a0e..0df05a3a 100644 --- a/ostree/ostree_src.go +++ b/ostree/ostree_src.go @@ -14,6 +14,8 @@ import ( "strings" "unsafe" + "github.com/containers/image/v5/internal/imagesource/stubs" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/types" "github.com/containers/storage/pkg/ioutils" @@ -34,6 +36,8 @@ import ( import "C" type ostreeImageSource struct { + stubs.NoGetBlobAtInitialize + ref ostreeReference tmpDir string repo *C.struct_OstreeRepo @@ -42,8 +46,14 @@ type ostreeImageSource struct { } // newImageSource returns an ImageSource for reading from an existing directory. -func newImageSource(tmpDir string, ref ostreeReference) (types.ImageSource, error) { - return &ostreeImageSource{ref: ref, tmpDir: tmpDir, compressed: nil}, nil +func newImageSource(tmpDir string, ref ostreeReference) (private.ImageSource, error) { + return &ostreeImageSource{ + NoGetBlobAtInitialize: stubs.NoGetBlobAt(ref), + + ref: ref, + tmpDir: tmpDir, + compressed: nil, + }, nil } // Reference returns the reference used to set up this source. diff --git a/ostree/ostree_src_test.go b/ostree/ostree_src_test.go new file mode 100644 index 00000000..60c35e69 --- /dev/null +++ b/ostree/ostree_src_test.go @@ -0,0 +1,8 @@ +//go:build containers_image_ostree +// +build containers_image_ostree + +package ostree + +import "github.com/containers/image/v5/internal/private" + +var _ private.ImageSource = (*ostreeImageSource)(nil) diff --git a/sif/src.go b/sif/src.go index ccf12596..2ed8a3c6 100644 --- a/sif/src.go +++ b/sif/src.go @@ -9,6 +9,8 @@ import ( "io" "os" + "github.com/containers/image/v5/internal/imagesource/stubs" + "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/internal/tmpdir" "github.com/containers/image/v5/types" "github.com/opencontainers/go-digest" @@ -19,6 +21,8 @@ import ( ) type sifImageSource struct { + stubs.NoGetBlobAtInitialize + ref sifReference workDir string layerDigest digest.Digest @@ -55,7 +59,7 @@ func getBlobInfo(path string) (digest.Digest, int64, error) { // newImageSource returns an ImageSource for reading from an existing directory. // newImageSource extracts SIF objects and saves them in a temp directory. -func newImageSource(ctx context.Context, sys *types.SystemContext, ref sifReference) (types.ImageSource, error) { +func newImageSource(ctx context.Context, sys *types.SystemContext, ref sifReference) (private.ImageSource, error) { sifImg, err := sif.LoadContainerFromPath(ref.file, sif.OptLoadWithFlag(os.O_RDONLY)) if err != nil { return nil, fmt.Errorf("loading SIF file: %w", err) @@ -137,6 +141,8 @@ func newImageSource(ctx context.Context, sys *types.SystemContext, ref sifRefere succeeded = true return &sifImageSource{ + NoGetBlobAtInitialize: stubs.NoGetBlobAt(ref), + ref: ref, workDir: workDir, layerDigest: layerDigest, diff --git a/sif/src_test.go b/sif/src_test.go new file mode 100644 index 00000000..8402e23b --- /dev/null +++ b/sif/src_test.go @@ -0,0 +1,5 @@ +package sif + +import "github.com/containers/image/v5/internal/private" + +var _ private.ImageSource = (*sifImageSource)(nil) diff --git a/storage/storage_src.go b/storage/storage_src.go index 93d1f5e2..bb674cc1 100644 --- a/storage/storage_src.go +++ b/storage/storage_src.go @@ -15,6 +15,7 @@ import ( "github.com/containers/image/v5/docker/reference" "github.com/containers/image/v5/internal/image" + "github.com/containers/image/v5/internal/imagesource/stubs" "github.com/containers/image/v5/internal/tmpdir" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/types" @@ -28,6 +29,8 @@ import ( ) type storageImageSource struct { + stubs.NoGetBlobAtInitialize + imageRef storageReference image *storage.Image systemContext *types.SystemContext // SystemContext used in GetBlob() to create temporary files @@ -48,6 +51,8 @@ func newImageSource(ctx context.Context, sys *types.SystemContext, imageRef stor // Build the reader object. image := &storageImageSource{ + NoGetBlobAtInitialize: stubs.NoGetBlobAt(imageRef), + imageRef: imageRef, systemContext: sys, image: img, diff --git a/storage/storage_test.go b/storage/storage_test.go index ef69399d..6bbab68e 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -39,6 +39,7 @@ var ( _ types.ImageDestination = &storageImageDestination{} _ private.ImageDestination = (*storageImageDestination)(nil) _ types.ImageSource = &storageImageSource{} + _ private.ImageSource = (*storageImageSource)(nil) _ types.ImageReference = &storageReference{} _ types.ImageTransport = &storageTransport{} ) diff --git a/tarball/tarball_src.go b/tarball/tarball_src.go index aedfdf5d..a5c4fa84 100644 --- a/tarball/tarball_src.go +++ b/tarball/tarball_src.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/containers/image/v5/internal/imagesource/stubs" "github.com/containers/image/v5/types" "github.com/klauspost/pgzip" digest "github.com/opencontainers/go-digest" @@ -19,6 +20,8 @@ import ( ) type tarballImageSource struct { + stubs.NoGetBlobAtInitialize + reference tarballReference filenames []string diffIDs []digest.Digest @@ -185,6 +188,8 @@ func (r *tarballReference) NewImageSource(ctx context.Context, sys *types.System // Return the image. src := &tarballImageSource{ + NoGetBlobAtInitialize: stubs.NoGetBlobAt(r), + reference: *r, filenames: filenames, diffIDs: diffIDs, diff --git a/tarball/tarball_src_test.go b/tarball/tarball_src_test.go new file mode 100644 index 00000000..5d9f48cf --- /dev/null +++ b/tarball/tarball_src_test.go @@ -0,0 +1,5 @@ +package tarball + +import "github.com/containers/image/v5/internal/private" + +var _ private.ImageSource = (*tarballImageSource)(nil)