Move drivers to pkg/drivers, share utils

Share most of the disk image setup between hyperkit and kvm drivers.
Move and remove a lot of shared configuration between all the in-tree
drivers: kvm, hyperkit, none.
pull/1941/head
Matt Rickard 2017-09-10 14:59:11 -07:00
parent 3180e2e897
commit ebbc34d2ba
12 changed files with 152 additions and 130 deletions

View File

@ -18,7 +18,7 @@ package main
import ( import (
"github.com/docker/machine/libmachine/drivers/plugin" "github.com/docker/machine/libmachine/drivers/plugin"
"k8s.io/minikube/pkg/minikube/drivers/hyperkit" "k8s.io/minikube/pkg/drivers/hyperkit"
) )
func main() { func main() {

125
pkg/drivers/drivers.go Normal file
View File

@ -0,0 +1,125 @@
/*
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 drivers
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"github.com/cloudflare/cfssl/log"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/pkg/errors"
)
func GetDiskPath(d *drivers.BaseDriver) string {
return filepath.Join(d.ResolveStorePath("."), d.GetMachineName()+".rawdisk")
}
type CommonDriver struct{}
//Not implemented yet
func (d *CommonDriver) GetCreateFlags() []mcnflag.Flag {
return nil
}
//Not implemented yet
func (d *CommonDriver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}
func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error {
tarBuf, err := mcnutils.MakeDiskImage(sshKeyPath)
if err != nil {
return err
}
file, err := os.OpenFile(diskPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
file.Seek(0, os.SEEK_SET)
if _, err := file.Write(tarBuf.Bytes()); err != nil {
return err
}
if err := file.Close(); err != nil {
return errors.Wrapf(err, "closing file %s", diskPath)
}
if err := os.Truncate(diskPath, int64(diskSizeMb*1000000)); err != nil {
return err
}
return nil
}
func publicSSHKeyPath(d *drivers.BaseDriver) string {
return d.GetSSHKeyPath() + ".pub"
}
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func Restart(d drivers.Driver) error {
for _, f := range []func() error{d.Stop, d.Start} {
if err := f(); err != nil {
return err
}
}
return nil
}
func MakeDiskImage(d *drivers.BaseDriver, boot2dockerURL string, diskSize int) error {
//TODO(r2d4): rewrite this, not using b2dutils
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(boot2dockerURL, d.MachineName); err != nil {
return errors.Wrap(err, "Error copying ISO to machine dir")
}
log.Info("Creating ssh key...")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
log.Info("Creating raw disk image...")
diskPath := GetDiskPath(d)
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createRawDiskImage(publicSSHKeyPath(d), diskPath, diskSize); err != nil {
return err
}
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return err
}
}
return nil
}
func fixPermissions(path string) error {
os.Chown(path, syscall.Getuid(), syscall.Getegid())
files, _ := ioutil.ReadDir(path)
for _, f := range files {
fp := filepath.Join(path, f.Name())
if err := os.Chown(fp, syscall.Getuid(), syscall.Getegid()); err != nil {
return err
}
}
return nil
}

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package hyperkit package drivers
import ( import (
"io/ioutil" "io/ioutil"
@ -35,7 +35,7 @@ func Test_createDiskImage(t *testing.T) {
sizeInMb := 100 sizeInMb := 100
sizeInBytes := int64(sizeInMb) * 1000000 sizeInBytes := int64(sizeInMb) * 1000000
if err := createDiskImage(sshPath, diskPath, sizeInMb); err != nil { if err := createRawDiskImage(sshPath, diskPath, sizeInMb); err != nil {
t.Errorf("createDiskImage() error = %v", err) t.Errorf("createDiskImage() error = %v", err)
} }
fi, err := os.Lstat(diskPath) fi, err := os.Lstat(diskPath)

View File

@ -28,13 +28,12 @@ import (
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
hyperkit "github.com/moby/hyperkit/go" hyperkit "github.com/moby/hyperkit/go"
"github.com/pborman/uuid" "github.com/pborman/uuid"
"github.com/pkg/errors"
vmnet "github.com/zchee/go-vmnet" vmnet "github.com/zchee/go-vmnet"
pkgdrivers "k8s.io/minikube/pkg/drivers"
commonutil "k8s.io/minikube/pkg/util" commonutil "k8s.io/minikube/pkg/util"
) )
@ -46,6 +45,7 @@ const (
type Driver struct { type Driver struct {
*drivers.BaseDriver *drivers.BaseDriver
*pkgdrivers.CommonDriver
Boot2DockerURL string Boot2DockerURL string
DiskSize int DiskSize int
CPU int CPU int
@ -58,19 +58,16 @@ func NewDriver(hostName, storePath string) *Driver {
BaseDriver: &drivers.BaseDriver{ BaseDriver: &drivers.BaseDriver{
SSHUser: "docker", SSHUser: "docker",
}, },
CommonDriver: &pkgdrivers.CommonDriver{},
} }
} }
func (d *Driver) Create() error { func (d *Driver) Create() error {
b2dutils := mcnutils.NewB2dUtils(d.StorePath) // TODO: handle different disk types.
if err := pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize); err != nil {
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil { return errors.Wrap(err, "making disk image")
return err
} }
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return err
}
isoPath := d.ResolveStorePath(isoFilename) isoPath := d.ResolveStorePath(isoFilename)
if err := d.extractKernel(isoPath); err != nil { if err := d.extractKernel(isoPath); err != nil {
return err return err
@ -84,12 +81,6 @@ func (d *Driver) DriverName() string {
return "hyperkit" return "hyperkit"
} }
// GetCreateFlags returns the mcnflag.Flag slice representing the flags
// that can be set, their descriptions and defaults.
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return nil
}
// GetSSHHostname returns hostname for use with ssh // GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) { func (d *Driver) GetSSHHostname() (string, error) {
return d.IPAddress, nil return d.IPAddress, nil
@ -131,11 +122,6 @@ func (d *Driver) Kill() error {
return d.sendSignal(syscall.SIGKILL) return d.sendSignal(syscall.SIGKILL)
} }
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
func (d *Driver) PreCreateCheck() error {
return nil
}
// Remove a host // Remove a host
func (d *Driver) Remove() error { func (d *Driver) Remove() error {
s, err := d.GetState() s, err := d.GetState()
@ -150,37 +136,12 @@ func (d *Driver) Remove() error {
return nil return nil
} }
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func (d *Driver) Restart() error { func (d *Driver) Restart() error {
for _, f := range []func() error{d.Stop, d.Start} { return pkgdrivers.Restart(d)
if err := f(); err != nil {
return err
}
}
return nil
}
// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
return nil
} }
// Start a host // Start a host
func (d *Driver) Start() error { func (d *Driver) Start() error {
// TODO: handle different disk types.
diskPath := filepath.Join(d.ResolveStorePath("."), d.MachineName+".rawdisk")
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createDiskImage(d.publicSSHKeyPath(), diskPath, d.DiskSize); err != nil {
return err
}
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return err
}
}
h, err := hyperkit.New("", "", filepath.Join(d.StorePath, "machines", d.MachineName)) h, err := hyperkit.New("", "", filepath.Join(d.StorePath, "machines", d.MachineName))
if err != nil { if err != nil {
return err return err
@ -206,10 +167,9 @@ func (d *Driver) Start() error {
// Need to strip 0's // Need to strip 0's
mac = trimMacAddress(mac) mac = trimMacAddress(mac)
log.Infof("Generated MAC %s", mac) log.Infof("Generated MAC %s", mac)
h.Disks = []hyperkit.DiskConfig{ h.Disks = []hyperkit.DiskConfig{
{ {
Path: diskPath, Path: pkgdrivers.GetDiskPath(d.BaseDriver),
Size: d.DiskSize, Size: d.DiskSize,
Driver: "virtio-blk", Driver: "virtio-blk",
}, },
@ -256,10 +216,6 @@ func (d *Driver) extractKernel(isoPath string) error {
return nil return nil
} }
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}
func (d *Driver) sendSignal(s os.Signal) error { func (d *Driver) sendSignal(s os.Signal) error {
pid := d.getPid() pid := d.getPid()
proc, err := os.FindProcess(pid) proc, err := os.FindProcess(pid)

View File

@ -28,11 +28,10 @@ import (
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
libvirt "github.com/libvirt/libvirt-go" libvirt "github.com/libvirt/libvirt-go"
"github.com/pkg/errors" "github.com/pkg/errors"
pkgdrivers "k8s.io/minikube/pkg/drivers"
) )
const ( const (
@ -42,6 +41,7 @@ const (
type Driver struct { type Driver struct {
*drivers.BaseDriver *drivers.BaseDriver
*pkgdrivers.CommonDriver
// How much memory, in MB, to allocate to the VM // How much memory, in MB, to allocate to the VM
Memory int Memory int
@ -73,28 +73,20 @@ func NewDriver(hostName, storePath string) *Driver {
BaseDriver: &drivers.BaseDriver{ BaseDriver: &drivers.BaseDriver{
MachineName: hostName, MachineName: hostName,
StorePath: storePath, StorePath: storePath,
SSHUser: "docker",
}, },
CommonDriver: &pkgdrivers.CommonDriver{},
Boot2DockerURL: constants.DefaultIsoUrl, Boot2DockerURL: constants.DefaultIsoUrl,
CPU: constants.DefaultCPUS, CPU: constants.DefaultCPUS,
PrivateNetwork: defaultPrivateNetworkName,
DiskSize: util.CalculateDiskSizeInMB(constants.DefaultDiskSize), DiskSize: util.CalculateDiskSizeInMB(constants.DefaultDiskSize),
Memory: constants.DefaultMemory, Memory: constants.DefaultMemory,
PrivateNetwork: defaultPrivateNetworkName,
Network: defaultNetworkName, Network: defaultNetworkName,
DiskPath: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), fmt.Sprintf("%s.img", config.GetMachineName())), DiskPath: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), fmt.Sprintf("%s.rawdisk", config.GetMachineName())),
ISO: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), "boot2docker.iso"), ISO: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), "boot2docker.iso"),
} }
} }
//Not implemented yet
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return nil
}
//Not implemented yet
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}
func (d *Driver) PreCommandCheck() error { func (d *Driver) PreCommandCheck() error {
conn, err := getConnection() conn, err := getConnection()
if err != nil { if err != nil {
@ -171,30 +163,10 @@ func (d *Driver) GetIP() (string, error) {
return ip, nil return ip, nil
} }
func (d *Driver) GetMachineName() string {
return d.MachineName
}
func (d *Driver) GetSSHHostname() (string, error) { func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP() return d.GetIP()
} }
func (d *Driver) GetSSHUsername() string {
return "docker"
}
func (d *Driver) GetSSHKeyPath() string {
return d.ResolveStorePath("id_rsa")
}
func (d *Driver) GetSSHPort() (int, error) {
if d.SSHPort == 0 {
d.SSHPort = 22
}
return d.SSHPort, nil
}
func (d *Driver) DriverName() string { func (d *Driver) DriverName() string {
return "kvm" return "kvm"
} }
@ -210,16 +182,7 @@ func (d *Driver) Kill() error {
} }
func (d *Driver) Restart() error { func (d *Driver) Restart() error {
dom, conn, err := d.getDomain() return pkgdrivers.Restart(d)
if err != nil {
return errors.Wrap(err, "getting connection")
}
defer closeDomain(dom, conn)
if err := d.Stop(); err != nil {
return errors.Wrap(err, "stopping VM:")
}
return d.Start()
} }
func (d *Driver) Start() error { func (d *Driver) Start() error {
@ -269,13 +232,6 @@ func (d *Driver) Start() error {
func (d *Driver) Create() error { func (d *Driver) Create() error {
log.Info("Creating machine...") log.Info("Creating machine...")
//TODO(r2d4): rewrite this, not using b2dutils
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return errors.Wrap(err, "Error copying ISO to machine dir")
}
log.Info("Creating network...") log.Info("Creating network...")
err := d.createNetwork() err := d.createNetwork()
if err != nil { if err != nil {
@ -301,8 +257,7 @@ func (d *Driver) Create() error {
} }
log.Info("Building disk image...") log.Info("Building disk image...")
err = d.buildDiskImage() if err = pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize); err != nil {
if err != nil {
return errors.Wrap(err, "Error creating disk") return errors.Wrap(err, "Error creating disk")
} }

View File

@ -24,9 +24,9 @@ import (
"strings" "strings"
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/state" "github.com/docker/machine/libmachine/state"
"github.com/pkg/errors" "github.com/pkg/errors"
pkgdrivers "k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
) )
@ -35,6 +35,7 @@ const driverName = "none"
// none Driver is a driver designed to run localkube w/o a VM // none Driver is a driver designed to run localkube w/o a VM
type Driver struct { type Driver struct {
*drivers.BaseDriver *drivers.BaseDriver
*pkgdrivers.CommonDriver
URL string URL string
} }
@ -59,10 +60,6 @@ func (d *Driver) PreCreateCheck() error {
return nil return nil
} }
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{}
}
func (d *Driver) Create() error { func (d *Driver) Create() error {
// creation for the none driver is handled by commands.go // creation for the none driver is handled by commands.go
return nil return nil
@ -81,18 +78,10 @@ func (d *Driver) GetSSHHostname() (string, error) {
return "", fmt.Errorf("driver does not support ssh commands") return "", fmt.Errorf("driver does not support ssh commands")
} }
func (d *Driver) GetSSHKeyPath() string {
return ""
}
func (d *Driver) GetSSHPort() (int, error) { func (d *Driver) GetSSHPort() (int, error) {
return 0, fmt.Errorf("driver does not support ssh commands") return 0, fmt.Errorf("driver does not support ssh commands")
} }
func (d *Driver) GetSSHUsername() string {
return ""
}
func (d *Driver) GetURL() (string, error) { func (d *Driver) GetURL() (string, error) {
return "tcp://127.0.0.1:2376", nil return "tcp://127.0.0.1:2376", nil
} }
@ -155,10 +144,6 @@ func (d *Driver) Restart() error {
return nil return nil
} }
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}
func (d *Driver) Start() error { func (d *Driver) Start() error {
if err := os.MkdirAll("/usr/lib/systemd/system/", os.FileMode(0755)); err != nil { if err := os.MkdirAll("/usr/lib/systemd/system/", os.FileMode(0755)); err != nil {
return err return err

View File

@ -21,9 +21,9 @@ import (
"github.com/docker/machine/drivers/vmwarefusion" "github.com/docker/machine/drivers/vmwarefusion"
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"k8s.io/minikube/pkg/drivers/hyperkit"
cfg "k8s.io/minikube/pkg/minikube/config" cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/drivers/hyperkit"
) )
func createVMwareFusionHost(config MachineConfig) drivers.Driver { func createVMwareFusionHost(config MachineConfig) drivers.Driver {

View File

@ -22,9 +22,9 @@ import (
"path/filepath" "path/filepath"
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"k8s.io/minikube/pkg/drivers/none"
cfg "k8s.io/minikube/pkg/minikube/config" cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine/drivers/none"
) )
type kvmDriver struct { type kvmDriver struct {
@ -47,6 +47,7 @@ func createKVMHost(config MachineConfig) *kvmDriver {
BaseDriver: &drivers.BaseDriver{ BaseDriver: &drivers.BaseDriver{
MachineName: cfg.GetMachineName(), MachineName: cfg.GetMachineName(),
StorePath: constants.GetMinipath(), StorePath: constants.GetMinipath(),
SSHUser: "docker",
}, },
Memory: config.Memory, Memory: config.Memory,
CPU: config.CPUs, CPU: config.CPUs,
@ -54,7 +55,7 @@ func createKVMHost(config MachineConfig) *kvmDriver {
PrivateNetwork: "docker-machines", PrivateNetwork: "docker-machines",
Boot2DockerURL: config.Downloader.GetISOFileURI(config.MinikubeISO), Boot2DockerURL: config.Downloader.GetISOFileURI(config.MinikubeISO),
DiskSize: config.DiskSize, DiskSize: config.DiskSize,
DiskPath: filepath.Join(constants.GetMinipath(), "machines", cfg.GetMachineName(), fmt.Sprintf("%s.img", cfg.GetMachineName())), DiskPath: filepath.Join(constants.GetMinipath(), "machines", cfg.GetMachineName(), fmt.Sprintf("%s.rawdisk", cfg.GetMachineName())),
ISO: filepath.Join(constants.GetMinipath(), "machines", cfg.GetMachineName(), "boot2docker.iso"), ISO: filepath.Join(constants.GetMinipath(), "machines", cfg.GetMachineName(), "boot2docker.iso"),
CacheMode: "default", CacheMode: "default",
IOMode: "threads", IOMode: "threads",

View File

@ -24,7 +24,7 @@ import (
"github.com/docker/machine/libmachine/drivers/plugin" "github.com/docker/machine/libmachine/drivers/plugin"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/pkg/errors" "github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/machine/drivers/none" "k8s.io/minikube/pkg/drivers/none"
) )
var driverMap = map[string]driverGetter{ var driverMap = map[string]driverGetter{