Skip to content

Commit 4b9eb64

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 5387e5a commit 4b9eb64

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
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: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
internalManifest "github.com/containers/image/v5/internal/manifest"
1313
"github.com/containers/image/v5/manifest"
1414
"github.com/containers/image/v5/signature"
15+
"github.com/containers/image/v5/types"
16+
digest "github.com/opencontainers/go-digest"
1517
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
1618
"github.com/sirupsen/logrus"
1719
"golang.org/x/exp/slices"
@@ -87,17 +89,43 @@ func (c *copier) copyMultipleImages(ctx context.Context, policyContext *signatur
8789
}
8890

8991
// Copy each image, or just the ones we want to copy, in turn.
92+
optionsInstances := options.Instances
9093
instanceDigests := updatedList.Instances()
94+
if len(options.InstancePlatforms) > 0 {
95+
// Build a map for helping us avoid duplication of instance digests.
96+
instanceDigestMap := make(map[digest.Digest]struct{})
97+
for _, instanceDigest := range optionsInstances {
98+
instanceDigestMap[instanceDigest] = struct{}{}
99+
}
100+
// Choose the best match for each platform we were asked to also copy.
101+
for _, platform := range options.InstancePlatforms {
102+
platformContext := types.SystemContext{
103+
OSChoice: platform.OS,
104+
ArchitectureChoice: platform.Architecture,
105+
VariantChoice: platform.Variant,
106+
}
107+
instanceDigest, err := updatedList.ChooseInstance(&platformContext)
108+
if err != nil {
109+
return nil, fmt.Errorf("While choosing the instance for platform spec %q: %w", platform, err)
110+
}
111+
instanceDigestMap[instanceDigest] = struct{}{}
112+
}
113+
// Deduplicate the set of digests.
114+
optionsInstances = nil
115+
for instanceDigest := range instanceDigestMap {
116+
optionsInstances = append(optionsInstances, instanceDigest)
117+
}
118+
}
91119
imagesToCopy := len(instanceDigests)
92120
if options.ImageListSelection == CopySpecificImages {
93-
imagesToCopy = len(options.Instances)
121+
imagesToCopy = len(optionsInstances)
94122
}
95123
c.Printf("Copying %d of %d images in list\n", imagesToCopy, len(instanceDigests))
96124
updates := make([]manifest.ListUpdate, len(instanceDigests))
97125
instancesCopied := 0
98126
for i, instanceDigest := range instanceDigests {
99127
if options.ImageListSelection == CopySpecificImages &&
100-
!slices.Contains(options.Instances, instanceDigest) {
128+
!slices.Contains(optionsInstances, instanceDigest) {
101129
update, err := updatedList.Instance(instanceDigest)
102130
if err != nil {
103131
return nil, err

0 commit comments

Comments
 (0)