diff --git a/common/libimage/copier.go b/common/libimage/copier.go index 5151343ed6..3a94dc4fe6 100644 --- a/common/libimage/copier.go +++ b/common/libimage/copier.go @@ -213,17 +213,13 @@ func getDockerAuthConfig(name, passwd, creds, idToken string) (*types.DockerAuth } } -// NewCopier is a simple, exported wrapper for newCopier -func NewCopier(options *CopyOptions, sc *types.SystemContext) (*copier, error) { - return newCopier(options, sc) -} - // newCopier creates a copier. Note that fields in options *may* overwrite the // counterparts of the specified system context. Please make sure to call // `(*copier).close()`. -func newCopier(options *CopyOptions, sc *types.SystemContext) (*copier, error) { +func (r *Runtime) newCopier(options *CopyOptions) (*copier, error) { c := copier{extendTimeoutSocket: options.extendTimeoutSocket} - c.systemContext = sc + c.systemContext = r.systemContextCopy() + if options.SourceLookupReferenceFunc != nil { c.sourceLookup = options.SourceLookupReferenceFunc } @@ -337,22 +333,14 @@ func newCopier(options *CopyOptions, sc *types.SystemContext) (*copier, error) { return &c, nil } -// newCopier creates a copier. Note that fields in options *may* overwrite the -// counterparts of the specified system context. Please make sure to call -// `(*copier).close()`. -func (r *Runtime) newCopier(options *CopyOptions) (*copier, error) { - sc := r.systemContextCopy() - return newCopier(options, sc) -} - -// Close open resources. -func (c *copier) Close() error { +// close open resources. +func (c *copier) close() error { return c.policyContext.Destroy() } -// Copy the source to the destination. Returns the bytes of the copied +// copy the source to the destination. Returns the bytes of the copied // manifest which may be used for digest computation. -func (c *copier) Copy(ctx context.Context, source, destination types.ImageReference) ([]byte, error) { +func (c *copier) copy(ctx context.Context, source, destination types.ImageReference) ([]byte, error) { logrus.Debugf("Copying source image %s to destination image %s", source.StringWithinTransport(), destination.StringWithinTransport()) // Avoid running out of time when running inside a systemd unit by diff --git a/common/libimage/import.go b/common/libimage/import.go index a03f288533..552c48eae0 100644 --- a/common/libimage/import.go +++ b/common/libimage/import.go @@ -108,9 +108,9 @@ func (r *Runtime) Import(ctx context.Context, path string, options *ImportOption if err != nil { return "", err } - defer c.Close() + defer c.close() - if _, err := c.Copy(ctx, srcRef, destRef); err != nil { + if _, err := c.copy(ctx, srcRef, destRef); err != nil { return "", err } diff --git a/common/libimage/manifest_list.go b/common/libimage/manifest_list.go index a23315da3c..5c6bbd8395 100644 --- a/common/libimage/manifest_list.go +++ b/common/libimage/manifest_list.go @@ -796,7 +796,7 @@ func (m *ManifestList) Push(ctx context.Context, destination string, options *Ma if err != nil { return "", err } - defer copier.Close() + defer copier.close() pushOptions := manifests.PushOptions{ AddCompression: options.AddCompression, diff --git a/common/libimage/pull.go b/common/libimage/pull.go index c4ad5df0c7..3db1b2992b 100644 --- a/common/libimage/pull.go +++ b/common/libimage/pull.go @@ -235,7 +235,7 @@ func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference, if err != nil { return nil, err } - defer c.Close() + defer c.close() // Figure out a name for the storage destination. var storageName, imageName string @@ -321,7 +321,7 @@ func (r *Runtime) copyFromDefault(ctx context.Context, ref types.ImageReference, return nil, fmt.Errorf("parsing %q: %w", storageName, err) } - _, err = c.Copy(ctx, ref, destRef) + _, err = c.copy(ctx, ref, destRef) return []string{imageName}, err } @@ -391,7 +391,7 @@ func (r *Runtime) copyFromDockerArchiveReaderReference(ctx context.Context, read if err != nil { return nil, err } - defer c.Close() + defer c.close() // Get a slice of storage references we can copy. references, destNames, err := r.storageReferencesReferencesFromArchiveReader(ctx, readerRef, reader) @@ -401,7 +401,7 @@ func (r *Runtime) copyFromDockerArchiveReaderReference(ctx context.Context, read // Now copy all of the images. Use readerRef for performance. for _, destRef := range references { - if _, err := c.Copy(ctx, readerRef, destRef); err != nil { + if _, err := c.copy(ctx, readerRef, destRef); err != nil { return nil, err } } @@ -640,7 +640,7 @@ func (r *Runtime) copySingleImageFromRegistry(ctx context.Context, imageName str if err != nil { return nil, err } - defer c.Close() + defer c.close() var pullErrors []error for _, candidate := range resolved.PullCandidates { @@ -678,7 +678,7 @@ func (r *Runtime) copySingleImageFromRegistry(ctx context.Context, imageName str } } var manifestBytes []byte - if manifestBytes, err = c.Copy(ctx, srcRef, destRef); err != nil { + if manifestBytes, err = c.copy(ctx, srcRef, destRef); err != nil { logrus.Debugf("Error pulling candidate %s: %v", candidateString, err) pullErrors = append(pullErrors, err) continue diff --git a/common/libimage/push.go b/common/libimage/push.go index 5db6cfbcfe..f89b8fc070 100644 --- a/common/libimage/push.go +++ b/common/libimage/push.go @@ -114,7 +114,7 @@ func (r *Runtime) Push(ctx context.Context, source, destination string, options return nil, err } - defer c.Close() + defer c.close() - return c.Copy(ctx, srcRef, destRef) + return c.copy(ctx, srcRef, destRef) } diff --git a/common/libimage/save.go b/common/libimage/save.go index 46529d10f3..62cad3288d 100644 --- a/common/libimage/save.go +++ b/common/libimage/save.go @@ -123,9 +123,9 @@ func (r *Runtime) saveSingleImage(ctx context.Context, name, format, path string if err != nil { return err } - defer c.Close() + defer c.close() - _, err = c.Copy(ctx, srcRef, destRef) + _, err = c.copy(ctx, srcRef, destRef) return err } @@ -208,7 +208,7 @@ func (r *Runtime) saveDockerArchive(ctx context.Context, names []string, path st if err != nil { return err } - defer c.Close() + defer c.close() destRef, err := writer.NewReference(nil) if err != nil { @@ -220,7 +220,7 @@ func (r *Runtime) saveDockerArchive(ctx context.Context, names []string, path st return err } - if _, err := c.Copy(ctx, srcRef, destRef); err != nil { + if _, err := c.copy(ctx, srcRef, destRef); err != nil { return err } }