-
Notifications
You must be signed in to change notification settings - Fork 772
[Refactor] Refactor the load command flagging process #1820
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
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package types | ||
|
|
||
| type LoadCommandOptions struct { | ||
| GOptions GlobalCommandOptions | ||
| // Input read from tar archive file, instead of STDIN | ||
| Input string | ||
| // Platform import content for a specific platform | ||
| Platform []string | ||
| // AllPlatforms import content for all platforms | ||
| AllPlatforms bool | ||
| } |
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,119 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package load | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/containerd/containerd" | ||
| "github.com/containerd/containerd/archive/compression" | ||
| "github.com/containerd/containerd/images" | ||
| "github.com/containerd/containerd/images/archive" | ||
| "github.com/containerd/containerd/platforms" | ||
| "github.com/containerd/nerdctl/pkg/api/types" | ||
| "github.com/containerd/nerdctl/pkg/clientutil" | ||
| "github.com/containerd/nerdctl/pkg/platformutil" | ||
| ) | ||
|
|
||
| type readCounter struct { | ||
| io.Reader | ||
| N int | ||
| } | ||
|
|
||
| func (r *readCounter) Read(p []byte) (int, error) { | ||
| n, err := r.Reader.Read(p) | ||
| if n > 0 { | ||
| r.N += n | ||
| } | ||
| return n, err | ||
| } | ||
|
|
||
| func Load(ctx context.Context, stdin io.Reader, stdout io.Writer, options types.LoadCommandOptions) error { | ||
| if options.Input != "" { | ||
| f, err := os.Open(options.Input) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() | ||
| stdin = f | ||
| } else { | ||
| // check if stdin is empty. | ||
| stdinStat, err := os.Stdin.Stat() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if stdinStat.Size() == 0 && (stdinStat.Mode()&os.ModeNamedPipe) == 0 { | ||
| return errors.New("stdin is empty and input flag is not specified") | ||
| } | ||
| } | ||
| decompressor, err := compression.DecompressStream(stdin) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| platMC, err := platformutil.NewMatchComparer(options.AllPlatforms, options.Platform) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return loadImage(ctx, decompressor, stdout, options, platMC, false) | ||
| } | ||
|
|
||
| func loadImage(ctx context.Context, in io.Reader, stdout io.Writer, options types.LoadCommandOptions, platMC platforms.MatchComparer, quiet bool) error { | ||
| // In addition to passing WithImagePlatform() to client.Import(), we also need to pass WithDefaultPlatform() to NewClient(). | ||
| // Otherwise unpacking may fail. | ||
|
|
||
| client, ctx, cancel, err := clientutil.NewClient(ctx, options.GOptions.Namespace, options.GOptions.Address, containerd.WithDefaultPlatform(platMC)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer cancel() | ||
|
|
||
| r := &readCounter{Reader: in} | ||
| imgs, err := client.Import(ctx, r, containerd.WithDigestRef(archive.DigestTranslator(options.GOptions.Snapshotter)), containerd.WithSkipDigestRef(func(name string) bool { return name != "" }), containerd.WithImportPlatform(platMC)) | ||
| if err != nil { | ||
| if r.N == 0 { | ||
| // Avoid confusing "unrecognized image format" | ||
| return errors.New("no image was built") | ||
| } | ||
| if errors.Is(err, images.ErrEmptyWalk) { | ||
| err = fmt.Errorf("%w (Hint: set `--platform=PLATFORM` or `--all-platforms`)", err) | ||
| } | ||
| return err | ||
| } | ||
| for _, img := range imgs { | ||
| image := containerd.NewImageWithPlatform(client, img, platMC) | ||
|
|
||
| // TODO: Show unpack status | ||
| if !quiet { | ||
| fmt.Fprintf(stdout, "unpacking %s (%s)...\n", img.Name, img.Target.Digest) | ||
| } | ||
| err = image.Unpack(ctx, options.GOptions.Snapshotter) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if quiet { | ||
| fmt.Fprintln(stdout, img.Target.Digest) | ||
| } else { | ||
| fmt.Fprintf(stdout, "Loaded image: %s\n", img.Name) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
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.
no change required here, but we might gradually change the pointer usage of global option to non-pointer to avoid unintented shared memory among goroutines (#1795 (comment)) and also be consistent with other command option usages.
processRootCmdFlagsshould be changed to return a struct not a struct pointer. Similarly, SubCommandOptions should hold a global option struct (as inLoadCommandOptions) not a struct pointer.I guess the original intent of using global option as a pointer is to avoid unnecessary copies, but I think it should be fine as those option structs are quite small, and also pointers use heap allocation which means more GC work.
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 will make a split PR to update it