try reload dockerd first before restart
parent
61abd1e64c
commit
5a443f09fb
|
@ -27,6 +27,7 @@ import (
|
|||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog/v2"
|
||||
|
@ -151,17 +152,33 @@ func (EnvNoProxyGetter) GetNoProxyVar() (string, string) {
|
|||
return noProxyVar, noProxyValue
|
||||
}
|
||||
|
||||
// ensureDockerd ensures dockerd inside minikube is running before a docker-env command
|
||||
func ensureDockerd(name string, runner command.Runner) {
|
||||
if ok := isDockerActive(runner); ok {
|
||||
return
|
||||
}
|
||||
|
||||
// Docker Docs: https://docs.docker.com/config/containers/live-restore
|
||||
// On Linux, you can avoid a restart (and avoid any downtime for your containers) by reloading the Docker daemon.
|
||||
klog.Warningf("dockerd is not active will try to reload it...")
|
||||
if err := sysinit.New(runner).Reload("docker"); err != nil {
|
||||
klog.Warningf("will try to restar docker because reload failed: %v", err)
|
||||
if err := sysinit.New(runner).Restart("docker"); err != nil {
|
||||
exit.Message(reason.RuntimeRestart, `The Docker service within '{{.name}}' is not active`, out.V{"name": name})
|
||||
}
|
||||
// if we get to the point that we have to restart docker (instead of reload)
|
||||
// will need to wait for apisever container to come up, this usually takes 5 seconds
|
||||
// verifying apisever using kverify would add code complexity for a rare case.
|
||||
klog.Warningf("waiting 5 seconds to ensure apisever container is up...")
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
}
|
||||
|
||||
// isDockerActive checks if Docker is active
|
||||
func isDockerActive(r command.Runner) bool {
|
||||
return sysinit.New(r).Active("docker")
|
||||
}
|
||||
|
||||
func mustRestartDocker(name string, runner command.Runner) {
|
||||
if err := sysinit.New(runner).Restart("docker"); err != nil {
|
||||
exit.Message(reason.RuntimeRestart, `The Docker service within '{{.name}}' is not active`, out.V{"name": name})
|
||||
}
|
||||
}
|
||||
|
||||
// dockerEnvCmd represents the docker-env command
|
||||
var dockerEnvCmd = &cobra.Command{
|
||||
Use: "docker-env",
|
||||
|
@ -205,10 +222,7 @@ var dockerEnvCmd = &cobra.Command{
|
|||
out.V{"runtime": co.Config.KubernetesConfig.ContainerRuntime})
|
||||
}
|
||||
|
||||
if ok := isDockerActive(co.CP.Runner); !ok {
|
||||
klog.Warningf("dockerd is not active will try to restart it...")
|
||||
mustRestartDocker(cname, co.CP.Runner)
|
||||
}
|
||||
ensureDockerd(cname, co.CP.Runner)
|
||||
|
||||
port := constants.DockerDaemonPort
|
||||
if driver.NeedsPortForward(driverName) {
|
||||
|
@ -239,7 +253,7 @@ var dockerEnvCmd = &cobra.Command{
|
|||
if err != nil { // docker might be up but been loaded with wrong certs/config
|
||||
// to fix issues like this #8185
|
||||
klog.Warningf("couldn't connect to docker inside minikube. will try to restart dockerd service... output: %s error: %v", string(out), err)
|
||||
mustRestartDocker(cname, co.CP.Runner)
|
||||
esnureDockerd(cname, co.CP.Runner)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ package sysinit
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os/exec"
|
||||
"path"
|
||||
|
@ -128,6 +129,12 @@ func (s *OpenRC) Restart(svc string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Reload reloads a service
|
||||
// currently only used by our docker-env that doesn't need openrc implementation
|
||||
func (s *OpenRC) Reload(svc string) error {
|
||||
return fmt.Errorf("reload is not implemented for OpenRC yet ! Please implement if needed")
|
||||
}
|
||||
|
||||
// Stop stops a service
|
||||
func (s *OpenRC) Stop(svc string) error {
|
||||
rr, err := s.r.RunCmd(exec.Command("sudo", "service", svc, "stop"))
|
||||
|
|
|
@ -50,6 +50,9 @@ type Manager interface {
|
|||
// Restart restarts a service
|
||||
Restart(string) error
|
||||
|
||||
// Reload restarts a service
|
||||
Reload(string) error
|
||||
|
||||
// Stop stops a service
|
||||
Stop(string) error
|
||||
|
||||
|
|
|
@ -34,8 +34,8 @@ func (s *Systemd) Name() string {
|
|||
return "systemd"
|
||||
}
|
||||
|
||||
// reload reloads systemd configuration
|
||||
func (s *Systemd) reload() error {
|
||||
// daemonReload reloads systemd configuration
|
||||
func (s *Systemd) daemonReload() error {
|
||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "daemon-reload"))
|
||||
return err
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func (s *Systemd) Enable(svc string) error {
|
|||
|
||||
// Start starts a service
|
||||
func (s *Systemd) Start(svc string) error {
|
||||
if err := s.reload(); err != nil {
|
||||
if err := s.daemonReload(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "start", svc))
|
||||
|
@ -72,13 +72,22 @@ func (s *Systemd) Start(svc string) error {
|
|||
|
||||
// Restart restarts a service
|
||||
func (s *Systemd) Restart(svc string) error {
|
||||
if err := s.reload(); err != nil {
|
||||
if err := s.daemonReload(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "restart", svc))
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload reloads a service
|
||||
func (s *Systemd) Reload(svc string) error {
|
||||
if err := s.daemonReload(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "reload", svc))
|
||||
return err
|
||||
}
|
||||
|
||||
// Stop stops a service
|
||||
func (s *Systemd) Stop(svc string) error {
|
||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "stop", svc))
|
||||
|
|
Loading…
Reference in New Issue