Skip to content

Commit

Permalink
fix: remove duplicate line in known_hosts when minikube deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeProgrammer committed Aug 14, 2023
1 parent 13d7ebc commit 9bbab0f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/docker/machine/libmachine"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/client-go/util/homedir"
"k8s.io/klog/v2"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/drivers/kic"
Expand All @@ -53,6 +54,7 @@ import (
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/sshagent"
"k8s.io/minikube/pkg/minikube/style"
"k8s.io/minikube/pkg/util"
)

var (
Expand Down Expand Up @@ -233,6 +235,14 @@ func runDelete(_ *cobra.Command, args []string) {
defer cancel()

if deleteAll {
// use a background goroutine to run this delete
// so that this will not block for a long time
deleteKnownHostsChannel := make(chan struct{})
go func() {
deleteKnownHosts()
deleteKnownHostsChannel <- struct{}{}
}()

deleteContainersAndVolumes(delCtx, oci.Docker)
deleteContainersAndVolumes(delCtx, oci.Podman)

Expand All @@ -242,8 +252,11 @@ func runDelete(_ *cobra.Command, args []string) {
if len(errs) > 0 {
HandleDeletionErrors(errs)
} else {
// if deleteKnownHosts hasn't finished, then wait
<-deleteKnownHostsChannel
out.Step(style.DeletingHost, "Successfully deleted all profiles")
}

} else {
if len(args) > 0 {
exit.Message(reason.Usage, "usage: minikube delete")
Expand Down Expand Up @@ -723,3 +736,37 @@ func getPids(path string) ([]int, error) {

return pids, nil
}

func deleteKnownHosts() {
// remove this line from known_hosts file if it exists
knownHosts := filepath.Join(homedir.HomeDir(), ".ssh", "known_hosts")
machinesDir := filepath.Join(localpath.MiniPath(), "machines")
fileInfo, err := os.ReadDir(machinesDir)
if err != nil {
klog.Warningf("error reading machines in minikube home: %v", err)
return
}
for _, file := range fileInfo {
if file.IsDir() {
nodeName := file.Name()

knowHostPath := filepath.Join(localpath.MiniPath(), "machines", nodeName, "known_host")
if _, err := os.Stat(knowHostPath); err == nil {
// if this file exists, remove this line from known_hosts
key, err := os.ReadFile(knowHostPath)
if err != nil {
klog.Warningf("error reading keys from %s: %v", knowHostPath, 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(knowHostPath); err != nil {
klog.Warningf("failed to remove key: %v", err)
}
}
}
}

}
10 changes: 10 additions & 0 deletions cmd/minikube/cmd/ssh-host.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/node"
Expand Down Expand Up @@ -122,6 +123,15 @@ func appendKnownHelper(nodeName string, appendKnown bool) {

fmt.Fprintf(os.Stderr, "Host added: %s (%s)\n", knownHosts, host)

// then store it into minikube home folder
// so that when we execute `minikube delete`
// 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 {
out.ErrLn("WriteString to %s: %v", knownHostPath, err)
os.Exit(1)
}
return
}
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package util

import (
"bufio"
"fmt"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"

"github.com/blang/semver/v4"
units "github.com/docker/go-units"
Expand Down Expand Up @@ -123,3 +125,35 @@ func RemoveDuplicateStrings(initial []string) []string {
}
return result
}

// remove the specified line from the given file
func RemoveLineFromFile(knownHostLine string, filePath string) error {
fd, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return fmt.Errorf("failed to open %s: %v", filePath, err)
}
defer fd.Close()

// read each line from known_hosts and find theline we want to delete
scanner := bufio.NewScanner(fd)
newLines := make([]string, 0)
for scanner.Scan() {
line := string(scanner.Bytes())
if strings.TrimSpace(line) != strings.TrimSpace(knownHostLine) {
// remove the line which is identical with the key
newLines = append(newLines, line)
}
}
// remove the contents and move to the head of this file
fd.Truncate(0)

Check failure on line 148 in pkg/util/utils.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `fd.Truncate` is not checked (errcheck)
fd.Seek(0, 0)

Check failure on line 149 in pkg/util/utils.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `fd.Seek` is not checked (errcheck)

// write the new content into the file

for _, line := range newLines {
if _, err := fmt.Fprintln(fd, line); err != nil {
return fmt.Errorf("failed to write to %s: %v", filePath, err)
}
}
return nil
}

0 comments on commit 9bbab0f

Please sign in to comment.