Skip to content

Commit

Permalink
libimage/copier: wire ForceCompressionFormat for image copy
Browse files Browse the repository at this point in the history
Implement containers/image#2068 for
libimage/copier.

Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed Aug 8, 2023
1 parent 16ed6ef commit a1478d1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libimage/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type CopyOptions struct {
CompressionFormat *compression.Algorithm
// CompressionLevel specifies what compression level is used
CompressionLevel *int
// ForceCompressionFormat ensures that the compression algorithm set in
// DestinationCtx.CompressionFormat is used exclusively, and blobs of other
// compression algorithms are not reused.
ForceCompressionFormat bool

// containers-auth.json(5) file to use when authenticating against
// container registries.
Expand Down Expand Up @@ -294,6 +298,7 @@ func (r *Runtime) newCopier(options *CopyOptions) (*copier, error) {
c.imageCopyOptions.ProgressInterval = time.Second
}

c.imageCopyOptions.ForceCompressionFormat = options.ForceCompressionFormat
c.imageCopyOptions.ForceManifestMIMEType = options.ManifestMIMEType
c.imageCopyOptions.SourceCtx = c.systemContext
c.imageCopyOptions.DestinationCtx = c.systemContext
Expand Down
30 changes: 30 additions & 0 deletions libimage/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/containers/common/pkg/config"
"github.com/containers/image/v5/pkg/compression"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -96,3 +97,32 @@ func TestPushOtherPlatform(t *testing.T) {
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "docker-archive:"+tmp.Name(), pushOptions)
require.NoError(t, err)
}

func TestPushWithForceCompression(t *testing.T) {
runtime, cleanup := testNewRuntime(t)
defer cleanup()
ctx := context.Background()

// Prefetch alpine.
pullOptions := &PullOptions{}
pullOptions.Writer = os.Stdout
pullOptions.Architecture = "arm64"
pulledImages, err := runtime.Pull(ctx, "docker.io/library/alpine:latest", config.PullPolicyAlways, pullOptions)
require.NoError(t, err)
require.Len(t, pulledImages, 1)

data, err := pulledImages[0].Inspect(ctx, nil)
require.NoError(t, err)
require.Equal(t, "arm64", data.Architecture)

pushOptions := &PushOptions{}
pushOptions.CompressionFormat = &compression.Gzip
pushOptions.ForceCompressionFormat = true
pushOptions.Writer = os.Stdout
tmp, err := os.CreateTemp("", "")
require.NoError(t, err)
tmp.Close()
defer os.Remove(tmp.Name())
_, err = runtime.Push(ctx, "docker.io/library/alpine:latest", "docker-archive:"+tmp.Name(), pushOptions)
require.NoError(t, err)
}

0 comments on commit a1478d1

Please sign in to comment.