2016-08-23 15:03:41 +00:00
/ *
Copyright 2016 The Kubernetes Authors All rights reserved .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package provision
import (
"bytes"
"fmt"
"path"
2016-10-17 09:00:43 +00:00
"text/template"
2016-11-16 19:50:08 +00:00
"time"
2016-08-23 15:03:41 +00:00
"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/swarm"
2016-11-16 19:50:08 +00:00
"k8s.io/minikube/pkg/util"
2016-08-23 15:03:41 +00:00
)
type BuildrootProvisioner struct {
provision . SystemdProvisioner
}
func init ( ) {
provision . Register ( "Buildroot" , & provision . RegisteredProvisioner {
New : NewBuildrootProvisioner ,
} )
}
func NewBuildrootProvisioner ( d drivers . Driver ) provision . Provisioner {
return & BuildrootProvisioner {
provision . NewSystemdProvisioner ( "buildroot" , d ) ,
}
}
func ( p * BuildrootProvisioner ) String ( ) string {
return "buildroot"
}
func ( p * BuildrootProvisioner ) GenerateDockerOptions ( dockerPort int ) ( * provision . DockerOptions , error ) {
var engineCfg bytes . Buffer
driverNameLabel := fmt . Sprintf ( "provider=%s" , p . Driver . DriverName ( ) )
p . EngineOptions . Labels = append ( p . EngineOptions . Labels , driverNameLabel )
engineConfigTmpl := ` [ Unit ]
Description = Docker Application Container Engine
Documentation = https : //docs.docker.com
After = network . target docker . socket
Requires = docker . socket
[ Service ]
Type = notify
# DOCKER_RAMDISK disables pivot_root in Docker , using MS_MOVE instead .
Environment = DOCKER_RAMDISK = yes
2016-10-27 21:50:36 +00:00
{ { range . EngineOptions . Env } } Environment = { { . } }
{ { end } }
2017-04-05 04:40:05 +00:00
2017-04-10 02:38:45 +00:00
# This file is a systemd drop - in unit that inherits from the base dockerd configuration .
# The base configuration already specifies an ' ExecStart = ... ' command . The first directive
# here is to clear out that command inherited from the base configuration . Without this ,
# the command from the base configuration and the command specified here are treated as
# a sequence of commands , which is not the desired behavior , nor is it valid -- systemd
# will catch this invalid input and refuse to start the service with an error like :
# Service has more than one ExecStart = setting , which is only allowed for Type = oneshot services .
2017-04-05 04:40:05 +00:00
ExecStart =
2016-08-23 15:03:41 +00:00
ExecStart = / usr / bin / docker daemon - H tcp : //0.0.0.0:{{.DockerPort}} -H unix:///var/run/docker.sock --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}} {{ range .EngineOptions.Labels }}--label {{.}} {{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}} {{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}} {{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}} {{ end }}
ExecReload = / bin / kill - s HUP $ MAINPID
# Having non - zero Limit * s causes performance problems due to accounting overhead
# in the kernel . We recommend using cgroups to do container - local accounting .
LimitNOFILE = infinity
LimitNPROC = infinity
LimitCORE = infinity
# Uncomment TasksMax if your systemd version supports it .
# Only systemd 226 and above support this version .
TasksMax = infinity
TimeoutStartSec = 0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate = yes
# kill only the docker process , not all processes in the cgroup
KillMode = process
[ Install ]
WantedBy = multi - user . target
`
t , err := template . New ( "engineConfig" ) . Parse ( engineConfigTmpl )
if err != nil {
return nil , err
}
engineConfigContext := provision . EngineConfigContext {
DockerPort : dockerPort ,
AuthOptions : p . AuthOptions ,
EngineOptions : p . EngineOptions ,
}
t . Execute ( & engineCfg , engineConfigContext )
return & provision . DockerOptions {
EngineOptions : engineCfg . String ( ) ,
EngineOptionsPath : p . DaemonOptionsFile ,
} , nil
}
func ( p * BuildrootProvisioner ) Package ( name string , action pkgaction . PackageAction ) error {
return nil
}
func ( p * BuildrootProvisioner ) Provision ( swarmOptions swarm . Options , authOptions auth . Options , engineOptions engine . Options ) error {
p . SwarmOptions = swarmOptions
p . AuthOptions = authOptions
p . EngineOptions = engineOptions
log . Debugf ( "setting 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 )
log . Debugf ( "setting up certificates" )
2016-11-16 19:50:08 +00:00
configureAuth := func ( ) error {
if err := provision . ConfigureAuth ( p ) ; err != nil {
return & util . RetriableError { Err : err }
}
return nil
}
err := util . RetryAfter ( 5 , configureAuth , time . Second * 10 )
if err != nil {
log . Debugf ( "Error configuring auth during provisioning %v" , err )
2016-08-23 15:03:41 +00:00
return err
}
return nil
}
func setRemoteAuthOptions ( p provision . Provisioner ) auth . Options {
dockerDir := p . GetDockerOptionsDir ( )
authOptions := p . GetAuthOptions ( )
// due to windows clients, we cannot use filepath.Join as the paths
// will be mucked on the linux hosts
authOptions . CaCertRemotePath = path . Join ( dockerDir , "ca.pem" )
authOptions . ServerCertRemotePath = path . Join ( dockerDir , "server.pem" )
authOptions . ServerKeyRemotePath = path . Join ( dockerDir , "server-key.pem" )
return authOptions
}