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

karmadactl: Fix karmada-data directory not inilization isssue #2548

Merged
merged 1 commit into from
Sep 22, 2022

Conversation

jwcesign
Copy link
Member

@jwcesign jwcesign commented Sep 20, 2022

Signed-off-by: jwcesign jiangwei115@huawei.com

What type of PR is this?
/kind bug

What this PR does / why we need it:
This PR cause following error: #2297

jw@ecs-3fa1 [02:58:20 PM] [~/workspace/git/karmada-diff/karmada] [release-1.3]
-> % kubectl karmada init  --karmada-data=/home/jw/workspace/karmada_data
I0920 14:58:26.229852 3193825 deploy.go:145] kubeconfig file: /home/jw/.kube/karmada.config, kubernetes: https://192.168.1.237:42213
I0920 14:58:26.244413 3193825 deploy.go:165] karmada apiserver ip: [172.18.0.6]
I0920 14:58:26.620751 3193825 cert.go:229] Generate ca certificate success.
I0920 14:58:26.845036 3193825 cert.go:229] Generate karmada certificate success.
I0920 14:58:27.031043 3193825 cert.go:229] Generate apiserver certificate success.
I0920 14:58:27.088533 3193825 cert.go:229] Generate front-proxy-ca certificate success.
I0920 14:58:27.435088 3193825 cert.go:229] Generate front-proxy-client certificate success.
I0920 14:58:27.610710 3193825 cert.go:229] Generate etcd-ca certificate success.
I0920 14:58:27.725861 3193825 cert.go:229] Generate etcd-server certificate success.
I0920 14:58:27.844586 3193825 cert.go:229] Generate etcd-client certificate success.
I0920 14:58:27.844724 3193825 deploy.go:251] download crds file name: /home/jw/workspace/karmada_data/crds.tar.gz
error: prepare karmada failed.open /home/jw/workspace/karmada_data/crds.tar.gz: no such file or directory

With release-1.2 works

Which issue(s) this PR fixes:
Fixes bugs about dir lost

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

`karmadactl`: Fixed `--karmada-data` directory not initialization issue in `init` command.

@karmada-bot karmada-bot added the kind/bug Categorizes issue or PR as related to a bug. label Sep 20, 2022
@karmada-bot karmada-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Sep 20, 2022
Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

@jwcesign, can you help update the release note, the format can be as follows:

{component name}: {fix error information}

@XiShanYongYe-Chang
Copy link
Member

/cc @lonelyCZ @carlory can you help take a review?

@jwcesign
Copy link
Member Author

release

done

@jwcesign
Copy link
Member Author

cc @RainbowMango

@@ -177,7 +177,7 @@ func (i *CommandInitOption) Complete() error {
}
}

return nil
return os.MkdirAll(i.KarmadaDataPath, 0755)
Copy link
Member

Choose a reason for hiding this comment

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

Can you remind me why just removes the KarmadaDataPath and create it again?

Copy link
Member Author

Choose a reason for hiding this comment

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

based on this PR: #1455

When the current host uses init to install karmada multiple times, the installation fails because the data directory of karmada is not empty.

After remove it, it's not create it again then it will has error here:

if err := utils.DownloadFile(i.CRDs, filename); 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.

I just thought about that it is better not to remove whole directory, if users set a important existed directory, the command will remove it, which is dangerous. Like kubeamd reset, it only remove the files that created by kubeadm init. Perhaps, we can enhence it in a new PR.

For this pr, I think it is fine.

@@ -171,13 +171,17 @@ func (i *CommandInitOption) Complete() error {
}

// Determine whether KarmadaDataPath exists, if so, delete it
// Related issue: https://github.com/karmada-io/karmada/pull/1455
Copy link
Member

Choose a reason for hiding this comment

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

we don't need to explain it in code. It can be recorded by git commit

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's exactly what I want to say. We don't need to describe the history of the code, we just need to explain why we do it.

if utils.IsExist(i.KarmadaDataPath) {
if err := os.RemoveAll(i.KarmadaDataPath); err != nil {
return err
}
}

return nil
// Create this dir, next steps will download crds to this dir
Copy link
Member

Choose a reason for hiding this comment

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

We can add comments here.

Copy link
Member Author

Choose a reason for hiding this comment

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

U mean: add comment like TODO: enhence this operation like kubeadm reset?

