-
Notifications
You must be signed in to change notification settings - Fork 458
MCO-1956: Extract image utils from build package #5400
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,13 @@ package imagepruner | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/containers/common/pkg/retry" | ||
| "github.com/containers/image/v5/docker" | ||
| "github.com/containers/image/v5/image" | ||
| "github.com/containers/image/v5/manifest" | ||
| "github.com/containers/image/v5/types" | ||
| digest "github.com/opencontainers/go-digest" | ||
| "github.com/openshift/machine-config-operator/pkg/imageutils" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -58,24 +57,6 @@ func (i *imageInspectorImpl) DeleteImage(ctx context.Context, sysCtx *types.Syst | |
| return deleteImage(ctx, sysCtx, image) | ||
| } | ||
|
|
||
| // parseImageName parses the image name string into an ImageReference, | ||
| // handling various prefix formats like "docker://" and ensuring a standard format. | ||
| func parseImageName(imgName string) (types.ImageReference, error) { | ||
| if strings.Contains(imgName, "//") && !strings.HasPrefix(imgName, "docker://") { | ||
| return nil, fmt.Errorf("unknown transport for pullspec %s", imgName) | ||
| } | ||
|
|
||
| if strings.HasPrefix(imgName, "docker://") { | ||
| imgName = strings.ReplaceAll(imgName, "docker://", "//") | ||
| } | ||
|
|
||
| if !strings.HasPrefix(imgName, "//") { | ||
| imgName = "//" + imgName | ||
| } | ||
|
|
||
| return docker.Transport.ParseReference(imgName) | ||
| } | ||
|
|
||
| // deleteImage attempts to delete the specified image with retries, | ||
| // using the provided context and system context. | ||
| // | ||
|
|
@@ -85,7 +66,7 @@ func deleteImage(ctx context.Context, sysCtx *types.SystemContext, imageName str | |
| MaxRetry: cmdRetriesCount, | ||
| } | ||
|
|
||
| ref, err := parseImageName(imageName) | ||
| ref, err := imageutils.ParseImageName(imageName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -105,29 +86,19 @@ func deleteImage(ctx context.Context, sysCtx *types.SystemContext, imageName str | |
| // | ||
| //nolint:unparam | ||
| func imageInspect(ctx context.Context, sysCtx *types.SystemContext, imageName string) (*types.ImageInspectInfo, *digest.Digest, error) { | ||
| var ( | ||
| src types.ImageSource | ||
| imgInspect *types.ImageInspectInfo | ||
| err error | ||
| ) | ||
| ref, err := imageutils.ParseImageName(imageName) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("error parsing image name %q: %w", imageName, err) | ||
| } | ||
|
|
||
| retryOpts := retry.RetryOptions{ | ||
| MaxRetry: cmdRetriesCount, | ||
| } | ||
|
|
||
| ref, err := parseImageName(imageName) | ||
| src, err := imageutils.GetImageSourceFromReference(ctx, sysCtx, ref, &retryOpts) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("error parsing image name %q: %w", imageName, err) | ||
| } | ||
|
|
||
| // retry.IfNecessary takes into account whether the error is "retryable" | ||
| // so we don't keep looping on errors that will never resolve | ||
| if err := retry.RetryIfNecessary(ctx, func() error { | ||
| src, err = ref.NewImageSource(ctx, sysCtx) | ||
| return err | ||
| }, &retryOpts); err != nil { | ||
| return nil, nil, newErrImage(imageName, fmt.Errorf("error getting image source: %w", err)) | ||
| return nil, nil, fmt.Errorf("error getting image source for %s: %w", imageName, err) | ||
| } | ||
| defer src.Close() | ||
|
|
||
| var rawManifest []byte | ||
| unparsedInstance := image.UnparsedInstance(src, nil) | ||
|
|
@@ -144,14 +115,13 @@ func imageInspect(ctx context.Context, sysCtx *types.SystemContext, imageName st | |
| return nil, nil, fmt.Errorf("error retrieving image digest: %q: %w", imageName, err) | ||
| } | ||
|
|
||
| defer src.Close() | ||
|
|
||
| img, err := image.FromUnparsedImage(ctx, sysCtx, unparsedInstance) | ||
| if err != nil { | ||
| return nil, nil, newErrImage(imageName, fmt.Errorf("error parsing manifest for image: %w", err)) | ||
| } | ||
|
|
||
| if err := retry.RetryIfNecessary(ctx, func() error { | ||
| var imgInspect *types.ImageInspectInfo | ||
| if err := retry.IfNecessary(ctx, func() error { | ||
|
Contributor
Author
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. Changed to
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. Good catch! 💯 I think there are a few things we use in our code base that are deprecated. Maybe a good task for AI would be getting it to find what's deprecated and suggest solutions. 🤔 Maybe I'll try that out once the release freeze is behind us.
Contributor
Author
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. I catched it myself this time, but Claude has already told me about deprecated code. When I was coding the mco-sanitize Claude told me the GPG packet was about to be discontinued. |
||
| imgInspect, err = img.Inspect(ctx) | ||
| return err | ||
| }, &retryOpts); err != nil { | ||
|
|
||
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.
I moved this line a bit earlier as I think that the previous error conditions may make the function return without proper cleanup.