From 474b858bf511617237f930353566beca9a83f42b Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Tue, 2 May 2023 17:11:25 -0400 Subject: [PATCH] copy.Options: add an InstancePlatforms field Add an InstancePlatforms field to the copy.Options structure. When asked to copy only a specific subset of instances, combine the Instances list with the instances which match the platforms given in the InstancePlatforms list, returning an error if we're unable to find an instance which matches any of the InstancePlatforms. Signed-off-by: Nalin Dahyabhai --- copy/copy.go | 6 ++++-- copy/multiple.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/copy/copy.go b/copy/copy.go index bf8f4015b6..1223e726b6 100644 --- a/copy/copy.go +++ b/copy/copy.go @@ -23,6 +23,7 @@ import ( "github.com/containers/image/v5/types" encconfig "github.com/containers/ocicrypt/config" digest "github.com/opencontainers/go-digest" + imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sirupsen/logrus" "golang.org/x/exp/slices" "golang.org/x/sync/semaphore" @@ -91,8 +92,9 @@ type Options struct { PreserveDigests bool // manifest MIME type of image set by user. "" is default and means use the autodetection to the manifest MIME type ForceManifestMIMEType string - ImageListSelection ImageListSelection // set to either CopySystemImage (the default), CopyAllImages, or CopySpecificImages to control which instances we copy when the source reference is a list; ignored if the source reference is not a list - Instances []digest.Digest // if ImageListSelection is CopySpecificImages, copy only these instances and the list itself + ImageListSelection ImageListSelection // set to either CopySystemImage (the default), CopyAllImages, or CopySpecificImages to control which instances we copy when the source reference is a list; ignored if the source reference is not a list + Instances []digest.Digest // if ImageListSelection is CopySpecificImages, copy only these instances, instances matching the InstancePlatforms list, and the list itself + InstancePlatforms []imgspecv1.Platform // if ImageListSelection is CopySpecificImages, copy only matching instances, instances listed in the Instances list, and the list itself // Give priority to pulling gzip images if multiple images are present when configured to OptionalBoolTrue, // prefers the best compression if this is configured as OptionalBoolFalse. Choose automatically (and the choice may change over time) // if this is set to OptionalBoolUndefined (which is the default behavior, and recommended for most callers). diff --git a/copy/multiple.go b/copy/multiple.go index 097a18855e..beee8ea053 100644 --- a/copy/multiple.go +++ b/copy/multiple.go @@ -10,11 +10,13 @@ import ( "github.com/containers/image/v5/docker/reference" "github.com/containers/image/v5/internal/image" internalManifest "github.com/containers/image/v5/internal/manifest" + "github.com/containers/image/v5/internal/set" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/signature" + "github.com/containers/image/v5/types" + digest "github.com/opencontainers/go-digest" imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/sirupsen/logrus" - "golang.org/x/exp/slices" ) // copyMultipleImages copies some or all of an image list's instances, using @@ -89,15 +91,19 @@ func (c *copier) copyMultipleImages(ctx context.Context, policyContext *signatur // Copy each image, or just the ones we want to copy, in turn. instanceDigests := updatedList.Instances() imagesToCopy := len(instanceDigests) + var specificImages *set.Set[digest.Digest] if options.ImageListSelection == CopySpecificImages { - imagesToCopy = len(options.Instances) + if specificImages, err = determineSpecificImages(options, updatedList); err != nil { + return nil, err + } + imagesToCopy = len(specificImages.Values()) } c.Printf("Copying %d of %d images in list\n", imagesToCopy, len(instanceDigests)) updates := make([]manifest.ListUpdate, len(instanceDigests)) instancesCopied := 0 for i, instanceDigest := range instanceDigests { if options.ImageListSelection == CopySpecificImages && - !slices.Contains(options.Instances, instanceDigest) { + !specificImages.Contains(instanceDigest) { update, err := updatedList.Instance(instanceDigest) if err != nil { return nil, err @@ -196,3 +202,30 @@ func (c *copier) copyMultipleImages(ctx context.Context, policyContext *signatur return manifestList, nil } + +// determineSpecificImages returns a set of images to copy based on the +// Instances and InstancePlatforms fields of the passed-in options structure +func determineSpecificImages(options *Options, updatedList manifest.List) (*set.Set[digest.Digest], error) { + // Start with the instances that were listed by digest. + specificImages := set.New[digest.Digest]() + for _, instanceDigest := range options.Instances { + specificImages.Add(instanceDigest) + } + if len(options.InstancePlatforms) > 0 { + // Choose the best match for each platform we were asked to + // also copy, and add it to the set of instances to copy. + for _, platform := range options.InstancePlatforms { + platformContext := types.SystemContext{ + OSChoice: platform.OS, + ArchitectureChoice: platform.Architecture, + VariantChoice: platform.Variant, + } + instanceDigest, err := updatedList.ChooseInstance(&platformContext) + if err != nil { + return nil, fmt.Errorf("While choosing the instance for platform spec %q: %w", platform, err) + } + specificImages.Add(instanceDigest) + } + } + return specificImages, nil +}