Skip to content
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

Refactor the package structure in cmd/nerdctl #1639

Closed
wants to merge 14 commits into from

Conversation

Zheaoli
Copy link
Member

@Zheaoli Zheaoli commented Dec 11, 2022

Fix #1631

First, I'm sorry that I make this PR too large to review. I have to make a one-shot PR if I want to re-structured the directory because the code in the cmd/nerdctl has been mixed,

In this PR, I have made four things below

  1. split the code file in the cmd/nerdctl into subdirectory which the subcommand has organized
  2. make generic code into a common utils package in the cmd/nerdctl/utils
  3. move the shell-completion feature into completion package in the cmd/nerdct/completion
  4. move all the e2e test together.

Signed-off-by: Zheao.Li me@manjusaka.me

@Zheaoli Zheaoli marked this pull request as draft December 11, 2022 06:04
@AkihiroSuda AkihiroSuda added this to the v1.1.1 (tentative) milestone Dec 11, 2022
@Zheaoli Zheaoli force-pushed the manjusaka/support-1631 branch 5 times, most recently from 154b510 to 4d114ab Compare December 12, 2022 14:36
Copy link
Member

@djdongjin djdongjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: integration might be a better name than e2e

@Zheaoli
Copy link
Member Author

Zheaoli commented Dec 12, 2022

nit: integration might be a better name than e2e

