Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provisioner: only reload docker if necessary, don't install curl #7115

Merged
merged 2 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/minikube/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (api *LocalClient) Create(h *host.Host) error {
return fmt.Errorf("driver %q does not exist", h.DriverName)
}
if def.Init == nil {
// NOTE: This will call provision.DetectProvisioner
return api.legacyClient.Create(h)
}

Expand Down
47 changes: 13 additions & 34 deletions pkg/provision/buildroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ package provision
import (
"bytes"
"fmt"
"path"
"text/template"
"time"

"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/provision/serviceaction"
"github.com/docker/machine/libmachine/swarm"
"github.com/golang/glog"
"k8s.io/minikube/pkg/util/retry"
)

Expand All @@ -42,7 +40,7 @@ type BuildrootProvisioner struct {
// NewBuildrootProvisioner creates a new BuildrootProvisioner
func NewBuildrootProvisioner(d drivers.Driver) provision.Provisioner {
return &BuildrootProvisioner{
provision.NewSystemdProvisioner("buildroot", d),
NewSystemdProvisioner("buildroot", d),
}
}

Expand All @@ -65,7 +63,7 @@ func (p *BuildrootProvisioner) GenerateDockerOptions(dockerPort int) (*provision
noPivot := true
// Using pivot_root is not supported on fstype rootfs
if fstype, err := rootFileSystemType(p); err == nil {
log.Debugf("root file system type: %s", fstype)
glog.Infof("root file system type: %s", fstype)
noPivot = fstype == "rootfs"
}

Expand All @@ -79,7 +77,7 @@ Requires= minikube-automount.service docker.socket
Type=notify
`
if noPivot {
log.Warn("Using fundamentally insecure --no-pivot option")
glog.Warning("Using fundamentally insecure --no-pivot option")
engineConfigTmpl += `
# DOCKER_RAMDISK disables pivot_root in Docker, using MS_MOVE instead.
Environment=DOCKER_RAMDISK=yes
Expand Down Expand Up @@ -140,30 +138,11 @@ WantedBy=multi-user.target
return nil, err
}

dockerCfg := &provision.DockerOptions{
do := &provision.DockerOptions{
EngineOptions: engineCfg.String(),
EngineOptionsPath: "/lib/systemd/system/docker.service",
}

log.Info("Setting Docker configuration on the remote daemon...")

if _, err = p.SSHCommand(fmt.Sprintf("sudo mkdir -p %s && printf %%s \"%s\" | sudo tee %s", path.Dir(dockerCfg.EngineOptionsPath), dockerCfg.EngineOptions, dockerCfg.EngineOptionsPath)); err != nil {
return nil, err
}

// To make sure if there is a already-installed docker on the ISO to pick up the new systemd file
if err := p.Service("", serviceaction.DaemonReload); err != nil {
return nil, err
}

if err := p.Service("docker", serviceaction.Enable); err != nil {
return nil, err
}

if err := p.Service("docker", serviceaction.Restart); err != nil {
return nil, err
}
return dockerCfg, nil
return do, updateUnit(p, "docker", do.EngineOptions, do.EngineOptionsPath)
}

// Package installs a package
Expand All @@ -177,32 +156,32 @@ func (p *BuildrootProvisioner) Provision(swarmOptions swarm.Options, authOptions
p.AuthOptions = authOptions
p.EngineOptions = engineOptions

log.Infof("provisioning hostname %q", p.Driver.GetMachineName())
glog.Infof("provisioning hostname %q", p.Driver.GetMachineName())
if err := p.SetHostname(p.Driver.GetMachineName()); err != nil {
return err
}

p.AuthOptions = setRemoteAuthOptions(p)
log.Debugf("set auth options %+v", p.AuthOptions)
glog.Infof("set auth options %+v", p.AuthOptions)

log.Debugf("setting up certificates")
glog.Infof("setting up certificates")
configAuth := func() error {
if err := configureAuth(p); err != nil {
log.Warnf("configureAuth failed: %v", err)
glog.Warningf("configureAuth failed: %v", err)
return &retry.RetriableError{Err: err}
}
return nil
}

err := retry.Expo(configAuth, time.Second, 2*time.Minute)
if err != nil {
log.Debugf("Error configuring auth during provisioning %v", err)
glog.Infof("Error configuring auth during provisioning %v", err)
return err
}

log.Debugf("setting minikube options for container-runtime")
glog.Infof("setting minikube options for container-runtime")
if err := setContainerRuntimeOptions(p.Driver.GetMachineName(), p); err != nil {
log.Debugf("Error setting container-runtime options during provisioning %v", err)
glog.Infof("Error setting container-runtime options during provisioning %v", err)
return err
}

Expand Down
40 changes: 33 additions & 7 deletions pkg/provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (
"github.com/docker/machine/libmachine/cert"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/swarm"
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/command"
Expand Down Expand Up @@ -66,11 +66,24 @@ func init() {

}

// NewSystemdProvisioner is our fork of the same name in the upstream provision library, without the packages
func NewSystemdProvisioner(osReleaseID string, d drivers.Driver) provision.SystemdProvisioner {
return provision.SystemdProvisioner{
GenericProvisioner: provision.GenericProvisioner{
SSHCommander: provision.GenericSSHCommander{Driver: d},
DockerOptionsDir: "/etc/docker",
DaemonOptionsFile: "/etc/systemd/system/docker.service.d/10-machine.conf",
OsReleaseID: osReleaseID,
Driver: d,
},
}
}

func configureAuth(p miniProvisioner) error {
log.Infof("configureAuth start")
glog.Infof("configureAuth start")
start := time.Now()
defer func() {
log.Infof("configureAuth took %s", time.Since(start))
glog.Infof("configureAuth took %s", time.Since(start))
}()

driver := p.GetDriver()
Expand All @@ -90,7 +103,7 @@ func configureAuth(p miniProvisioner) error {

// The Host IP is always added to the certificate's SANs list
hosts := append(authOptions.ServerCertSANs, ip, "localhost", "127.0.0.1")
log.Debugf("generating server cert: %s ca-key=%s private-key=%s org=%s san=%s",
glog.Infof("generating server cert: %s ca-key=%s private-key=%s org=%s san=%s",
authOptions.ServerCertPath,
authOptions.CaCertPath,
authOptions.CaPrivateKeyPath,
Expand All @@ -116,11 +129,11 @@ func configureAuth(p miniProvisioner) error {
}

func copyHostCerts(authOptions auth.Options) error {
log.Infof("copyHostCerts")
glog.Infof("copyHostCerts")

err := os.MkdirAll(authOptions.StorePath, 0700)
if err != nil {
log.Errorf("mkdir failed: %v", err)
glog.Errorf("mkdir failed: %v", err)
}

hostCerts := map[string]string{
Expand All @@ -144,7 +157,7 @@ func copyHostCerts(authOptions auth.Options) error {
}

func copyRemoteCerts(authOptions auth.Options, driver drivers.Driver) error {
log.Infof("copyRemoteCerts")
glog.Infof("copyRemoteCerts")

remoteCerts := map[string]string{
authOptions.CaCertPath: authOptions.CaCertRemotePath,
Expand Down Expand Up @@ -276,3 +289,16 @@ func concatStrings(src []string, prefix string, postfix string) []string {
}
return ret
}

// updateUnit efficiently updates a systemd unit file
func updateUnit(p provision.SSHCommander, name string, content string, dst string) error {
glog.Infof("Updating %s unit: %s ...", name, dst)

if _, err := p.SSHCommand(fmt.Sprintf("sudo mkdir -p %s && printf %%s \"%s\" | sudo tee %s.new", path.Dir(dst), content, dst)); err != nil {
return err
}
if _, err := p.SSHCommand(fmt.Sprintf("sudo diff -u %s %s.new || { sudo mv %s.new %s; sudo systemctl -f daemon-reload && sudo sudo systemctl -f restart %s; }", dst, dst, dst, dst, name)); err != nil {
return err
}
return nil
}
48 changes: 14 additions & 34 deletions pkg/provision/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ package provision
import (
"bytes"
"fmt"
"path"
"text/template"
"time"

"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/provision/serviceaction"
"github.com/docker/machine/libmachine/swarm"
"github.com/golang/glog"
"k8s.io/minikube/pkg/util/retry"
)

Expand All @@ -43,7 +41,7 @@ type UbuntuProvisioner struct {
func NewUbuntuProvisioner(d drivers.Driver) provision.Provisioner {
return &UbuntuProvisioner{
BuildrootProvisioner{
provision.NewSystemdProvisioner("ubuntu", d),
NewSystemdProvisioner("ubuntu", d),
},
}
}
Expand All @@ -67,7 +65,7 @@ func (p *UbuntuProvisioner) GenerateDockerOptions(dockerPort int) (*provision.Do
noPivot := true
// Using pivot_root is not supported on fstype rootfs
if fstype, err := rootFileSystemType(p); err == nil {
log.Debugf("root file system type: %s", fstype)
glog.Infof("root file system type: %s", fstype)
noPivot = fstype == "rootfs"
}

Expand All @@ -83,7 +81,7 @@ Requires=docker.socket
Type=notify
`
if noPivot {
log.Warn("Using fundamentally insecure --no-pivot option")
glog.Warning("Using fundamentally insecure --no-pivot option")
engineConfigTmpl += `
# DOCKER_RAMDISK disables pivot_root in Docker, using MS_MOVE instead.
Environment=DOCKER_RAMDISK=yes
Expand Down Expand Up @@ -144,30 +142,11 @@ WantedBy=multi-user.target
return nil, err
}

dockerCfg := &provision.DockerOptions{
do := &provision.DockerOptions{
EngineOptions: engineCfg.String(),
EngineOptionsPath: "/lib/systemd/system/docker.service",
}

log.Info("Setting Docker configuration on the remote daemon...")

if _, err = p.SSHCommand(fmt.Sprintf("sudo mkdir -p %s && printf %%s \"%s\" | sudo tee %s", path.Dir(dockerCfg.EngineOptionsPath), dockerCfg.EngineOptions, dockerCfg.EngineOptionsPath)); err != nil {
return nil, err
}

// because in kic base image we pre-install docker it already has a service file. we need to daemon-reload for the new systemd file
if err := p.Service("", serviceaction.DaemonReload); err != nil {
return nil, err
}

if err := p.Service("docker", serviceaction.Enable); err != nil {
return nil, err
}

if err := p.Service("docker", serviceaction.Restart); err != nil {
return nil, err
}
return dockerCfg, nil
return do, updateUnit(p, "docker", do.EngineOptions, do.EngineOptionsPath)
}

// Package installs a package
Expand All @@ -181,32 +160,33 @@ func (p *UbuntuProvisioner) Provision(swarmOptions swarm.Options, authOptions au
p.AuthOptions = authOptions
p.EngineOptions = engineOptions

log.Infof("provisioning hostname %q", p.Driver.GetMachineName())
glog.Infof("provisioning hostname %q", p.Driver.GetMachineName())
if err := p.SetHostname(p.Driver.GetMachineName()); err != nil {
return err
}

p.AuthOptions = setRemoteAuthOptions(p)
log.Debugf("set auth options %+v", p.AuthOptions)
glog.Infof("set auth options %+v", p.AuthOptions)

log.Debugf("setting up certificates")
glog.Infof("setting up certificates")
configAuth := func() error {
if err := configureAuth(p); err != nil {
log.Warnf("configureAuth failed: %v", err)
glog.Warningf("configureAuth failed: %v", err)
return &retry.RetriableError{Err: err}
}
return nil
}

err := retry.Expo(configAuth, time.Second, 2*time.Minute)

if err != nil {
log.Debugf("Error configuring auth during provisioning %v", err)
glog.Infof("Error configuring auth during provisioning %v", err)
return err
}

log.Debugf("setting minikube options for container-runtime")
glog.Infof("setting minikube options for container-runtime")
if err := setContainerRuntimeOptions(p.Driver.GetMachineName(), p); err != nil {
log.Debugf("Error setting container-runtime options during provisioning %v", err)
glog.Infof("Error setting container-runtime options during provisioning %v", err)
return err
}

Expand Down