Skip to content

Commit

Permalink
Have cluster.Mount return an error which is checked in advice.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
andriyDev committed Jul 22, 2021
1 parent 5ca1164 commit 36cc7e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
8 changes: 7 additions & 1 deletion cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...")
Expand Down
33 changes: 25 additions & 8 deletions pkg/minikube/cluster/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 36cc7e0

Please sign in to comment.