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] Move containerd client helper from cmd to pkg/clientutil #1780

Merged
merged 1 commit into from
Dec 31, 2022

Conversation

Zheaoli
Copy link
Member

@Zheaoli Zheaoli commented Dec 29, 2022

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

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.

This snippet is duplicated a lot

	namespace, err := cmd.Flags().GetString("namespace")
	if err != nil {
		return err
	}
	address, err := cmd.Flags().GetString("address")
	if err != nil {
		return err
	}
	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)

I would suggest that we have a cmd/nerdctl/cmdutil.go which contains this kind of code if they're very common (e.g., read some flags from cmd and call the corresponding pkg functions). So in this case:

// cmd/nerdctl/cmdutil.go, ignore types....
func newClientFromCmd(cmd) client, ctx, cancel, err {
	namespace, err := cmd.Flags().GetString("namespace")
	if err != nil {
		return err
	}
	address, err := cmd.Flags().GetString("address")
	if err != nil {
		return err
	}
	return clientutil.NewClient(cmd.Context(), namespace, address)
}

Personally, I think it still make sense to have 2 functions (newClientFromCmd, and your clientutil.NewClient) compared to the old newClient which does two jobs.

@Zheaoli
Copy link
Member Author

Zheaoli commented Dec 29, 2022

I would suggest that we have a cmd/nerdctl/cmdutil.go which contains this kind of code if they're very common (e.g., read some flags from cmd and call the corresponding pkg functions). So in this case:

This shouldn't be a problem. after the #1680 is completed, all the global flagging processes will share a command helper function getGlobalFlag (like namespace, address etc)(Also this getGlobalFlag helper function would be used at in anywhere need to process the global flag)

I want to make the NewClient out of the cmd package because most of the command logic we want to split into pkg/cmd packages is related to this function. In other words, if we want to split the command logic out of the cmd package, we need to make NewClient out of cmd package first.

Like @AkihiroSuda has said in #1779 (comment), the code in pkg/** should not be related to cobra.

As I have said above, we will have a getGlobalFlag helper function, so the duplicated code here will be replaced by the code below in each command PR

globalFlag,err := getGlobalFlag(cmd)
if err!=nil{
    return err
}
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalFlag.Address, globalFlag.Namespace)

So I think the new wrapper function newClientFromCmd in cmd package should be unnecessary. For personal style, I would like to make the cmd package clean as we can

So IMHO, I think this PR is OK for now. But maybe we can introduce GlobalFlag into this PR? (I'm not sure

@djdongjin
Copy link
Member

:) Overall the PR LGTM and I agree both the refactor and the PR is necessary.

My suggestion is more a nit without much context, which can be seen as "add a 10 line helper in cmd side to dedup some duplication code".

Like @AkihiroSuda has said in #1779 (comment), the code in pkg/** should not be related to cobra.

The helper I suggested is also on the cmd side (simply read 2 flags from cmd and call your new function).

Just repeat, it's a nit and the PR is not blocked on me 😉 thanks for your work!

@Zheaoli Zheaoli force-pushed the manjusaka/split-client branch 2 times, most recently from 38dfd3d to 2b9347e Compare December 30, 2022 11:22
@Zheaoli Zheaoli force-pushed the manjusaka/split-client branch 2 times, most recently from 66dc6a3 to 8f58f59 Compare December 30, 2022 13:19
@fahedouch
Copy link
Member

I prefer to go through the refacto in small steps even if it introduces unnecessary/redundant code, it would make the process of contributing easier. LGTM to retains redundancy code until progressive introduction of getGlobalFlag

cmd/nerdctl/build.go Outdated Show resolved Hide resolved
cmd/nerdctl/build.go Show resolved Hide resolved
@Laitr0n
Copy link
Contributor

Laitr0n commented Dec 31, 2022

When I refactor the 'compose log' command, I realized that the 'client helper' should be refactored first. After this PR, can the refactoring of the 'compose xxx' command continue?

Copy link
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 added this to the v1.2.0 milestone Dec 31, 2022
@AkihiroSuda AkihiroSuda merged commit 11c6ff3 into containerd:main Dec 31, 2022
@Zheaoli Zheaoli deleted the manjusaka/split-client branch January 7, 2023 16:08
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.

None yet

5 participants