Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export portions of Copier #2164

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions libimage/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,17 @@ func getDockerAuthConfig(name, passwd, creds, idToken string) (*types.DockerAuth
}
}

// NewCopier is a simple, exported wrapper for newCopier
baude marked this conversation as resolved.
Show resolved Hide resolved
func NewCopier(options *CopyOptions, sc *types.SystemContext) (*copier, error) {
baude marked this conversation as resolved.
Show resolved Hide resolved
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()`.
baude marked this conversation as resolved.
Show resolved Hide resolved
func (r *Runtime) newCopier(options *CopyOptions) (*copier, error) {
func newCopier(options *CopyOptions, sc *types.SystemContext) (*copier, error) {
baude marked this conversation as resolved.
Show resolved Hide resolved
c := copier{extendTimeoutSocket: options.extendTimeoutSocket}
c.systemContext = r.systemContextCopy()

c.systemContext = sc
baude marked this conversation as resolved.
Show resolved Hide resolved
if options.SourceLookupReferenceFunc != nil {
c.sourceLookup = options.SourceLookupReferenceFunc
}
Expand Down Expand Up @@ -325,14 +329,22 @@ func (r *Runtime) newCopier(options *CopyOptions) (*copier, error) {
return &c, nil
}

// close open resources.
func (c *copier) close() error {
// 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) {
baude marked this conversation as resolved.
Show resolved Hide resolved
sc := r.systemContextCopy()
return newCopier(options, sc)
}

// 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
Expand Down
4 changes: 2 additions & 2 deletions libimage/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion libimage/manifest_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,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,
Expand Down
12 changes: 6 additions & 6 deletions libimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libimage/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions libimage/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down