diff --git a/pkg/minikube/cruntime/containerd.go b/pkg/minikube/cruntime/containerd.go index d431ee2636..3e91a45edc 100644 --- a/pkg/minikube/cruntime/containerd.go +++ b/pkg/minikube/cruntime/containerd.go @@ -267,7 +267,7 @@ func (r *Containerd) StopContainers(ids []string) error { // ContainerLogCmd returns the command to retrieve the log for a container based on ID func (r *Containerd) ContainerLogCmd(id string, len int, follow bool) string { - return criContainerLogCmd(id, len, follow) + return criContainerLogCmd(r.Runner, id, len, follow) } // SystemLogCmd returns the command to retrieve system logs diff --git a/pkg/minikube/cruntime/cri.go b/pkg/minikube/cruntime/cri.go index f053c74404..75613527fe 100644 --- a/pkg/minikube/cruntime/cri.go +++ b/pkg/minikube/cruntime/cri.go @@ -331,16 +331,26 @@ plugin_dirs = [ ` ) +// getCommandPath returns the absolute path of a command +func getCommandPath(cr CommandRunner, cmd string) string { + rr, err := cr.RunCmd(exec.Command("which", cmd)) + if err != nil { + return cmd + } + return strings.Split(rr.Stdout.String(), "\n")[0] +} + // listCRIContainers returns a list of containers using crictl func listCRIContainers(cr CommandRunner, filter string) ([]string, error) { var err error var rr *command.RunResult state := "Running" + crictl := getCommandPath(cr, "crictl") if filter != "" { - c := exec.Command("sudo", "crictl", "ps", "-a", fmt.Sprintf("--name=%s", filter), fmt.Sprintf("--state=%s", state), "--quiet") + c := exec.Command("sudo", crictl, "ps", "-a", fmt.Sprintf("--name=%s", filter), fmt.Sprintf("--state=%s", state), "--quiet") rr, err = cr.RunCmd(c) } else { - rr, err = cr.RunCmd(exec.Command("sudo", "crictl", "ps", "-a", fmt.Sprintf("--state=%s", state), "--quiet")) + rr, err = cr.RunCmd(exec.Command("sudo", crictl, "ps", "-a", fmt.Sprintf("--state=%s", state), "--quiet")) } if err != nil { return nil, err @@ -361,7 +371,8 @@ func killCRIContainers(cr CommandRunner, ids []string) error { } glog.Infof("Killing containers: %s", ids) - args := append([]string{"crictl", "rm"}, ids...) + crictl := getCommandPath(cr, "crictl") + args := append([]string{crictl, "rm"}, ids...) c := exec.Command("sudo", args...) if _, err := cr.RunCmd(c); err != nil { return errors.Wrap(err, "kill cri containers.") @@ -375,7 +386,9 @@ func stopCRIContainers(cr CommandRunner, ids []string) error { return nil } glog.Infof("Stopping containers: %s", ids) - args := append([]string{"crictl", "rm"}, ids...) + + crictl := getCommandPath(cr, "crictl") + args := append([]string{crictl, "rm"}, ids...) c := exec.Command("sudo", args...) if _, err := cr.RunCmd(c); err != nil { return errors.Wrap(err, "stop cri containers") @@ -428,9 +441,12 @@ func generateCRIOConfig(cr CommandRunner, imageRepository string) error { } // criContainerLogCmd returns the command to retrieve the log for a container based on ID -func criContainerLogCmd(id string, len int, follow bool) string { +func criContainerLogCmd(cr CommandRunner, id string, len int, follow bool) string { + crictl := getCommandPath(cr, "crictl") var cmd strings.Builder - cmd.WriteString("sudo crictl logs ") + cmd.WriteString("sudo ") + cmd.WriteString(crictl) + cmd.WriteString(" logs ") if len > 0 { cmd.WriteString(fmt.Sprintf("--tail %d ", len)) } diff --git a/pkg/minikube/cruntime/crio.go b/pkg/minikube/cruntime/crio.go index 1b297d2e2b..2449733e12 100644 --- a/pkg/minikube/cruntime/crio.go +++ b/pkg/minikube/cruntime/crio.go @@ -170,7 +170,7 @@ func (r *CRIO) StopContainers(ids []string) error { // ContainerLogCmd returns the command to retrieve the log for a container based on ID func (r *CRIO) ContainerLogCmd(id string, len int, follow bool) string { - return criContainerLogCmd(id, len, follow) + return criContainerLogCmd(r.Runner, id, len, follow) } // SystemLogCmd returns the command to retrieve system logs diff --git a/pkg/minikube/cruntime/cruntime.go b/pkg/minikube/cruntime/cruntime.go index cfc65c008c..4c0a011866 100644 --- a/pkg/minikube/cruntime/cruntime.go +++ b/pkg/minikube/cruntime/cruntime.go @@ -105,7 +105,7 @@ func New(c Config) (Manager, error) { // ContainerStatusCommand works across container runtimes with good formatting func ContainerStatusCommand() string { // Fallback to 'docker ps' if it fails (none driver) - return "sudo crictl ps -a || sudo docker ps -a" + return "sudo `which crictl || echo crictl` ps -a || sudo docker ps -a" } // disableOthers disables all other runtimes except for me. diff --git a/pkg/minikube/cruntime/cruntime_test.go b/pkg/minikube/cruntime/cruntime_test.go index 13ed8bdde6..971ed22d2c 100644 --- a/pkg/minikube/cruntime/cruntime_test.go +++ b/pkg/minikube/cruntime/cruntime_test.go @@ -143,9 +143,11 @@ func (f *FakeRunner) RunCmd(cmd *exec.Cmd) (*command.RunResult, error) { switch bin { case "systemctl": return buffer(f.systemctl(args, root)) + case "which": + return buffer(f.which(args, root)) case "docker": return buffer(f.docker(args, root)) - case "crictl": + case "crictl", "/usr/bin/crictl": return buffer(f.crictl(args, root)) case "crio": return buffer(f.crio(args, root)) @@ -321,6 +323,13 @@ func (f *FakeRunner) systemctl(args []string, root bool) (string, error) { // no return out, nil } +// which is a fake implementation of which +func (f *FakeRunner) which(args []string, root bool) (string, error) { // nolint result 0 (string) is always "" + command := args[0] + path := fmt.Sprintf("/usr/bin/%s", command) + return path, nil +} + func TestVersion(t *testing.T) { var tests = []struct { runtime string