Skip to content

Commit

Permalink
manifests,push: add support for ReplicateWithCompression
Browse files Browse the repository at this point in the history
`c/image` recently added support for EnsureCompressionVariantsExist,
following PR exposes the feature to `c/common` manifests so actual users
can use it.

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed Jul 26, 2023
1 parent 94b250a commit 53146f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libimage/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/image"
"github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/pkg/compression"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/signature/signer"
is "github.com/containers/image/v5/storage"
Expand Down Expand Up @@ -70,6 +71,7 @@ type PushOptions struct {
RemoveSignatures bool // true to discard signatures in images
ManifestType string // the format to use when saving the list - possible options are oci, v2s1, and v2s2
SourceFilter LookupReferenceFunc // filter the list source
ReplicateWithCompression []string // replicate required imges with requested compression algorithms
}

// Create creates a new list containing information about the specified image,
Expand Down Expand Up @@ -239,6 +241,7 @@ func (l *list) Push(ctx context.Context, dest types.ImageReference, options Push
return nil, "", err
}
}
compressionVariant := prepareReplicateWithCompression(option.ReplicateWithCompression)
copyOptions := &cp.Options{
ImageListSelection: options.ImageListSelection,
Instances: options.Instances,
Expand All @@ -252,6 +255,7 @@ func (l *list) Push(ctx context.Context, dest types.ImageReference, options Push
SignBySigstorePrivateKeyFile: options.SignBySigstorePrivateKeyFile,
SignSigstorePrivateKeyPassphrase: options.SignSigstorePrivateKeyPassphrase,
ForceManifestMIMEType: singleImageManifestType,
EnsureCompressionVariantExists: compressionVariants,
}

// Copy whatever we were asked to copy.
Expand All @@ -266,6 +270,19 @@ func (l *list) Push(ctx context.Context, dest types.ImageReference, options Push
return nil, manifestDigest, nil
}

func prepareReplicateWithCompression(variants []string) ([]OptionCompressionVariant, error) {
res := []cp.OptionCompressionVariant{}
for _, compressionVariant := range variants {
algo, err := compression.AlgorithmByName(name)
if err != nil {
return nil, fmt.Errorf("requested algorithm %s is not supported for replication: %w", err)
}
variant := cp.CopyOptionCompressionVariant{Algorithm: algo}
res = append(res, cp.OptionCompressionVariant{Algorithm: algo})
}
return res, nil
}

// Add adds information about the specified image to the list, computing the
// image's manifest's digest, retrieving OS and architecture information from
// the image's configuration, and recording the image's reference so that it
Expand Down
4 changes: 4 additions & 0 deletions libimage/manifests/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,8 @@ func TestPush(t *testing.T) {
options.Instances = append(options.Instances, otherListDigest)
_, _, err = list.Push(ctx, destRef, options)
assert.Nilf(t, err, "list.Push(four specified)")

options.ReplicateWithCompression = []string{"zstd"}
_, _, err = list.Push(ctx, destRef, options)
assert.Nilf(t, err, "list.Push(with replication for zstd specified)")
}

0 comments on commit 53146f9

Please sign in to comment.