Copy link
Member

Choose a reason for hiding this comment

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

No. The comment could be shorter and clearer. Like above comments

// When the current host uses init to install karmada multiple times, the installation fails
// because the data directory of karmada is not empty.

It is not necessary to describe the install karmada multiple times that should be in PR description rather than in code.

Copy link
Member Author

Choose a reason for hiding this comment

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

how about this:

	// The installation will fail because of the history data, so delete it and create it again
	if utils.IsExist(i.KarmadaDataPath) {
		if err := os.RemoveAll(i.KarmadaDataPath); err != nil {
			return err
		}
	}

	return os.MkdirAll(i.KarmadaDataPath, os.FileMode(0755))
}

cc @lonelyCZ

@RainbowMango
Copy link
Member

/assign

@RainbowMango RainbowMango changed the title Fix bugs about karmada-data dir lost karmadactl: Fix karmada-data directory not inilization isssue Sep 21, 2022
Comment on lines 173 to 180
// When the current host uses init to install karmada multiple times, the installation fails
// because the data directory of karmada is not empty.
if utils.IsExist(i.KarmadaDataPath) {
if err := os.RemoveAll(i.KarmadaDataPath); err != nil {
return err
}
}

return nil
// Create this dir, next steps will download crds to this dir
return os.MkdirAll(i.KarmadaDataPath, os.FileMode(0755))
Copy link
Member

Choose a reason for hiding this comment

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

My suggestion:

	// Initialize karmada-data directory.
	// If the directory already exists, just clean it up.
	if utils.IsExist(i.KarmadaDataPath) {
		if err := os.RemoveAll(i.KarmadaDataPath); err != nil {
			return err
		}
	}
	if err := os.MkdirAll(i.KarmadaDataPath, os.FileMode(0755)); err != nil {
		return fmt.Errorf("failed to create directory: %s, error: %v", i.KarmadaDataPath, err)
	}

	return nil

Copy link
Member Author

Choose a reason for hiding this comment

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

cyclomatic complexity execced, how about extract one function:

		}
	}

	return cleanupDataDirectory(i.KarmadaDataPath)
}

// cleanupDataDirectory clean up the karamda data directory
func cleanupDataDirectory(karmadaDataPath string) error {
	// Initialize karmada-data directory.
	// If the directory already exists, just clean it up.
	if utils.IsExist(karmadaDataPath) {
		if err := os.RemoveAll(karmadaDataPath); err != nil {
			return err
		}
	}
	if err := os.MkdirAll(karmadaDataPath, os.FileMode(0755)); err != nil {
		return fmt.Errorf("failed to create directory: %s, error: %v", karmadaDataPath, err)
	}

	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.

What we are going to do is ensure a directory exists and is clean, more than cleanup.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@jwcesign jwcesign force-pushed the fix-dir-lost branch 3 times, most recently from a6b6697 to b992a3d Compare September 21, 2022 09:29
@karmada-bot karmada-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 21, 2022

// initializeDirectory clean up the directory path if it exists, then recreate it
func initializeDirectory(path string) error {
// Initialize karmada-data directory.
Copy link
Member

Choose a reason for hiding this comment

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

It's ok to introduce a function here, but the comment might be inappropriate, as it's not dedicated to karmada-data now. It's a common function now.

Copy link
Member

Choose a reason for hiding this comment

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

I guess we can add a unit test for it.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed, and add ut case

return initializeDirectory(i.KarmadaDataPath)
}

// initializeDirectory clean up the directory path if it exists, then recreate it
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// initializeDirectory clean up the directory path if it exists, then recreate it
// initializeDirectory initializes a directory and makes sure it's 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.

fixed

@karmada-bot karmada-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 22, 2022
@jwcesign
Copy link
Member Author

cc @RainbowMango

@RainbowMango
Copy link
Member

Please fixed the lint issue.

Signed-off-by: jwcesign <jiangwei115@huawei.com>
Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

Please help to cherry-pick this to release-1.3.

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 22, 2022
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 22, 2022
@karmada-bot karmada-bot merged commit 6102f41 into karmada-io:master Sep 22, 2022
karmada-bot added a commit that referenced this pull request Sep 22, 2022
…-upstream-release-1.3

Automated cherry pick of #2548: Fix bugs about karmada-data dir lost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants