-
Notifications
You must be signed in to change notification settings - Fork 884
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
Conversation
There was a problem hiding this 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}
done |
@@ -177,7 +177,7 @@ func (i *CommandInitOption) Complete() error { | |||
} | |||
} | |||
|
|||
return nil | |||
return os.MkdirAll(i.KarmadaDataPath, 0755) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
6c730a4
to
7cb0b4f
Compare
@@ -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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
7cb0b4f
to
0bce8fc
Compare
/assign |
// 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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
a6b6697
to
b992a3d
Compare
b992a3d
to
9a1a425
Compare
|
||
// initializeDirectory clean up the directory path if it exists, then recreate it | ||
func initializeDirectory(path string) error { | ||
// Initialize karmada-data directory. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// initializeDirectory clean up the directory path if it exists, then recreate it | |
// initializeDirectory initializes a directory and makes sure it's empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
9a1a425
to
8b0e9f3
Compare
Please fixed the lint issue. |
Signed-off-by: jwcesign <jiangwei115@huawei.com>
8b0e9f3
to
cdaa934
Compare
There was a problem hiding this 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.
[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 |
…-upstream-release-1.3 Automated cherry pick of #2548: Fix bugs about karmada-data dir lost
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
With
release-1.2
worksWhich issue(s) this PR fixes:
Fixes bugs about dir lost
Special notes for your reviewer:
Does this PR introduce a user-facing change?: