From be65e4c2ad9168e69761220029dde7681feab3a3 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Tue, 21 Jan 2020 19:46:42 -0800 Subject: [PATCH] Merge upstream --- cmd/minikube/cmd/pause.go | 23 +++++++++++++++----- cmd/minikube/cmd/unpause.go | 35 ++++++++++++++++++++++--------- pkg/minikube/cruntime/cruntime.go | 13 +++++++++++- pkg/minikube/cruntime/docker.go | 29 +++++++++++++++++++++++-- pkg/minikube/kubelet/kubelet.go | 4 ++-- 5 files changed, 84 insertions(+), 20 deletions(-) diff --git a/cmd/minikube/cmd/pause.go b/cmd/minikube/cmd/pause.go index 731c5f87d0..797cffa25d 100644 --- a/cmd/minikube/cmd/pause.go +++ b/cmd/minikube/cmd/pause.go @@ -17,7 +17,11 @@ limitations under the License. package cmd import ( + "os" + + "github.com/golang/glog" "github.com/spf13/cobra" + "github.com/spf13/viper" "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" @@ -33,16 +37,25 @@ var pauseCmd = &cobra.Command{ Use: "pause", Short: "pause containers", Run: func(cmd *cobra.Command, args []string) { + cname := viper.GetString(config.MachineProfile) api, err := machine.NewAPIClient() if err != nil { exit.WithError("Error getting client", err) } defer api.Close() - cc, err := config.Load() - if err != nil { - exit.WithError("Error getting config", err) + cc, err := config.Load(cname) + + if err != nil && !os.IsNotExist(err) { + exit.WithError("Error loading profile config", err) } - host, err := cluster.CheckIfHostExistsAndLoad(api, cc.Name) + + if err != nil { + out.ErrT(out.Meh, `"{{.name}}" profile does not exist`, out.V{"name": cname}) + os.Exit(1) + } + + glog.Infof("config: %+v", cc) + host, err := cluster.CheckIfHostExistsAndLoad(api, cname) if err != nil { exit.WithError("Error getting host", err) } @@ -52,7 +65,7 @@ var pauseCmd = &cobra.Command{ exit.WithError("Failed to get command runner", err) } - config := cruntime.Config{Type: cc.ContainerRuntime} + config := cruntime.Config{Type: cc.ContainerRuntime, Runner: r} cr, err := cruntime.New(config) if err != nil { exit.WithError("Failed runtime", err) diff --git a/cmd/minikube/cmd/unpause.go b/cmd/minikube/cmd/unpause.go index 3e45eaf711..e0df53060e 100644 --- a/cmd/minikube/cmd/unpause.go +++ b/cmd/minikube/cmd/unpause.go @@ -17,8 +17,13 @@ limitations under the License. package cmd import ( - "github.com/spf13/cobra" + "os" + "github.com/golang/glog" + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/cruntime" "k8s.io/minikube/pkg/minikube/exit" @@ -32,22 +37,27 @@ var unpauseCmd = &cobra.Command{ Use: "unpause", Short: "unpause Kubernetes", Run: func(cmd *cobra.Command, args []string) { + cname := viper.GetString(config.MachineProfile) api, err := machine.NewAPIClient() if err != nil { exit.WithError("Error getting client", err) } defer api.Close() + cc, err := config.Load(cname) - cc, err := config.Load() - if err != nil { - exit.WithError("Error getting config", err) + if err != nil && !os.IsNotExist(err) { + exit.WithError("Error loading profile config", err) } - host, err := api.Load(cc.Name) - config := cruntime.Config{Type: cc.ContainerRuntime} - cr, err := cruntime.New(config) if err != nil { - exit.WithError("Failed runtime", err) + out.ErrT(out.Meh, `"{{.name}}" profile does not exist`, out.V{"name": cname}) + os.Exit(1) + } + + glog.Infof("config: %+v", cc) + host, err := cluster.CheckIfHostExistsAndLoad(api, cname) + if err != nil { + exit.WithError("Error getting host", err) } r, err := machine.CommandRunner(host) @@ -55,10 +65,15 @@ var unpauseCmd = &cobra.Command{ exit.WithError("Failed to get command runner", err) } - err = pause.Pause(cr, r) + config := cruntime.Config{Type: cc.ContainerRuntime, Runner: r} + cr, err := cruntime.New(config) + if err != nil { + exit.WithError("Failed runtime", err) + } + err = pause.Unpause(cr, r) if err != nil { exit.WithError("Pause", err) } - out.T(out.Pause, "The '{{.name}}' cluster is now paused", out.V{"name": cc.Name}) + out.T(out.Unpause, "The '{{.name}}' cluster is now unpaused", out.V{"name": cc.Name}) }, } diff --git a/pkg/minikube/cruntime/cruntime.go b/pkg/minikube/cruntime/cruntime.go index 9ca3e078bf..df81613678 100644 --- a/pkg/minikube/cruntime/cruntime.go +++ b/pkg/minikube/cruntime/cruntime.go @@ -36,7 +36,7 @@ const ( ) func (cs ContainerState) String() string { - return [...]string{"All", "Running", "Paused"}[cs] + return [...]string{"all", "running", "paused"}[cs] } // CommandRunner is the subset of command.Runner this package consumes @@ -61,6 +61,8 @@ type Manager interface { // Style is an associated StyleEnum for Name() Style() out.StyleEnum + // CGroupDriver returns cgroup driver ("cgroupfs" or "systemd") + CGroupDriver() (string, error) // KubeletOptions returns kubelet options for a runtime. KubeletOptions() map[string]string // SocketPath returns the path to the socket file for a given runtime @@ -71,6 +73,9 @@ type Manager interface { // Load an image idempotently into the runtime on a host LoadImage(string) error + // ImageExists takes image name and image sha checks if an it exists + ImageExists(string, string) bool + // ListContainers returns a list of managed by this container runtime ListContainers(ListOptions) ([]string, error) // KillContainers removes containers based on ID @@ -122,6 +127,12 @@ 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 `which crictl || echo crictl` ps -a || sudo docker ps -a" +} + // disableOthers disables all other runtimes except for me. func disableOthers(me Manager, cr CommandRunner) error { // valid values returned by manager.Name() diff --git a/pkg/minikube/cruntime/docker.go b/pkg/minikube/cruntime/docker.go index 1bc2ab6920..0b5297f381 100644 --- a/pkg/minikube/cruntime/docker.go +++ b/pkg/minikube/cruntime/docker.go @@ -102,6 +102,20 @@ func (r *Docker) Disable() error { return nil } +// ImageExists checks if an image exists +func (r *Docker) ImageExists(name string, sha string) bool { + // expected output looks like [SHA_ALGO:SHA] + c := exec.Command("docker", "inspect", "--format='{{.Id}}'", name) + rr, err := r.Runner.RunCmd(c) + if err != nil { + return false + } + if !strings.Contains(rr.Output(), sha) { + return false + } + return true +} + // LoadImage loads an image into this runtime func (r *Docker) LoadImage(path string) error { glog.Infof("Loading image: %s", path) @@ -113,6 +127,17 @@ func (r *Docker) LoadImage(path string) error { } +// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd") +func (r *Docker) CGroupDriver() (string, error) { + // Note: the server daemon has to be running, for this call to return successfully + c := exec.Command("docker", "info", "--format", "'{{.CgroupDriver}}'") + rr, err := r.Runner.RunCmd(c) + if err != nil { + return "", err + } + return strings.Split(rr.Stdout.String(), "\n")[0], nil +} + // KubeletOptions returns kubelet options for a runtime. func (r *Docker) KubeletOptions() map[string]string { return map[string]string{ @@ -127,9 +152,9 @@ func (r *Docker) ListContainers(o ListOptions) ([]string, error) { case All: args = append(args, "-a") case Paused: - args = append(args, "--filter status=paused") + args = append(args, "--filter", "status=paused") } - args = append(args, fmt.Sprintf("--filter=name=%s", KubernetesContainerPrefix+o.Name), "--format=\"{{.ID}}\"") + args = append(args, fmt.Sprintf("--filter=name=%s", KubernetesContainerPrefix+o.Name), "--format={{.ID}}") rr, err := r.Runner.RunCmd(exec.Command("docker", args...)) if err != nil { return nil, errors.Wrapf(err, "docker") diff --git a/pkg/minikube/kubelet/kubelet.go b/pkg/minikube/kubelet/kubelet.go index c8e0436a2f..315f492878 100644 --- a/pkg/minikube/kubelet/kubelet.go +++ b/pkg/minikube/kubelet/kubelet.go @@ -87,7 +87,7 @@ func Check(cr command.Runner) error { // Disable disables the Kubelet func Disable(cr command.Runner) error { glog.Infof("disabling kubelet ...") - c := exec.Command("systemctl", "disable", "kubelet") + c := exec.Command("sudo", "systemctl", "disable", "kubelet") if _, err := cr.RunCmd(c); err != nil { return errors.Wrap(err, "disable") } @@ -97,7 +97,7 @@ func Disable(cr command.Runner) error { // Enable enables the Kubelet func Enable(cr command.Runner) error { glog.Infof("enabling kubelet ...") - c := exec.Command("systemctl", "enable", "kubelet") + c := exec.Command("sudo", "systemctl", "enable", "kubelet") if _, err := cr.RunCmd(c); err != nil { return errors.Wrap(err, "enable") }