-
Notifications
You must be signed in to change notification settings - Fork 105
OCPBUGS-66263: Include index image sub-digests in dry run mapping.txt #1355
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
dorzel
wants to merge
3
commits into
openshift:main
Choose a base branch
from
dorzel:OCPBUGS-66263
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -3,14 +3,23 @@ package cli | |
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| imgmanifest "go.podman.io/image/v5/manifest" | ||
| "go.podman.io/image/v5/transports/alltransports" | ||
| "go.podman.io/image/v5/types" | ||
|
|
||
| "github.com/openshift/oc-mirror/v2/internal/pkg/api/v2alpha1" | ||
| "github.com/openshift/oc-mirror/v2/internal/pkg/consts" | ||
| "github.com/openshift/oc-mirror/v2/internal/pkg/emoji" | ||
| "github.com/openshift/oc-mirror/v2/internal/pkg/image" | ||
| ) | ||
|
|
||
| func (o *ExecutorSchema) DryRun(ctx context.Context, allImages []v2alpha1.CopyImageSchema) error { | ||
| func (o *ExecutorSchema) DryRun(ctx context.Context, allImages []v2alpha1.CopyImageSchema, preCollectedManifestLists map[string][]string) error { | ||
| // set up location of logs dir | ||
| outDir := filepath.Join(o.Opts.Global.WorkingDir, dryRunOutDir) | ||
| // clean up logs directory | ||
|
|
@@ -22,6 +31,28 @@ func (o *ExecutorSchema) DryRun(ctx context.Context, allImages []v2alpha1.CopyIm | |
| o.Log.Error(" %v ", err) | ||
| return err | ||
| } | ||
|
|
||
| // Inspect only images not already classified during collection. | ||
| var remaining []v2alpha1.CopyImageSchema | ||
| for _, img := range allImages { | ||
| if _, found := preCollectedManifestLists[img.Origin]; !found { | ||
| remaining = append(remaining, img) | ||
| } | ||
| } | ||
|
|
||
| o.Log.Info(emoji.LeftPointingMagnifyingGlass+" inspecting %d remaining images for manifest lists (%d already detected during collection)...", | ||
| len(remaining), len(allImages)-len(remaining)) | ||
| runtimeDigests := o.inspectManifestLists(ctx, remaining) | ||
|
|
||
| // Merge pre-collected and runtime manifest list results. | ||
| manifestListDigests := make(map[string][]string, len(preCollectedManifestLists)+len(runtimeDigests)) | ||
| for k, v := range preCollectedManifestLists { | ||
| manifestListDigests[k] = v | ||
| } | ||
| for k, v := range runtimeDigests { | ||
| manifestListDigests[k] = v | ||
| } | ||
|
|
||
| // creating file for storing list of cached images | ||
| mappingTxtFilePath := filepath.Join(outDir, mappingFile) | ||
| mappingTxtFile, err := os.Create(mappingTxtFilePath) | ||
|
|
@@ -33,8 +64,31 @@ func (o *ExecutorSchema) DryRun(ctx context.Context, allImages []v2alpha1.CopyIm | |
| nbMissingImgs := 0 | ||
| var buff bytes.Buffer | ||
| var missingImgsBuff bytes.Buffer | ||
|
|
||
| for _, img := range allImages { | ||
| buff.WriteString(img.Source + "=" + img.Destination + "\n") | ||
|
|
||
| // Collect sub-digest source=destination pairs | ||
| type subDigestEntry struct{ source, dest string } | ||
| var subDigestEntries []subDigestEntry | ||
|
|
||
| // Look up manifest list sub-digests: check both by Origin (pre-collected | ||
| // during operator collection) and by Source (detected at runtime). | ||
| manifestDigests := manifestListDigests[img.Origin] | ||
| if len(manifestDigests) == 0 { | ||
| manifestDigests = manifestListDigests[img.Source] | ||
| } | ||
| if len(manifestDigests) > 0 { | ||
| // This is a manifest list, write each sub-digest with digest-pinned destination | ||
| sourceBase, _, _ := strings.Cut(img.Source, "@") | ||
| for _, digest := range manifestDigests { | ||
| subSource := sourceBase + "@" + digest | ||
| subDest := subDigestDestination(img.Destination, digest) | ||
| buff.WriteString(subSource + "=" + subDest + "\n") | ||
| subDigestEntries = append(subDigestEntries, subDigestEntry{source: subSource, dest: subDest}) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if o.Opts.IsMirrorToDisk() { | ||
| exists, err := o.Mirror.Check(ctx, img.Destination, o.Opts, false) | ||
| if err != nil { | ||
|
|
@@ -43,6 +97,10 @@ func (o *ExecutorSchema) DryRun(ctx context.Context, allImages []v2alpha1.CopyIm | |
| if err != nil || !exists { | ||
| missingImgsBuff.WriteString(img.Source + "=" + img.Destination + "\n") | ||
| nbMissingImgs++ | ||
| // Also include sub-digest entries in missing list | ||
| for _, sub := range subDigestEntries { | ||
| missingImgsBuff.WriteString(sub.source + "=" + sub.dest + "\n") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -73,3 +131,116 @@ func (o *ExecutorSchema) DryRun(ctx context.Context, allImages []v2alpha1.CopyIm | |
| o.Log.Info(emoji.PageFacingUp+" list of all images for mirroring in : %s", mappingTxtFilePath) | ||
| return nil | ||
| } | ||
|
|
||
| // subDigestDestination returns a digest-pinned destination for a sub-digest entry. | ||
| // For docker:// destinations, it strips the tag and appends the sub-digest to avoid | ||
| // destination overwrites when multiple architectures map to the same tag. | ||
| // For non-docker destinations (oci:, dir:, etc.), the destination is returned as-is. | ||
| func subDigestDestination(dest string, digest string) string { | ||
| if !strings.HasPrefix(dest, consts.DockerProtocol) { | ||
| return dest | ||
| } | ||
| destSpec, err := image.ParseRef(dest) | ||
| if err != nil { | ||
| return dest | ||
| } | ||
| return destSpec.Transport + destSpec.Name + "@" + digest | ||
| } | ||
|
|
||
| // inspectManifestLists concurrently inspects all images to identify manifest lists | ||
| // and returns a map of source references to their sub-manifest digests. | ||
| // Concurrency is bounded via a semaphore to avoid overwhelming registries. | ||
| func (o *ExecutorSchema) inspectManifestLists(ctx context.Context, images []v2alpha1.CopyImageSchema) map[string][]string { | ||
| manifestListDigests := make(map[string][]string) | ||
| var mu sync.Mutex | ||
| var wg sync.WaitGroup | ||
|
|
||
| parallelism := o.Opts.ParallelImages | ||
| if parallelism == 0 { | ||
| parallelism = maxParallelImageDownloads | ||
| } | ||
| semaphore := make(chan struct{}, parallelism) | ||
|
|
||
| cancelCtx, cancel := context.WithCancel(ctx) | ||
| defer cancel() | ||
|
|
||
| for _, img := range images { | ||
| select { | ||
| case <-cancelCtx.Done(): | ||
| break | ||
|
Contributor
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. If the idea here is go outside of the for loop, only or |
||
| default: | ||
| } | ||
|
|
||
| semaphore <- struct{}{} | ||
|
|
||
| wg.Add(1) | ||
| go func(source string) { | ||
| defer wg.Done() | ||
| defer func() { <-semaphore }() | ||
|
|
||
| digests, err := o.getManifestListDigests(cancelCtx, source) | ||
| if err != nil { | ||
| o.Log.Warn("unable to inspect manifest for %s: %v", source, err) | ||
| return | ||
| } | ||
| if len(digests) > 0 { | ||
| mu.Lock() | ||
| manifestListDigests[source] = digests | ||
| mu.Unlock() | ||
| } | ||
| }(img.Source) | ||
| } | ||
| wg.Wait() | ||
| return manifestListDigests | ||
| } | ||
|
|
||
| // getManifestListDigests inspects the source image to check if it's a manifest list | ||
| // and returns the sub-manifest digests. Works with any transport supported by | ||
| // containers/image (docker://, oci:, etc.) via alltransports.ParseImageName. | ||
| // Returns a slice of digest strings (e.g., ["sha256:abc...", "sha256:def..."]) or nil if not a manifest list. | ||
| func (o *ExecutorSchema) getManifestListDigests(ctx context.Context, source string) ([]string, error) { | ||
| srcRef, err := alltransports.ParseImageName(source) | ||
| if err != nil { | ||
| // Retry with docker:// prefix for sources without transport (e.g., Cincinnati sources) | ||
| srcRef, err = alltransports.ParseImageName(consts.DockerProtocol + source) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error parsing image name %s: %w", source, err) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| sysCtx, err := o.Opts.SrcImage.NewSystemContext() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating system context: %w", err) | ||
| } | ||
| // The local cache registry is HTTP-only; ensure we skip TLS verification for it. | ||
| if o.Opts.LocalStorageFQDN != "" && strings.Contains(source, o.Opts.LocalStorageFQDN) { | ||
| sysCtx.DockerInsecureSkipTLSVerify = types.OptionalBoolTrue | ||
| } | ||
|
|
||
| imgSrc, err := srcRef.NewImageSource(ctx, sysCtx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating image source for %s: %w", source, err) | ||
| } | ||
| defer imgSrc.Close() | ||
|
|
||
| manifestBytes, manifestType, err := imgSrc.GetManifest(ctx, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error getting manifest for %s: %w", source, err) | ||
| } | ||
|
|
||
| if !imgmanifest.MIMETypeIsMultiImage(manifestType) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| list, err := imgmanifest.ListFromBlob(manifestBytes, manifestType) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error parsing manifest list for %s: %w", source, err) | ||
| } | ||
|
|
||
| instances := list.Instances() | ||
| digests := make([]string, 0, len(instances)) | ||
| for _, instance := range instances { | ||
| digests = append(digests, instance.String()) | ||
| } | ||
| return digests, nil | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maps.Copy could be used here to reduce the code.