Skip to content

Commit

Permalink
Adds mechanism to remove pid from pidfile if mount is sigtermd
Browse files Browse the repository at this point in the history
  • Loading branch information
x7upLime committed Apr 21, 2023
1 parent c3b6a9e commit 3ea179f
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 3 deletions.
72 changes: 69 additions & 3 deletions cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand All @@ -29,17 +30,20 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/detect"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/style"
pkgnetwork "k8s.io/minikube/pkg/network"
"k8s.io/minikube/pkg/util/lock"
"k8s.io/minikube/third_party/go9p/ufs"
)

Expand Down Expand Up @@ -202,7 +206,7 @@ var mountCmd = &cobra.Command{
out.Infof("Bind Address: {{.Address}}", out.V{"Address": net.JoinHostPort(bindIP, fmt.Sprint(port))})

var wg sync.WaitGroup
pid := make(chan int)
pidchan := make(chan int)
if cfg.Type == nineP {
wg.Add(1)
go func(pid chan int) {
Expand All @@ -211,8 +215,9 @@ var mountCmd = &cobra.Command{
ufs.StartServer(net.JoinHostPort(bindIP, strconv.Itoa(port)), debugVal, hostPath)
out.Step(style.Stopped, "Userspace file server is shutdown")
wg.Done()
}(pid)
}(pidchan)
}
pid := <-pidchan

// Unmount if Ctrl-C or kill request is received.
c := make(chan os.Signal, 1)
Expand All @@ -224,11 +229,17 @@ var mountCmd = &cobra.Command{
if err != nil {
out.FailureT("Failed unmount: {{.error}}", out.V{"error": err})
}

err = removePidFromFile(pid)
if err != nil {
out.FailureT("Failed removing pid from pidfile: {{.error}}", out.V{"error": err})
}

exit.Message(reason.Interrupted, "Received {{.name}} signal", out.V{"name": sig})
}
}()

err = cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg, <-pid)
err = cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg, pid)
if err != nil {
if rtErr, ok := err.(*cluster.MountError); ok && rtErr.ErrorType == cluster.MountErrorConnect {
exit.Error(reason.GuestMountCouldNotConnect, "mount could not connect", rtErr)
Expand Down Expand Up @@ -268,3 +279,58 @@ func getPort() (int, error) {
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

// removePidFromFile looks at the default locations for the mount-pids file,
// for the profile in use. If a file is found and its content shows PID, PID gets removed.
func removePidFromFile(pid int) error {
profile := viper.GetString("profile")
paths := []string{
localpath.MiniPath(), // legacy mount-process path for backwards compatibility
localpath.Profile(profile),
}

for _, path := range paths {
err := removePid(path, pid)
if err != nil {
return err
}
}

return nil
}

// removePid reads the file at PATH and tries to remove PID from it if found
func removePid(path string, pid int) error {
// is it the file we're looking for?
pidPath := filepath.Join(path, constants.MountProcessFileName)
if _, err := os.Stat(pidPath); os.IsNotExist(err) {
return nil
}

// we found the correct file
// we're reading the pids...
out, err := os.ReadFile(pidPath)
if err != nil {
return errors.Wrap(err, "ReadFile")
}

pids := []int{}
strPids := strings.Fields(string(out))
for _, p := range strPids {
intPid, err := strconv.Atoi(p)
if err != nil {
return errors.Wrap(err, "while converting pids")
}

// we skip the pid we're looking for
if intPid == pid {
continue
}

pids = append(pids, intPid)
}

// we convert the pids list back to string and write it back to file
newPids := fmt.Sprintf("%s ", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(pids)), " "), "[]"))
return lock.WriteFile(pidPath, []byte(newPids), 0o644)
}
1 change: 1 addition & 0 deletions translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "Port, der für das über den Proxy erreichbare Dashboard freigegeben wird. Wenn man 0 angibt, wird ein zufälliger Port ausgewählt.",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "Externer Adapter, auf dem der externe Switch erzeugt wird, wenn kein externer Switch gefunden wurde. (nur hyperv Treiber)",
"Fail check if container paused": "Schlägt fehl, wenn der Container pausiert ist",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "Runtime fehlgeschlagen",
"Failed to build image": "Bau des Images fehlgeschlagen",
"Failed to cache and load images": "Cachen und laden der Images fehlgeschlagen",
Expand Down
1 change: 1 addition & 0 deletions translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "",
"Fail check if container paused": "",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "",
"Failed to build image": "No se pudo construir la imagen",
"Failed to cache and load images": "",
Expand Down
1 change: 1 addition & 0 deletions translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "Port exposé du tableau de bord proxyfié. Réglez sur 0 pour choisir un port aléatoire.",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "L'adaptateur externe sur lequel un commutateur externe sera créé si aucun commutateur externe n'est trouvé. (pilote hyperv uniquement)",
"Fail check if container paused": "Échec de la vérification si le conteneur est en pause",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "Échec de l'exécution",
"Failed to build image": "Échec de la création de l'image",
"Failed to cache and load images": "Échec de la mise en cache et du chargement des images",
Expand Down
1 change: 1 addition & 0 deletions translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "プロキシー化されたダッシュボードの公開ポート。0 に設定すると、ランダムなポートが選ばれます。",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "外部スイッチが見つからない場合に、外部スイッチが作成される外部アダプター (hyperv ドライバーのみ)。",
"Fail check if container paused": "コンテナーが一時停止しているかどうかのチェックに失敗しました",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "ランタイムが失敗しました",
"Failed to build image": "イメージのビルドに失敗しました",
"Failed to cache and load images": "イメージのキャッシュとロードに失敗しました",
Expand Down
1 change: 1 addition & 0 deletions translations/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "",
"Fail check if container paused": "",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "런타임이 실패하였습니다",
"Failed to build image": "",
"Failed to cache ISO": "ISO 캐싱에 실패하였습니다",
Expand Down
1 change: 1 addition & 0 deletions translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "",
"Fail check if container paused": "",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "",
"Failed to build image": "",
"Failed to cache and load images": "",
Expand Down
1 change: 1 addition & 0 deletions translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "",
"Fail check if container paused": "",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "",
"Failed to build image": "",
"Failed to cache and load images": "",
Expand Down
1 change: 1 addition & 0 deletions translations/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "",
"Fail check if container paused": "",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "",
"Failed to build image": "",
"Failed to cache and load images": "",
Expand Down
1 change: 1 addition & 0 deletions translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
"Exposed port of the proxyfied dashboard. Set to 0 to pick a random port.": "",
"External Adapter on which external switch will be created if no external switch is found. (hyperv driver only)": "",
"Fail check if container paused": "",
"Failed removing pid from pidfile: {{.error}}": "",
"Failed runtime": "",
"Failed to build image": "",
"Failed to cache ISO": "缓存ISO 时失败",
Expand Down

0 comments on commit 3ea179f

Please sign in to comment.