Skip to content

Commit

Permalink
Improve podman status when checking for sudo
Browse files Browse the repository at this point in the history
Only use sudo for info and running on linux
  • Loading branch information
afbjorklund committed Apr 19, 2020
1 parent 19be561 commit e99340b
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions pkg/minikube/registry/drvs/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package podman
import (
"context"
"fmt"
"os"
"os/exec"
"os/user"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -63,16 +66,17 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
}

func status() registry.State {
_, err := exec.LookPath(oci.Podman)
docURL := "https://minikube.sigs.k8s.io/docs/drivers/podman/"
podman, err := exec.LookPath(oci.Podman)
if err != nil {
return registry.State{Error: err, Installed: false, Healthy: false, Fix: "Podman is required.", Doc: "https://minikube.sigs.k8s.io/docs/reference/drivers/podman/"}
return registry.State{Error: err, Installed: false, Healthy: false, Fix: "Install Podman", Doc: docURL}
}

// Allow no more than 2 seconds for version command
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "sudo", oci.Podman, "version", "-f", "{{.Version}}")
cmd := exec.CommandContext(ctx, oci.Podman, "version", "-f", "{{.Version}}")
o, err := cmd.CombinedOutput()
output := string(o)
if err != nil {
Expand All @@ -87,13 +91,41 @@ func status() registry.State {
if v.LT(minReqPodmanVer) {
glog.Warningf("Warning ! mininim required version for podman is %s. your version is %q. minikube might not work. use at your own risk. To install latest version please see https://podman.io/getting-started/installation.html ", minReqPodmanVer.String(), v.String())
}

// Allow no more than 3 seconds for querying state
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
err = exec.CommandContext(ctx, "sudo", oci.Podman, "info").Run()
if err != nil {
return registry.State{Error: err, Installed: true, Healthy: false, Fix: "Podman is not running or taking too long to respond. Try: restarting podman."}

// Run with sudo on linux (local), otherwise podman-remote (as podman)
if runtime.GOOS == "linux" {
cmd = exec.CommandContext(ctx, "sudo", "-n", oci.Podman, "info")
cmd.Env = append(os.Environ(), "LANG=C", "LC_ALL=C") // sudo is localized
} else {
cmd = exec.CommandContext(ctx, oci.Podman, "info")
}
_, err = cmd.Output()
if err == nil {
return registry.State{Installed: true, Healthy: true}
}

glog.Warningf("podman returned error: %v", err)

if exitErr, ok := err.(*exec.ExitError); ok {
stderr := strings.TrimSpace(string(exitErr.Stderr))
newErr := fmt.Errorf(`%q %v: %s`, strings.Join(cmd.Args, " "), exitErr, stderr)

username := "$USER"
if u, err := user.Current(); err == nil {
username = u.Username
}

if strings.Contains(stderr, "a password is required") && runtime.GOOS == "linux" {
return registry.State{Error: newErr, Installed: true, Healthy: false, Fix: fmt.Sprintf("Add your user to the 'sudoers' file: '%s ALL=(ALL) NOPASSWD: %s'", username, podman), Doc: "https://podman.io"}
}

// We don't have good advice, but at least we can provide a good error message
return registry.State{Error: newErr, Installed: true, Healthy: false, Doc: docURL}
}

return registry.State{Installed: true, Healthy: true}
return registry.State{Error: err, Installed: true, Healthy: false, Doc: docURL}
}

0 comments on commit e99340b

Please sign in to comment.