-
Notifications
You must be signed in to change notification settings - Fork 116
Fixes for run image extension #1134
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b698140
When pulling remote image data, fail if the remote image is not found
13f323f
When validating dockerfiles, set extend to true if there are any inst…
bedfb81
Update matching logic when considering if two image names are equival…
2afaa5a
Comments and cleanup
a5a1425
When exporting, continue to use run image identifier (which could be …
47480dd
Add Contains method to structs that hold run image information for ex…
07a7676
When finding the run image info for export, use the run image "image"…
debf8de
Fix detector acceptance and add more logging
eb08a3c
Fix: use "image" instead of "reference" and also guard against image …
ff750f3
Add comment
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
2 changes: 1 addition & 1 deletion
2
acceptance/testdata/restorer/container/layers/some-extend-false-analyzed.toml.placeholder
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| [run-image] | ||
| reference = "REPLACE" | ||
| reference = "" | ||
| image = "REPLACE" |
2 changes: 1 addition & 1 deletion
2
acceptance/testdata/restorer/container/layers/some-extend-true-analyzed.toml.placeholder
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [run-image] | ||
| reference = "REPLACE" | ||
| reference = "" | ||
| extend = true | ||
| image = "REPLACE" |
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
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
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
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 |
|---|---|---|
|
|
@@ -11,7 +11,6 @@ import ( | |
| "github.com/buildpacks/imgutil/layout/sparse" | ||
| "github.com/buildpacks/imgutil/remote" | ||
| "github.com/google/go-containerregistry/pkg/authn" | ||
| "github.com/google/go-containerregistry/pkg/name" | ||
|
|
||
| "github.com/buildpacks/lifecycle" | ||
| "github.com/buildpacks/lifecycle/auth" | ||
|
|
@@ -85,60 +84,43 @@ func (r *restoreCmd) Exec() error { | |
| ) | ||
| if analyzedMD, err = files.ReadAnalyzed(r.AnalyzedPath, cmd.DefaultLogger); err == nil { | ||
| if r.supportsBuildImageExtension() && r.BuildImageRef != "" { | ||
| cmd.DefaultLogger.Debugf("Pulling builder image metadata...") | ||
| buildImage, err := r.pullSparse(r.BuildImageRef) | ||
| cmd.DefaultLogger.Debugf("Pulling builder image metadata for %s...", r.BuildImageRef) | ||
| remoteBuildImage, err := r.pullSparse(r.BuildImageRef) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "read builder image") | ||
| return cmd.FailErr(err, "pull builder image") | ||
| } | ||
| digestRef, err := digestReference(r.BuildImageRef, buildImage) | ||
| digestRef, err := remoteBuildImage.Identifier() | ||
| if err != nil { | ||
| return cmd.FailErr(err, "get digest reference for builder image") | ||
| } | ||
| analyzedMD.BuildImage = &files.ImageIdentifier{Reference: digestRef} | ||
| analyzedMD.BuildImage = &files.ImageIdentifier{Reference: digestRef.String()} | ||
| cmd.DefaultLogger.Debugf("Adding build image info to analyzed metadata: ") | ||
| cmd.DefaultLogger.Debugf(encoding.ToJSONMaybe(analyzedMD.BuildImage)) | ||
| } | ||
| var ( | ||
| remoteRunImage imgutil.Image | ||
| ) | ||
| runImageName := analyzedMD.RunImageImage() // FIXME: if we have a digest reference available in `Reference` (e.g., in the non-daemon case) we should use it | ||
|
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. just verifying you still want this FIXME
Member
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. This seems to be an edge case and not super important to fix right now |
||
| if r.supportsRunImageExtension() && needsPulling(analyzedMD.RunImage) { | ||
| cmd.DefaultLogger.Debugf("Pulling run image metadata...") | ||
| runImageRef := analyzedMD.RunImageImage() | ||
| if runImageRef == "" { | ||
| runImageRef = analyzedMD.RunImage.Reference // older platforms don't populate Image | ||
| } | ||
| runImage, err := r.pullSparse(runImageRef) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "read run image") | ||
| } | ||
| targetData, err := platform.GetTargetMetadata(runImage) | ||
| cmd.DefaultLogger.Debugf("Pulling run image metadata for %s...", runImageName) | ||
| remoteRunImage, err = r.pullSparse(runImageName) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "read target data from run image") | ||
| return cmd.FailErr(err, "pull run image") | ||
| } | ||
| digestRef, err := digestReference(runImageRef, runImage) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "get digest reference for builder image") | ||
| } | ||
| analyzedMD.RunImage = &files.RunImage{ | ||
| Reference: digestRef, | ||
| Image: analyzedMD.RunImageImage(), | ||
| Extend: true, | ||
| TargetMetadata: targetData, | ||
| // update analyzed metadata, even if we only needed to pull the image metadata, because | ||
| // the extender needs a digest reference in analyzed.toml, | ||
| // and daemon images will only have a daemon image ID | ||
| if err = updateAnalyzedMD(&analyzedMD, remoteRunImage); err != nil { | ||
| return cmd.FailErr(err, "update analyzed metadata") | ||
| } | ||
| } else if r.supportsTargetData() && needsUpdating(analyzedMD.RunImage) { | ||
| cmd.DefaultLogger.Debugf("Updating analyzed metadata...") | ||
| runImage, err := remote.NewImage(analyzedMD.RunImage.Reference, r.keychain) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "read run image") | ||
| } | ||
| targetData, err := platform.GetTargetMetadata(runImage) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "read target data from run image") | ||
| cmd.DefaultLogger.Debugf("Updating run image info in analyzed metadata...") | ||
| remoteRunImage, err = remote.NewImage(runImageName, r.keychain) | ||
| if err != nil || !remoteRunImage.Found() { | ||
| return cmd.FailErr(err, "pull run image") | ||
| } | ||
| digestRef, err := digestReference(analyzedMD.RunImage.Reference, runImage) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "get digest reference for builder image") | ||
| } | ||
| analyzedMD.RunImage = &files.RunImage{ | ||
| Reference: digestRef, | ||
| Image: analyzedMD.RunImageImage(), | ||
| Extend: analyzedMD.RunImage.Extend, | ||
| TargetMetadata: targetData, | ||
| if err = updateAnalyzedMD(&analyzedMD, remoteRunImage); err != nil { | ||
| return cmd.FailErr(err, "update analyzed metadata") | ||
| } | ||
| } | ||
| if err = encoding.WriteTOML(r.AnalyzedPath, analyzedMD); err != nil { | ||
|
|
@@ -169,20 +151,47 @@ func (r *restoreCmd) Exec() error { | |
| return r.restore(appMeta, group, cacheStore) | ||
| } | ||
|
|
||
| func updateAnalyzedMD(analyzedMD *files.Analyzed, remoteRunImage imgutil.Image) error { | ||
| digestRef, err := remoteRunImage.Identifier() | ||
| if err != nil { | ||
| return cmd.FailErr(err, "get digest reference for run image") | ||
| } | ||
| targetData, err := platform.GetTargetMetadata(remoteRunImage) | ||
| if err != nil { | ||
| return cmd.FailErr(err, "read target data from run image") | ||
| } | ||
| cmd.DefaultLogger.Debugf("Run image info in analyzed metadata was: ") | ||
| cmd.DefaultLogger.Debugf(encoding.ToJSONMaybe(analyzedMD.RunImage)) | ||
| analyzedMD.RunImage.Reference = digestRef.String() | ||
| analyzedMD.RunImage.TargetMetadata = targetData | ||
| cmd.DefaultLogger.Debugf("Run image info in analyzed metadata is: ") | ||
| cmd.DefaultLogger.Debugf(encoding.ToJSONMaybe(analyzedMD.RunImage)) | ||
| return nil | ||
| } | ||
|
|
||
| func needsPulling(runImage *files.RunImage) bool { | ||
| return runImage != nil && runImage.Extend | ||
| if runImage == nil { | ||
| // sanity check to prevent panic, should be unreachable | ||
| return false | ||
| } | ||
| return runImage.Extend | ||
| } | ||
|
|
||
| func needsUpdating(runImage *files.RunImage) bool { | ||
| if runImage == nil { | ||
| // sanity check to prevent panic, should be unreachable | ||
| return false | ||
| } | ||
| if runImage.TargetMetadata != nil && runImage.TargetMetadata.OS != "" { | ||
| if runImage.Reference != "" && isPopulated(runImage.TargetMetadata) { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func isPopulated(metadata *files.TargetMetadata) bool { | ||
| return metadata != nil && metadata.OS != "" | ||
| } | ||
|
|
||
| func (r *restoreCmd) supportsBuildImageExtension() bool { | ||
| return r.PlatformAPI.AtLeast("0.10") | ||
| } | ||
|
|
@@ -201,9 +210,12 @@ func (r *restoreCmd) pullSparse(imageRef string) (imgutil.Image, error) { | |
| return nil, fmt.Errorf("failed to create cache directory: %w", err) | ||
| } | ||
| // get remote image | ||
| remoteImage, err := remote.NewV1Image(imageRef, r.keychain) | ||
| remoteImage, err := remote.NewImage(imageRef, r.keychain, remote.FromBaseImage(imageRef)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get remote image: %w", err) | ||
| return nil, fmt.Errorf("failed to initialize remote image: %w", err) | ||
| } | ||
| if !remoteImage.Found() { | ||
| return nil, fmt.Errorf("failed to get remote image") | ||
| } | ||
| // check for usable kaniko dir | ||
| if _, err := os.Stat(kanikoDir); err != nil { | ||
|
|
@@ -213,13 +225,15 @@ func (r *restoreCmd) pullSparse(imageRef string) (imgutil.Image, error) { | |
| return nil, nil | ||
| } | ||
| // save to disk | ||
| h, err := remoteImage.Digest() | ||
| h, err := remoteImage.UnderlyingImage().Digest() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get remote image digest: %w", err) | ||
| } | ||
| path := filepath.Join(baseCacheDir, h.String()) | ||
| cmd.DefaultLogger.Debugf("Saving image metadata to %s...", path) | ||
| sparseImage, err := sparse.NewImage( | ||
| filepath.Join(baseCacheDir, h.String()), | ||
| remoteImage, | ||
| path, | ||
| remoteImage.UnderlyingImage(), | ||
| layout.WithMediaTypes(imgutil.DefaultTypes), | ||
| ) | ||
| if err != nil { | ||
|
|
@@ -228,32 +242,7 @@ func (r *restoreCmd) pullSparse(imageRef string) (imgutil.Image, error) { | |
| if err = sparseImage.Save(); err != nil { | ||
| return nil, fmt.Errorf("failed to save sparse image: %w", err) | ||
| } | ||
| return sparseImage, nil | ||
| } | ||
|
|
||
| func digestReference(imageRef string, image imgutil.Image) (string, error) { | ||
| ir, err := name.ParseReference(imageRef) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| _, err = name.NewDigest(ir.String()) | ||
| if err == nil { | ||
| // if we already have a digest reference, return it | ||
| return imageRef, nil | ||
| } | ||
| id, err := image.Identifier() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| digest, err := name.NewDigest(id.String()) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| digestRef, err := name.NewDigest(fmt.Sprintf("%s@%s", ir.Context().Name(), digest.DigestStr()), name.WeakValidation) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return digestRef.String(), nil | ||
| return remoteImage, nil | ||
| } | ||
|
|
||
| func (r *restoreCmd) restoresLayerMetadata() bool { | ||
|
|
||
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.