-
Notifications
You must be signed in to change notification settings - Fork 54
fix: handle single arch images #489
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
+271
−181
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package imagehandler | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/crane" | ||
| v1 "github.com/google/go-containerregistry/pkg/v1" | ||
| "github.com/google/go-containerregistry/pkg/v1/empty" | ||
| "github.com/google/go-containerregistry/pkg/v1/layout" | ||
| "go.uber.org/zap" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| // Handler processes a fetched image, e.g. exporting it to local storage. | ||
| type Handler func(ctx context.Context, logger *zap.Logger, img v1.Image) error | ||
|
|
||
| // Export streams the image's flattened filesystem as a tar to exportHandler. | ||
| func Export(exportHandler func(logger *zap.Logger, r io.Reader) error) Handler { | ||
| return func(_ context.Context, logger *zap.Logger, img v1.Image) error { | ||
| logger.Info("extracting the image") | ||
|
|
||
| r, w := io.Pipe() | ||
|
|
||
| var eg errgroup.Group | ||
|
|
||
| eg.Go(func() error { | ||
| defer w.Close() //nolint:errcheck | ||
|
|
||
| return crane.Export(img, w) | ||
| }) | ||
|
|
||
| eg.Go(func() error { | ||
| err := exportHandler(logger, r) | ||
| if err != nil { | ||
| r.CloseWithError(err) // signal the exporter to stop | ||
| } | ||
|
|
||
| return err | ||
| }) | ||
|
|
||
| if err := eg.Wait(); err != nil { | ||
| return fmt.Errorf("error extracting the image: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // OCI exports the image to an OCI layout at path. | ||
| func OCI(path string) Handler { | ||
| return func(_ context.Context, logger *zap.Logger, img v1.Image) error { | ||
| if err := os.RemoveAll(path); err != nil { | ||
| return fmt.Errorf("error removing the directory %q: %w", path, err) | ||
| } | ||
|
|
||
| l, err := layout.Write(path, empty.Index) | ||
| if err != nil { | ||
| return fmt.Errorf("error creating layout: %w", err) | ||
| } | ||
|
|
||
| logger.Info("exporting the image", zap.String("destination", path)) | ||
|
|
||
| var opts []layout.Option | ||
|
|
||
| // go-containerregistry's partial.Descriptor only copies Platform onto the index entry | ||
| // when the image was resolved from a multi-arch index. Derive platform from the image's | ||
| // config file if present so single arch images keep working. | ||
| if cfg, cfgErr := img.ConfigFile(); cfgErr == nil && cfg.Architecture != "" { | ||
| opts = append(opts, layout.WithPlatform(v1.Platform{ | ||
| OS: cfg.OS, | ||
| Architecture: cfg.Architecture, | ||
| Variant: cfg.Variant, | ||
| })) | ||
| } | ||
|
|
||
| if err = l.AppendImage(img, opts...); err != nil { | ||
| return fmt.Errorf("error exporting the image: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package imagehandler_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| v1 "github.com/google/go-containerregistry/pkg/v1" | ||
| "github.com/google/go-containerregistry/pkg/v1/layout" | ||
| "github.com/google/go-containerregistry/pkg/v1/mutate" | ||
| "github.com/google/go-containerregistry/pkg/v1/random" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap/zaptest" | ||
|
|
||
| "github.com/siderolabs/image-factory/internal/artifacts/imagehandler" | ||
| ) | ||
|
|
||
| // TestOCIRecordsPlatform asserts that a single-arch image is written to the OCI layout with a | ||
| // platform descriptor, so the Talos imager can match it to a target architecture. | ||
| func TestOCIRecordsPlatform(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| img, err := random.Image(1024, 1) | ||
|
Orzelius marked this conversation as resolved.
|
||
| require.NoError(t, err) | ||
|
|
||
| // Single-arch images (e.g. custom-built extensions) carry their arch only in the config. | ||
| img, err = mutate.ConfigFile(img, &v1.ConfigFile{OS: "linux", Architecture: "arm64"}) | ||
| require.NoError(t, err) | ||
|
|
||
| path := t.TempDir() + "/oci" | ||
|
|
||
| require.NoError(t, imagehandler.OCI(path)(t.Context(), zaptest.NewLogger(t), img)) | ||
|
|
||
| idx, err := layout.ImageIndexFromPath(path) | ||
| require.NoError(t, err) | ||
|
|
||
| manifest, err := idx.IndexManifest() | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, manifest.Manifests, 1) | ||
| require.NotNil(t, manifest.Manifests[0].Platform, "platform descriptor must be recorded") | ||
| require.Equal(t, "linux", manifest.Manifests[0].Platform.OS) | ||
| require.Equal(t, "arm64", manifest.Manifests[0].Platform.Architecture) | ||
| } | ||
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.
This here is the only change