nice catch(

@Zheaoli Zheaoli marked this pull request as ready for review December 12, 2022 14:41
@Zheaoli Zheaoli marked this pull request as draft December 12, 2022 14:42
@@ -26,6 +26,8 @@ import (

"github.com/containerd/containerd/events"
"github.com/containerd/containerd/log"
client2 "github.com/containerd/nerdctl/cmd/nerdctl/client"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the package name to be like ncclient

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will clean dirty name when I pass all CI(lol

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another suggestion is ndclient 😃 since nd reads more similar to nerdctl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue that the contaiNERD client library is also "nerd" 🤓

@@ -26,6 +26,8 @@ import (

"github.com/containerd/containerd/events"
"github.com/containerd/containerd/log"
client2 "github.com/containerd/nerdctl/cmd/nerdctl/client"
fmt2 "github.com/containerd/nerdctl/cmd/nerdctl/utils"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the package name to be something like utils/fmtutil.

@@ -14,16 +14,20 @@
limitations under the License.
*/

package main
package create
Copy link
Member

@AkihiroSuda AkihiroSuda Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in the nerdctl/container package, or maybe better in the nerdctl/container/create pkg?
Because the "canonical" form of nerdctl create is nerdctl container create.

Same applies to the run, exec, cp commands, etc.

Also, nerdctl info should be in nerdctl/system or nerdctl/system/info.

@@ -30,6 +30,8 @@ import (
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/images/converter"
"github.com/containerd/containerd/images/converter/uncompress"
client2 "github.com/containerd/nerdctl/cmd/nerdctl/client"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can change either the package name (something like cliclient or nerdclient) or rename the variable client to avoid client2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have already fix(

@Zheaoli Zheaoli force-pushed the manjusaka/support-1631 branch 2 times, most recently from 87fa51d to 8e52853 Compare December 12, 2022 18:40
@Zheaoli Zheaoli marked this pull request as ready for review December 12, 2022 19:06
@Zheaoli Zheaoli requested review from monirul, AkihiroSuda and djdongjin and removed request for monirul and djdongjin December 12, 2022 19:08
Revert unexpected changes in 5588f70

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
@AkihiroSuda
Copy link
Member

AkihiroSuda commented Dec 16, 2022

A bunch of functions seems misplaced in wrong files. (See my follow-up commits above)

I'm trying to fix them, but if I fail to do that by the end of the day, I'll prioritize merging other PRs.
But I fear that basically this PR isn't rebaseable after merging other PRs...

@AkihiroSuda
Copy link
Member

And I also don't want to split the integration test package from the command packages

Copy link
Member

@ktock ktock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to fix them, but if I fail to do that by the end of the day, I'll prioritize merging other PRs.
But I fear that basically this PR isn't rebaseable after merging other PRs...

Thank you for the following up. Possible plan B might be incrementally separating out cobra-independent functions into pkg directory but I'm not sure we need to do so because codes under cmd/nerdctl are tightly coupled. (cc @Zheaoli)

Comment on lines +56 to +57
func InspectAction(cmd *cobra.Command, args []string) error {
client, ctx, cancel, err := ncclient.New(cmd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exported functions should have godoc. In this case, XAction seems prints formatted strings to stdout so godoc should include the information about the format so that the caller can make sure it prints the expected outputs.
Maybe, exported functions should be independent from cobra and stdio.

"github.com/containerd/containerd/cio"
)

func PauseContainer(ctx context.Context, client *containerd.Client, id string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can put this under pkg dir because it doesn't have cobra dependency?

// formatSlice is expected to be only used for `nerdctl OBJECT inspect` commands.
func formatSlice(cmd *cobra.Command, x []interface{}) error {
// FormatSlice is expected to be only used for `nerdctl OBJECT inspect` commands.
func FormatSlice(cmd *cobra.Command, x []interface{}) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the purpose of the function is formatting rather than printing, I think it should receive io.Writer rather than *cobra.Command.

@djdongjin
Copy link
Member

I'm trying to fix them, but if I fail to do that by the end of the day, I'll prioritize merging other PRs.
But I fear that basically this PR isn't rebaseable after merging other PRs...

Probably we can prioritize bugfix PRs which usually happens in pkg side, and wait a little bit on feature PRs?

@AkihiroSuda
Copy link
Member

Sorry, I have to admit that I'm unable to review/carry this amount of PR 🙇‍♂️

Possible plan B might be incrementally separating out cobra-independent functions into pkg directory but I'm not sure we need to do so because codes under cmd/nerdctl are tightly coupled.

Yes, this should be done in a series of small PRs if we want to continue this.
e.g.,

  • PR 1: move cobra-independent functions into pkg/...
  • PR 2: split other common functions into somewhere like cmd/nerdctl/cmdpkg/...
  • PR 3: split the client cmdpkg
  • PR 4: split nerdctl namespace
  • PR 5: split nerdctl apparmor
  • PR 6: split nerdctl builder
  • PR 7: ...

The git diff has to be kept minimal for ease of reviewing and potential rebasing.
It mustn't contain unnecessary changes such as exposing private functions.
Some private functions could be exposed, but it has to be done in a separate PR.

Also, there shouldn't be the common package that is likely to result in being the home of random mutually irrelevant functions; the common package has to be split to multiple small cmdpkgs with clear purposes.


We also have to revisit whether we really have to split cmd/nerdctl in the first place ( #1631 (comment) ), as the functions in cmd/nerdctl aren't really designed to be imported from other projects.
There might be some functions that can be safely imported from third parties, but they should be rather decoupled from the CLI and should be moved to pkg/... .

Then there might be no good reason to split cmd/nerdctl ?
I expected that splitting cmd/nerdctl will increase the code readability as a side effect, but improper splitting will rather decrease the readability, so it has to be done seriously carefully.

@Zheaoli
Thanks anyway for your hard work, and hope you will get well soon! 👍

@Zheaoli
Copy link
Member Author

Zheaoli commented Dec 16, 2022

Sorry, I have to admit that I'm unable to review/carry this amount of PR 🙇‍♂️

Possible plan B might be incrementally separating out cobra-independent functions into pkg directory but I'm not sure we need to do so because codes under cmd/nerdctl are tightly coupled.

Yes, this should be done in a series of small PRs if we want to continue this.

e.g.,

  • PR 1: move cobra-independent functions into pkg/...

  • PR 2: split other common functions into somewhere like cmd/nerdctl/cmdpkg/...

  • PR 3: split the client cmdpkg

  • PR 4: split nerdctl namespace

  • PR 5: split nerdctl apparmor

  • PR 6: split nerdctl builder

  • PR 7: ...

The git diff has to be kept minimal for ease of reviewing and potential rebasing.

It mustn't contain unnecessary changes such as exposing private functions.

Some private functions could be exposed, but it has to be done in a separate PR.

Also, there shouldn't be the common package that is likely to result in being the home of random mutually irrelevant functions; the common package has to be split to multiple small cmdpkgs with clear purposes.


We also have to revisit whether we really have to split cmd/nerdctl in the first place ( #1631 (comment) ), as the functions in cmd/nerdctl aren't really designed to be imported from other projects.

There might be some functions that can be safely imported from third parties, but they should be rather decoupled from the CLI and should be moved to pkg/... .

Then there might be no good reason to split cmd/nerdctl ?

I expected that splitting cmd/nerdctl will increase the code readability as a side effect, but improper splitting will rather decrease the readability, so it has to be done seriously carefully.

@Zheaoli

Thanks anyway for your hard work, and hope you will get well soon! 👍

I'm sorry for this huge PR, very sorry. It's my bad, the effort scope is beyond my think when I start this process.

I will close this PR first, and I will make a refactor proposal to design the refactor step.

Sorry again for this damn huge PR...

@AkihiroSuda
Copy link
Member

Not "damn", don't blame your self ❤️

@Zheaoli Zheaoli closed this Dec 16, 2022
@Zheaoli Zheaoli deleted the manjusaka/support-1631 branch December 16, 2022 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move *.go files for subcommand out main package
7 participants