try reload dockerd first before restart
parent
61abd1e64c
commit
5a443f09fb
|
@ -27,6 +27,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
@ -151,17 +152,33 @@ func (EnvNoProxyGetter) GetNoProxyVar() (string, string) {
|
||||||
return noProxyVar, noProxyValue
|
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
|
// isDockerActive checks if Docker is active
|
||||||
func isDockerActive(r command.Runner) bool {
|
func isDockerActive(r command.Runner) bool {
|
||||||
return sysinit.New(r).Active("docker")
|
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
|
// dockerEnvCmd represents the docker-env command
|
||||||
var dockerEnvCmd = &cobra.Command{
|
var dockerEnvCmd = &cobra.Command{
|
||||||
Use: "docker-env",
|
Use: "docker-env",
|
||||||
|
@ -205,10 +222,7 @@ var dockerEnvCmd = &cobra.Command{
|
||||||
out.V{"runtime": co.Config.KubernetesConfig.ContainerRuntime})
|
out.V{"runtime": co.Config.KubernetesConfig.ContainerRuntime})
|
||||||
}
|
}
|
||||||
|
|
||||||
if ok := isDockerActive(co.CP.Runner); !ok {
|
ensureDockerd(cname, co.CP.Runner)
|
||||||
klog.Warningf("dockerd is not active will try to restart it...")
|
|
||||||
mustRestartDocker(cname, co.CP.Runner)
|
|
||||||
}
|
|
||||||
|
|
||||||
port := constants.DockerDaemonPort
|
port := constants.DockerDaemonPort
|
||||||
if driver.NeedsPortForward(driverName) {
|
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
|
if err != nil { // docker might be up but been loaded with wrong certs/config
|
||||||
// to fix issues like this #8185
|
// 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)
|
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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
@ -128,6 +129,12 @@ func (s *OpenRC) Restart(svc string) error {
|
||||||
return err
|
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
|
// Stop stops a service
|
||||||
func (s *OpenRC) Stop(svc string) error {
|
func (s *OpenRC) Stop(svc string) error {
|
||||||
rr, err := s.r.RunCmd(exec.Command("sudo", "service", svc, "stop"))
|
rr, err := s.r.RunCmd(exec.Command("sudo", "service", svc, "stop"))
|
||||||
|
|
|
@ -50,6 +50,9 @@ type Manager interface {
|
||||||
// Restart restarts a service
|
// Restart restarts a service
|
||||||
Restart(string) error
|
Restart(string) error
|
||||||
|
|
||||||
|
// Reload restarts a service
|
||||||
|
Reload(string) error
|
||||||
|
|
||||||
// Stop stops a service
|
// Stop stops a service
|
||||||
Stop(string) error
|
Stop(string) error
|
||||||
|
|
||||||
|
|
|
@ -34,8 +34,8 @@ func (s *Systemd) Name() string {
|
||||||
return "systemd"
|
return "systemd"
|
||||||
}
|
}
|
||||||
|
|
||||||
// reload reloads systemd configuration
|
// daemonReload reloads systemd configuration
|
||||||
func (s *Systemd) reload() error {
|
func (s *Systemd) daemonReload() error {
|
||||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "daemon-reload"))
|
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "daemon-reload"))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func (s *Systemd) Enable(svc string) error {
|
||||||
|
|
||||||
// Start starts a service
|
// Start starts a service
|
||||||
func (s *Systemd) Start(svc string) error {
|
func (s *Systemd) Start(svc string) error {
|
||||||
if err := s.reload(); err != nil {
|
if err := s.daemonReload(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "start", svc))
|
_, 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
|
// Restart restarts a service
|
||||||
func (s *Systemd) Restart(svc string) error {
|
func (s *Systemd) Restart(svc string) error {
|
||||||
if err := s.reload(); err != nil {
|
if err := s.daemonReload(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "restart", svc))
|
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "restart", svc))
|
||||||
return err
|
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
|
// Stop stops a service
|
||||||
func (s *Systemd) Stop(svc string) error {
|
func (s *Systemd) Stop(svc string) error {
|
||||||
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "stop", svc))
|
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "stop", svc))
|
||||||
|
|
Loading…
Reference in New Issue