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: make run command args dealing more explicit #1500

Merged
merged 1 commit into from
Jun 22, 2022

Conversation

allencloud
Copy link
Member

Signed-off-by: Allen Sun [email protected]

Describe what this PR does / why we need it

Almost make sealer run command argument dealing more explicitly. Here I carry some basic pricinples:

  • always use defensive programming to pre-process all the input raw data from command, make all invalid and unformatted data out of the inside main code procedure;
  • make the data passing as less as possible. When you need to pass only one int n value, just pass this number int, no need to pass the all struct which contains the int n value. When data flow is just sufficient, the code readability would be improved a lot. And beginners do not have to learn the struct instance, and just know the init n value meaning.
  • avoid changing the input pointer as possible, only if you have to. Especially do not do that in some built-in packages.

Based on the above principles, this pull request did:

  • remove the ClusterArgs, which seems to have very short-time lifecycle and may not be necessary;
  • prepare everything for a future cluster, and finally initialize a cluster instance. Rather than initialize a cluster instance, and keep putting more attributes in the instance.
  • Avoid to use pointer for simple type, such as string. It would introduce more complexity and reduce code readability;

Does this pull request fix one issue?

none

Describe how you did it

none

Describe how to verify it

none

Special notes for reviews

none

@codecov-commenter
Copy link

Codecov Report

Merging #1500 (99726a2) into main (ec33c1e) will increase coverage by 0.01%.
The diff coverage is 4.87%.

@@            Coverage Diff             @@
##             main    #1500      +/-   ##
==========================================
+ Coverage   10.64%   10.65%   +0.01%     
==========================================
  Files          84       84              
  Lines        7588     7609      +21     
==========================================
+ Hits          808      811       +3     
- Misses       6678     6697      +19     
+ Partials      102      101       -1     
Impacted Files Coverage Δ
apply/apply.go 0.00% <ø> (ø)
apply/run.go 0.00% <0.00%> (ø)
apply/scale.go 10.78% <0.00%> (-1.18%) ⬇️
pkg/plugin/taint_plugin.go 43.33% <100.00%> (+2.50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ec33c1e...99726a2. Read the comment docs.

@github-actions github-actions bot added the doc Improvements or additions to documentation label Jun 10, 2022
@allencloud allencloud force-pushed the refac-cmd-args branch 4 times, most recently from e276f35 to f70ba98 Compare June 11, 2022 03:54
ipStr: "10.110.101.1-10.110.101.5",
wantErr: false,
},
/*{
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure why it would fail if I add this case.

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure why it would fail if I add this case.

Maybe because 0.x.x.x is not a real ip address

@allencloud allencloud force-pushed the refac-cmd-args branch 6 times, most recently from c4fa01f to ecbd521 Compare June 21, 2022 02:08
apply/run.go Outdated
hosts []v2.Host
func ConstructClusterFromArg(imageName string, runArgs *Args) (*v2.Cluster, error) {
var passwd string
if runArgs.Password != "" {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe SSH.Passwd can be set directly to runArgs.Password, when runArgs.Password is empty, passwd is also empty.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, roger that.

}

// 2. validate if it is IP list, like 192.168.0.5,192.168.0.6,192.168.0.7
for _, ip := range strings.Split(inputStr, ",") {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to skip empty cases like: x.x.x.x,,x.x.x.y

Copy link
Member Author

Choose a reason for hiding this comment

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

I think x.x.x.x,,x.x.x.y is invalid. This would be split into a string array of []string{"x.x.x.x", "","x.x.x.y"}, and net.ParseIP("") will report error.

Copy link
Member Author

Choose a reason for hiding this comment

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

We should make the validation as strictly as possible at the very beginning. It could reduce much more complexity. And it could make end-users feel explicit. This time I remove the x.x.x.x:port, since it is quite complicated. We can add this after refactoring if necessary then.

apply/run.go Outdated
Port: strconv.Itoa(int(c.runArgs.Port)),
}
// validate input nodes IP info
if err := validateIPStr(runArgs.Nodes); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

When we run a cluster with only master, we get an error because the worker nodes are empty.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, So good catch. Thanks greatly for your careful review. Updated, now.

return nil
var ips = strings.Split(ipStr, "-")
if ipStr == "" || !strings.Contains(ipStr, "-") {
return "", nil
Copy link
Member

Choose a reason for hiding this comment

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

The previous method was to change the value of the pointer, now we need to return itself if we don't penetrate the pointer type.

@bxy4543 bxy4543 self-assigned this Jun 22, 2022
@allencloud allencloud merged commit 421d4fd into sealerio:main Jun 22, 2022
@allencloud allencloud deleted the refac-cmd-args branch June 22, 2022 09:28
bxy4543 pushed a commit to bxy4543/sealer that referenced this pull request Jun 27, 2022
@kakaZhou719 kakaZhou719 added optimization refactor sealer code refactor related and removed doc Improvements or additions to documentation test plugin labels Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization refactor sealer code refactor related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants