Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Powell <44844360+spowelljr@users.noreply.github.com>
  • Loading branch information
ComradeProgrammer and spowelljr committed Jan 27, 2025
1 parent 30044cf commit 60e2932
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
43 changes: 25 additions & 18 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,24 +757,31 @@ func deleteKnownHosts() {
}
for _, file := range fileInfo {
if file.IsDir() {
nodeName := file.Name()

knownHostPath := filepath.Join(localpath.MiniPath(), "machines", nodeName, "known_host")
if _, err := os.Stat(knownHostPath); err == nil {
// if this file exists, remove this line from known_hosts
key, err := os.ReadFile(knownHostPath)
if err != nil {
klog.Warningf("error reading keys from %s: %v", knownHostPath, err)
continue
}
if err := util.RemoveLineFromFile(string(key), knownHosts); err != nil {
klog.Warningf("failed to remove key: %v", err)
}
// and, remove the file which stores this key
if err := os.Remove(knownHostPath); err != nil {
klog.Warningf("failed to remove key: %v", err)
}
}
continue
}
nodeName := file.Name()

knownHostPath := filepath.Join(localpath.MiniPath(), "machines", nodeName, "known_host")
_, err := os.Stat(knownHostPath)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
klog.Warningf("error reading %s: %v", knownHostPath, err)
continue
}
// if this file exists, remove this line from known_hosts
key, err := os.ReadFile(knownHostPath)
if err != nil {
klog.Warningf("error reading key from %s: %v", knownHostPath, err)
continue
}
if err := util.RemoveLineFromFile(string(key), knownHosts); err != nil {
klog.Warningf("failed to remove key: %v", err)
}
// and, remove the file which stores this key
if err := os.Remove(knownHostPath); err != nil {
klog.Warningf("failed to remove %s: %v", knownHostPath, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/ssh-host.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func appendKnownHelper(nodeName string, appendKnown bool) error {
// these keys can be removed properly
_, cc := mustload.Partial(ClusterFlagValue())
knownHostPath := filepath.Join(localpath.MiniPath(), "machines", config.MachineName(*cc, *n), "known_host")
if err := os.WriteFile(knownHostPath, []byte(keys), 0666); err != nil {
if err := os.WriteFile(knownHostPath, []byte(keys), 0644); err != nil {
return fmt.Errorf("WriteFile to %s: %v", knownHostPath, err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,19 @@ func TestRemoveLineFromFile(t *testing.T) {
filePath := "./test_known_hosts"
// first we write the content into the test file
if err := os.WriteFile(filePath, []byte(testFileContent), 0666); err != nil {
t.Errorf("Failed to write to test file, err=%v", err)
t.Fatalf("Failed to write to test file, err=%v", err)
}
defer os.Remove(filePath)

line := "35.244.161.222 remove.test"
if err := RemoveLineFromFile(line, filePath); err != nil {
t.Errorf("Failed to run RemoveLineFromFile, err=%v", err)
t.Fatalf("Failed to run RemoveLineFromFile, err=%v", err)
}
// now read the file again to see whether this line still exists

newContent, err := os.ReadFile(filePath)
if err != nil {
t.Errorf("Failed to read file, err=%v", err)
t.Fatalf("Failed to read file, err=%v", err)
}
if strings.Contains(string(newContent), line) {
t.Errorf("Failed to remove the line")
Expand Down

0 comments on commit 60e2932

Please sign in to comment.