1
0
mirror of https://github.com/containers/image.git synced 2025-04-18 19:44:05 +03:00

Implement private.ImageSource in non-forwarding transports

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č <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač 2022-07-02 02:06:00 +02:00
parent 6f22578367
commit 592371c8be
17 changed files with 94 additions and 9 deletions

View File

@ -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_

View File

@ -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) {

View File

@ -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 {

View File

@ -0,0 +1,5 @@
package archive
import "github.com/containers/image/v5/internal/private"
var _ private.ImageSource = (*archiveImageSource)(nil)

View File

@ -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")

View File

@ -0,0 +1,5 @@
package daemon
import "github.com/containers/image/v5/internal/private"
var _ private.ImageSource = (*daemonImageSource)(nil)

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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)

View File

@ -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,

5
sif/src_test.go Normal file
View File

@ -0,0 +1,5 @@
package sif
import "github.com/containers/image/v5/internal/private"
var _ private.ImageSource = (*sifImageSource)(nil)

View File

@ -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,

View File

@ -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{}
)

View File

@ -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,

View File

@ -0,0 +1,5 @@
package tarball
import "github.com/containers/image/v5/internal/private"
var _ private.ImageSource = (*tarballImageSource)(nil)