-
Notifications
You must be signed in to change notification settings - Fork 226
feat(copy): Support copying artifacts across multiple platforms. #1954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hellocn9
wants to merge
4
commits into
oras-project:main
Choose a base branch
from
hellocn9:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
842629d
feat(copy): Support copying artifacts across multiple platforms.
hellocn9 bd5a7b0
refactor(copy): Optimize platform copying logic to support multi-plat…
hellocn9 8b9cbc3
test(copy): add related unit tests and E2E tests for multi-platform c…
hellocn9 70e0d23
refactor(copy): Optimize platform copying logic to support multi-plat…
hellocn9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ import ( | |
| "oras.land/oras/cmd/oras/internal/argument" | ||
| "oras.land/oras/cmd/oras/internal/command" | ||
| "oras.land/oras/cmd/oras/internal/display" | ||
| "oras.land/oras/cmd/oras/internal/display/metadata" | ||
| "oras.land/oras/cmd/oras/internal/display/status" | ||
| oerrors "oras.land/oras/cmd/oras/internal/errors" | ||
| "oras.land/oras/cmd/oras/internal/option" | ||
|
|
@@ -85,6 +86,9 @@ Example - Copy an artifact and referrers using specific methods for the Referrer | |
| Example - Copy certain platform of an artifact: | ||
| oras cp --platform linux/arm/v5 localhost:5000/net-monitor:v1 localhost:6000/net-monitor-copy:v1 | ||
|
|
||
| Example - Copy certain platforms of an artifact: | ||
| oras cp --platform linux/amd64,linux/arm64,linux/arm/v7 localhost:5000/net-monitor:v1 localhost:6000/net-monitor-copy:v1 | ||
|
|
||
| Example - Copy an artifact with multiple tags: | ||
| oras cp localhost:5000/net-monitor:v1 localhost:6000/net-monitor-copy:tag1,tag2,tag3 | ||
|
|
||
|
|
@@ -138,20 +142,181 @@ func runCopy(cmd *cobra.Command, opts *copyOptions) error { | |
| ctx = registryutil.WithScopeHint(ctx, dst, auth.ActionPull, auth.ActionPush) | ||
| statusHandler, metadataHandler := display.NewCopyHandler(opts.Printer, opts.TTY, dst) | ||
|
|
||
| desc, err := doCopy(ctx, statusHandler, src, dst, opts) | ||
| // Check if multiple platforms are specified | ||
| if len(opts.Platform.Platforms) > 1 && !opts.recursive { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part is confusing me here, why is this checking for not recursive? If there are multiple platforms specified don't we want to copy the multiple platforms? |
||
| // Handle multiple platforms - copy manifests that match the specified platforms | ||
| return copyMultiplePlatforms(ctx, statusHandler, metadataHandler, src, dst, opts) | ||
| } else { | ||
| // Original behavior for single platform or recursive mode | ||
| desc, err := doCopy(ctx, statusHandler, src, dst, opts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if from, err := digest.Parse(opts.From.Reference); err == nil && from != desc.Digest { | ||
| // correct source digest | ||
| opts.From.RawReference = fmt.Sprintf("%s@%s", opts.From.Path, desc.Digest.String()) | ||
| } | ||
|
|
||
| if err := metadataHandler.OnCopied(&opts.BinaryTarget, desc); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(opts.extraRefs) != 0 { | ||
| tagNOpts := oras.DefaultTagNOptions | ||
| tagNOpts.Concurrency = opts.concurrency | ||
| tagListener := listener.NewTaggedListener(dst, metadataHandler.OnTagged) | ||
| if _, err = oras.TagN(ctx, tagListener, opts.To.Reference, opts.extraRefs, tagNOpts); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return metadataHandler.Render() | ||
| } | ||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // copyMultiplePlatforms handles copying when multiple platforms are specified | ||
| func copyMultiplePlatforms(ctx context.Context, statusHandler status.CopyHandler, metadataHandler metadata.CopyHandler, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts *copyOptions) error { | ||
| // Resolve the source reference to get the root descriptor | ||
| resolveOpts := oras.DefaultResolveOptions | ||
| // We don't set TargetPlatform here since we want to get the full index/list | ||
| root, err := oras.Resolve(ctx, src, opts.From.Reference, resolveOpts) | ||
| if err != nil { | ||
| return err | ||
| return fmt.Errorf("failed to resolve %s: %w", opts.From.Reference, err) | ||
| } | ||
|
|
||
| // Check if the resolved descriptor is an index/manifest list | ||
| isIndex := root.MediaType == ocispec.MediaTypeImageIndex || root.MediaType == docker.MediaTypeManifestList | ||
| if !isIndex { | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If not an index, fall back to single platform behavior with first platform | ||
| tempOpts := *opts | ||
| tempOpts.Platform.Platform = opts.Platform.Platforms[0] | ||
| desc, err := doCopy(ctx, statusHandler, src, dst, &tempOpts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if from, err := digest.Parse(opts.From.Reference); err == nil && from != desc.Digest { | ||
| opts.From.RawReference = fmt.Sprintf("%s@%s", opts.From.Path, desc.Digest.String()) | ||
| } | ||
|
|
||
| if err := metadataHandler.OnCopied(&opts.BinaryTarget, desc); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(opts.extraRefs) != 0 { | ||
| tagNOpts := oras.DefaultTagNOptions | ||
| tagNOpts.Concurrency = opts.concurrency | ||
| tagListener := listener.NewTaggedListener(dst, metadataHandler.OnTagged) | ||
| if _, err = oras.TagN(ctx, tagListener, opts.To.Reference, opts.extraRefs, tagNOpts); err != nil { | ||
| return err | ||
| } | ||
| } | ||
hellocn9 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return metadataHandler.Render() | ||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // For indexes/lists, fetch the index content | ||
| indexContent, err := content.FetchAll(ctx, src, root) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to fetch index: %w", err) | ||
| } | ||
|
|
||
| var index ocispec.Index | ||
| if err := json.Unmarshal(indexContent, &index); err != nil { | ||
| return fmt.Errorf("failed to parse index: %w", err) | ||
| } | ||
|
|
||
| // Filter manifests based on the specified platforms | ||
| var filteredManifests []ocispec.Descriptor | ||
| for _, manifest := range index.Manifests { | ||
| if manifest.Platform != nil && matchesAnyPlatform(manifest.Platform, opts.Platform.Platforms) { | ||
| filteredManifests = append(filteredManifests, manifest) | ||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if len(filteredManifests) == 0 { | ||
| requestedPlatforms := opts.Platform.Platforms | ||
| var availablePlatforms []string | ||
| for _, manifest := range index.Manifests { | ||
| if manifest.Platform != nil { | ||
| availablePlatforms = append(availablePlatforms, fmt.Sprintf("%s/%s", manifest.Platform.OS, manifest.Platform.Architecture)) | ||
| } | ||
| } | ||
| availableDesc := "none" | ||
| if len(availablePlatforms) > 0 { | ||
| availableDesc = strings.Join(availablePlatforms, ", ") | ||
| } | ||
| return fmt.Errorf("no manifests match the requested platforms %v; available platforms in index: %s", requestedPlatforms, availableDesc) | ||
| } | ||
|
|
||
| // Create a new index with only the filtered manifests | ||
| newIndex := ocispec.Index{ | ||
| Versioned: index.Versioned, | ||
| MediaType: index.MediaType, | ||
| Manifests: filteredManifests, | ||
| Annotations: index.Annotations, | ||
| } | ||
hellocn9 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Marshal the new index | ||
| newIndexContent, err := json.Marshal(newIndex) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal new index: %w", err) | ||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if from, err := digest.Parse(opts.From.Reference); err == nil && from != desc.Digest { | ||
| // correct source digest | ||
| opts.From.RawReference = fmt.Sprintf("%s@%s", opts.From.Path, desc.Digest.String()) | ||
| // Create a descriptor for the new index | ||
| newIndexDesc := ocispec.Descriptor{ | ||
| MediaType: index.MediaType, | ||
| Digest: digest.FromBytes(newIndexContent), | ||
| Size: int64(len(newIndexContent)), | ||
| Annotations: index.Annotations, | ||
| } | ||
|
|
||
| if err := metadataHandler.OnCopied(&opts.BinaryTarget, desc); err != nil { | ||
| // Prepare copy options | ||
| extendedCopyGraphOptions := oras.DefaultExtendedCopyGraphOptions | ||
| extendedCopyGraphOptions.Concurrency = opts.concurrency | ||
| extendedCopyGraphOptions.FindPredecessors = func(ctx context.Context, src content.ReadOnlyGraphStorage, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return registry.Referrers(ctx, src, desc, "") | ||
| } | ||
|
|
||
| if mountRepo, canMount := getMountPoint(src, dst, opts); canMount { | ||
| extendedCopyGraphOptions.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) { | ||
| return []string{mountRepo}, nil | ||
| } | ||
| } | ||
| dst, err = statusHandler.StartTracking(dst) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { | ||
| _ = statusHandler.StopTracking() | ||
| }() | ||
| extendedCopyGraphOptions.OnCopySkipped = statusHandler.OnCopySkipped | ||
| extendedCopyGraphOptions.PreCopy = statusHandler.PreCopy | ||
| extendedCopyGraphOptions.PostCopy = statusHandler.PostCopy | ||
| extendedCopyGraphOptions.OnMounted = statusHandler.OnMounted | ||
|
|
||
| // Copy all matching manifests and their content | ||
| for _, manifestDesc := range filteredManifests { | ||
| // Copy the manifest itself | ||
| if err := oras.CopyGraph(ctx, src, dst, manifestDesc, extendedCopyGraphOptions.CopyGraphOptions); err != nil { | ||
| return fmt.Errorf("failed to copy manifest %s: %w", manifestDesc.Digest, err) | ||
| } | ||
| } | ||
|
|
||
| // Push the new index to the destination | ||
| if err := dst.Push(ctx, newIndexDesc, strings.NewReader(string(newIndexContent))); err != nil { | ||
| return fmt.Errorf("failed to push new index: %w", err) | ||
| } | ||
|
|
||
| // Tag the new index if needed | ||
| if opts.To.Reference != "" { | ||
| if err := dst.Tag(ctx, newIndexDesc, opts.To.Reference); err != nil { | ||
| return fmt.Errorf("failed to tag new index: %w", err) | ||
| } | ||
| } | ||
|
|
||
| // Handle extra references | ||
| if len(opts.extraRefs) != 0 { | ||
| tagNOpts := oras.DefaultTagNOptions | ||
| tagNOpts.Concurrency = opts.concurrency | ||
|
|
@@ -161,9 +326,47 @@ func runCopy(cmd *cobra.Command, opts *copyOptions) error { | |
| } | ||
| } | ||
|
|
||
| // Update reference if needed | ||
| if from, err := digest.Parse(opts.From.Reference); err == nil && from != newIndexDesc.Digest { | ||
| opts.From.RawReference = fmt.Sprintf("%s@%s", opts.From.Path, newIndexDesc.Digest.String()) | ||
| } | ||
|
|
||
| if err := metadataHandler.OnCopied(&opts.BinaryTarget, newIndexDesc); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return metadataHandler.Render() | ||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // matchesAnyPlatform checks if a manifest platform matches any of the specified platforms | ||
| func matchesAnyPlatform(manifestPlatform *ocispec.Platform, platforms []*ocispec.Platform) bool { | ||
| for _, platform := range platforms { | ||
| if platformMatches(manifestPlatform, platform) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // platformMatches checks if two platforms match | ||
| func platformMatches(a, b *ocispec.Platform) bool { | ||
| if a.OS != b.OS || a.Architecture != b.Architecture { | ||
| return false | ||
| } | ||
|
|
||
| // Variant is optional; only treat it as a mismatch if both variants are non-empty and different. | ||
| if a.Variant != "" && b.Variant != "" && a.Variant != b.Variant { | ||
| return false | ||
| } | ||
|
|
||
| // OSVersion is optional; only treat it as a mismatch if both OSVersions are non-empty and different. | ||
| if a.OSVersion != "" && b.OSVersion != "" && a.OSVersion != b.OSVersion { | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
hellocn9 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
hellocn9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func doCopy(ctx context.Context, copyHandler status.CopyHandler, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts *copyOptions) (desc ocispec.Descriptor, err error) { | ||
| // Prepare copy options | ||
| extendedCopyGraphOptions := oras.DefaultExtendedCopyGraphOptions | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.