Skip to content

Commit 474b858

Browse files
committed
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 <[email protected]>
1 parent 6b006dc commit 474b858

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

copy/copy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/containers/image/v5/types"
2424
encconfig "github.com/containers/ocicrypt/config"
2525
digest "github.com/opencontainers/go-digest"
26+
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
2627
"github.com/sirupsen/logrus"
2728
"golang.org/x/exp/slices"
2829
"golang.org/x/sync/semaphore"
@@ -91,8 +92,9 @@ type Options struct {
9192
PreserveDigests bool
9293
// manifest MIME type of image set by user. "" is default and means use the autodetection to the manifest MIME type
9394
ForceManifestMIMEType string
94-
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
95-
Instances []digest.Digest // if ImageListSelection is CopySpecificImages, copy only these instances and the list itself
95+
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
96+
Instances []digest.Digest // if ImageListSelection is CopySpecificImages, copy only these instances, instances matching the InstancePlatforms list, and the list itself
97+
InstancePlatforms []imgspecv1.Platform // if ImageListSelection is CopySpecificImages, copy only matching instances, instances listed in the Instances list, and the list itself
9698
// Give priority to pulling gzip images if multiple images are present when configured to OptionalBoolTrue,
9799
// prefers the best compression if this is configured as OptionalBoolFalse. Choose automatically (and the choice may change over time)
98100
// if this is set to OptionalBoolUndefined (which is the default behavior, and recommended for most callers).

copy/multiple.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"github.com/containers/image/v5/docker/reference"
1111
"github.com/containers/image/v5/internal/image"
1212
internalManifest "github.com/containers/image/v5/internal/manifest"
13+
"github.com/containers/image/v5/internal/set"
1314
"github.com/containers/image/v5/manifest"
1415
"github.com/containers/image/v5/signature"
16+
"github.com/containers/image/v5/types"
17+
digest "github.com/opencontainers/go-digest"
1518
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
1619
"github.com/sirupsen/logrus"
17-
"golang.org/x/exp/slices"
1820
)
1921

2022
// 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
8991
// Copy each image, or just the ones we want to copy, in turn.
9092
instanceDigests := updatedList.Instances()
9193
imagesToCopy := len(instanceDigests)
94+
var specificImages *set.Set[digest.Digest]
9295
if options.ImageListSelection == CopySpecificImages {
93-
imagesToCopy = len(options.Instances)
96+
if specificImages, err = determineSpecificImages(options, updatedList); err != nil {
97+
return nil, err
98+
}
99+
imagesToCopy = len(specificImages.Values())
94100
}
95101
c.Printf("Copying %d of %d images in list\n", imagesToCopy, len(instanceDigests))
96102
updates := make([]manifest.ListUpdate, len(instanceDigests))
97103
instancesCopied := 0
98104
for i, instanceDigest := range instanceDigests {
99105
if options.ImageListSelection == CopySpecificImages &&
100-
!slices.Contains(options.Instances, instanceDigest) {
106+
!specificImages.Contains(instanceDigest) {
101107
update, err := updatedList.Instance(instanceDigest)
102108
if err != nil {
103109
return nil, err
@@ -196,3 +202,30 @@ func (c *copier) copyMultipleImages(ctx context.Context, policyContext *signatur
196202

197203
return manifestList, nil
198204
}
205+
206+
// determineSpecificImages returns a set of images to copy based on the
207+
// Instances and InstancePlatforms fields of the passed-in options structure
208+
func determineSpecificImages(options *Options, updatedList manifest.List) (*set.Set[digest.Digest], error) {
209+
// Start with the instances that were listed by digest.
210+
specificImages := set.New[digest.Digest]()
211+
for _, instanceDigest := range options.Instances {
212+
specificImages.Add(instanceDigest)
213+
}
214+
if len(options.InstancePlatforms) > 0 {
215+
// Choose the best match for each platform we were asked to
216+
// also copy, and add it to the set of instances to copy.
217+
for _, platform := range options.InstancePlatforms {
218+
platformContext := types.SystemContext{
219+
OSChoice: platform.OS,
220+
ArchitectureChoice: platform.Architecture,
221+
VariantChoice: platform.Variant,
222+
}
223+
instanceDigest, err := updatedList.ChooseInstance(&platformContext)
224+
if err != nil {
225+
return nil, fmt.Errorf("While choosing the instance for platform spec %q: %w", platform, err)
226+
}
227+
specificImages.Add(instanceDigest)
228+
}
229+
}
230+
return specificImages, nil
231+
}

0 commit comments

Comments
 (0)