Have cluster.Mount return an error which is checked in advice.go.

pull/11979/head
Andriy Dzikh 2021-07-15 16:21:38 -07:00
parent 5ca1164fb9
commit 36cc7e0a70
2 changed files with 32 additions and 9 deletions

View File

@ -190,7 +190,13 @@ var mountCmd = &cobra.Command{
}
}()
cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg)
err = cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg)
if err != nil {
if rtErr, ok := err.(*cluster.MountError); ok && rtErr.ErrorType == cluster.MountErrorConnect {
exit.Error(reason.GuestMountCouldNotConnect, "mount could not connect", rtErr)
}
exit.Error(reason.GuestMount, "mount failed", err)
}
out.Step(style.Success, "Successfully mounted {{.sourcePath}} to {{.destinationPath}}", out.V{"sourcePath": hostPath, "destinationPath": vmPath})
out.Ln("")
out.Styled(style.Notice, "NOTE: This process must stay alive for the mount to be accessible ...")

View File

@ -27,8 +27,6 @@ import (
"github.com/pkg/errors"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/reason"
)
// MountConfig defines the options available to the Mount command
@ -56,26 +54,45 @@ type mountRunner interface {
RunCmd(*exec.Cmd) (*command.RunResult, error)
}
const (
// MountErrorUnknown failed with unknown error
MountErrorUnknown = iota
// MountErrorConnect
MountErrorConnect
)
// MountError wrapper around errors in the `Mount` function
type MountError struct {
// ErrorType enum for more info about the error
ErrorType int
// UnderlyingError the error being wrapped
UnderlyingError error
}
func (m *MountError) Error() string {
return m.UnderlyingError.Error()
}
// Mount runs the mount command from the 9p client on the VM to the 9p server on the host
func Mount(r mountRunner, source string, target string, c *MountConfig) {
func Mount(r mountRunner, source string, target string, c *MountConfig) error {
if err := Unmount(r, target); err != nil {
exit.Error(reason.GuestMount, "mount failed", errors.Wrap(err, "umount"))
return &MountError{ErrorType: MountErrorUnknown, UnderlyingError: errors.Wrap(err, "umount")}
}
if _, err := r.RunCmd(exec.Command("/bin/bash", "-c", fmt.Sprintf("sudo mkdir -m %o -p %s", c.Mode, target))); err != nil {
exit.Error(reason.GuestMount, "mount failed", errors.Wrap(err, "create folder pre-mount"))
return &MountError{ErrorType: MountErrorUnknown, UnderlyingError: errors.Wrap(err, "create folder pre-mount")}
}
rr, err := r.RunCmd(exec.Command("/bin/bash", "-c", mntCmd(source, target, c)))
if err != nil {
if strings.Contains(rr.Stderr.String(), "Connection timed out") {
exit.Error(reason.GuestMountCouldNotConnect, "mount could not connect", err)
} else {
exit.Error(reason.GuestMount, "mount failed", errors.Wrapf(err, "mount with cmd %s ", rr.Command()))
return &MountError{ErrorType: MountErrorConnect, UnderlyingError: err}
}
return &MountError{ErrorType: MountErrorUnknown, UnderlyingError: errors.Wrapf(err, "mount with cmd %s ", rr.Command())}
}
klog.Infof("mount successful: %q", rr.Output())
return nil
}
// returns either a raw UID number, or the subshell to resolve it.