Merge upstream

pull/5962/head
Thomas Stromberg 2020-01-21 19:46:42 -08:00
parent 7fe786aca4
commit be65e4c2ad
5 changed files with 84 additions and 20 deletions

View File

@ -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)

View File

@ -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})
},
}

View File

@ -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()

View File

@ -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")

View File

@ -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")
}