Skip to content

[Refactor] Refactor the compose command flagging process#1818

Merged
AkihiroSuda merged 1 commit into
containerd:mainfrom
Laitr0n:refactor_compose
Jan 10, 2023
Merged

[Refactor] Refactor the compose command flagging process#1818
AkihiroSuda merged 1 commit into
containerd:mainfrom
Laitr0n:refactor_compose

Conversation

@Laitr0n
Copy link
Copy Markdown
Contributor

@Laitr0n Laitr0n commented Jan 6, 2023

  • Create a file in pkg/api/types/${cmd}_types.go, and define the CommandOption for this command
  • Create some file in pkg/cmd/${cmd}, and move the command entry point in real into this package

Signed-off-by: Laitron meetlq@outlook.com

@Laitr0n Laitr0n marked this pull request as ready for review January 6, 2023 14:18
@Laitr0n
Copy link
Copy Markdown
Contributor Author

Laitr0n commented Jan 6, 2023

Comment thread cmd/nerdctl/compose.go Outdated
Comment thread cmd/nerdctl/compose.go Outdated
@@ -82,135 +69,44 @@ func newComposeCommand() *cobra.Command {
}

func getComposer(cmd *cobra.Command, client *containerd.Client, globalOptions *types.GlobalCommandOptions) (*composer.Composer, error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

After the below suggestion, this getComposer becomes a wraper of 2 function calls: getComposeOptions in cmd side and GetComposer in pkg side.

Although it seems redundant, I think it's fine for now to keep it to minimize the change. Otherwise, you need to change every getCompoer call in compose commands to first call getComposeOptions and then call compose.GetComposer.

Comment thread pkg/cmd/compose/compose.go Outdated
"github.com/sirupsen/logrus"
)

func GetComposer(options *composer.Options, globalOptions *types.GlobalCommandOptions, client *containerd.Client, volStore volumestore.VolumeStore, stdout, stderr io.Writer) (*composer.Composer, error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can put this function here because most pkg side compose logic are here. And we might need some further refactor on GetComposer and composer.New (GetComposer is basically a New with options).

https://github.com/containerd/nerdctl/blob/49c0e571fec30e98b1265dcd85d15244abea5e19/pkg/composer/composer.go

@Laitr0n Laitr0n force-pushed the refactor_compose branch 3 times, most recently from a44f517 to 7fe89c3 Compare January 7, 2023 15:03
@Laitr0n Laitr0n requested a review from djdongjin January 7, 2023 15:04
Comment thread cmd/nerdctl/compose_build.go Outdated
Comment thread cmd/nerdctl/compose.go
options.Experimental, err = cmd.Flags().GetBool("experimental")
if err != nil {
return nil, err
}
Copy link
Copy Markdown
Member

@djdongjin djdongjin Jan 7, 2023

Choose a reason for hiding this comment

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

I think the original format of the first half function should serve us well right? (e.g., read cmd flags, and create composer.Options altogether) (so suggest not change the 1st half)

Meanwhile, as you can see in this whole function, it contains two types of code:

  1. cobra related flag process: it's not related to compose implementation details so we should keep it in cmd side;
  2. compose implementation details: e.g., those o.NetworkExists = ..., o.VolumeExists = ... etc. they are not related to cobra/cmd after we get the option values from the 1st part.

What I meant in my comment (#1791 (comment)) is that we should spit this two parts into two functions (so cobra logic stays in cmd and implementation goes to pkg):

  1. getComposeOptions(cmd *cobra.Command) composer.Options: basically the 1st half. after this call, composer.Options contains all option values needed for the implementation.
  2. GetComposer(opt composer.Options, client *containerd.Client, stdout, stderr io.Writer) (*composer.Composer, error): basically the 2nd half. It first adds (our default) implementation to option and then returns a composer. If we want to enable callers to provide other implementations via composeoption, we should check if the field if nil.
if o.EnsureImage == nil {
    o.EnsureImage = func(....
}

After this change, the workflow for a compose command to obtain a composer will be:

    // get option values from cmd
	options, err := getComposeOptions(cmd, client, globalOptions)
	if err != nil {
		return err
	}
    // get composer
	c, err := composer.GetComposer(options, client, cmd.OutOrStdout(), cmd.ErrOrStdErr())

Let me know if you have other questions. @Laitr0n

@AkihiroSuda, @Zheaoli WDYT? (does this make sense :))

@Laitr0n Laitr0n force-pushed the refactor_compose branch 3 times, most recently from 3f08056 to cfe0acf Compare January 8, 2023 04:27
@Laitr0n Laitr0n requested a review from djdongjin January 8, 2023 11:14
Comment thread pkg/cmd/compose/compose.go Outdated
Comment thread cmd/nerdctl/compose.go Outdated
Comment on lines -68 to -119
options := &composer.Options{}
options.NerdctlCmd, options.NerdctlArgs = globalFlags(cmd)
var err error
options.ProjectDirectory, err = cmd.Flags().GetString("project-directory")
if err != nil {
return nil, err
}
envFile, err := cmd.Flags().GetString("env-file")
options.EnvFile, err = cmd.Flags().GetString("env-file")
if err != nil {
return nil, err
}
projectName, err := cmd.Flags().GetString("project-name")
options.Project, err = cmd.Flags().GetString("project-name")
if err != nil {
return nil, err
}
debugFull := globalOptions.DebugFull
snapshotter := globalOptions.Snapshotter
files, err := cmd.Flags().GetStringArray("file")
options.DebugPrintFull, err = cmd.Flags().GetBool("debug-full")
if err != nil {
return nil, err
}
insecure := globalOptions.InsecureRegistry
cniPath := globalOptions.CNIPath
cniNetconfpath := globalOptions.CNINetConfPath
hostsDirs := globalOptions.HostsDir
experimental := globalOptions.Experimental

o := composer.Options{
Project: projectName,
ProjectDirectory: projectDirectory,
ConfigPaths: files,
EnvFile: envFile,
NerdctlCmd: nerdctlCmd,
NerdctlArgs: nerdctlArgs,
DebugPrintFull: debugFull,
Experimental: experimental,
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we keep this first half not changed (e.g., return o, nil directly here)? This part is only syntax changes and we can just do the split it into 2 functions. It makes the PR easy to review.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And the return type can be composer.Options (i.e., not a pointer)

Comment thread pkg/cmd/compose/compose.go Outdated
@Laitr0n Laitr0n force-pushed the refactor_compose branch 2 times, most recently from d66d864 to 520c421 Compare January 9, 2023 13:14
@Laitr0n Laitr0n requested a review from djdongjin January 9, 2023 13:33
Copy link
Copy Markdown
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.

LGTM on CI green, thanks

Comment thread pkg/composer/composer.go Outdated
Comment thread pkg/composer/composer.go Outdated
Signed-off-by: Laitron <meetlq@outlook.com>
@Laitr0n Laitr0n requested review from AkihiroSuda and removed request for Zheaoli January 10, 2023 02:28
@AkihiroSuda AkihiroSuda requested a review from djdongjin January 10, 2023 02:30
Copy link
Copy Markdown
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.

LGTM, thanks

Copy link
Copy Markdown
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit 40d341d into containerd:main Jan 10, 2023
@AkihiroSuda AkihiroSuda added this to the v1.2.0 milestone Jan 10, 2023
@Laitr0n Laitr0n deleted the refactor_compose branch January 10, 2023 13:44
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.

3 participants