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 (
"github.com/docker/machine/libmachine/drivers/plugin"
"k8s.io/minikube/pkg/minikube/drivers/hyperkit"
"k8s.io/minikube/pkg/drivers/hyperkit"
)
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.
*/
package hyperkit
package drivers
import (
"io/ioutil"
@ -35,7 +35,7 @@ func Test_createDiskImage(t *testing.T) {
sizeInMb := 100
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)
}
fi, err := os.Lstat(diskPath)

View File

@ -28,13 +28,12 @@ import (
"github.com/docker/machine/libmachine/drivers"
"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"
hyperkit "github.com/moby/hyperkit/go"
"github.com/pborman/uuid"
"github.com/pkg/errors"
vmnet "github.com/zchee/go-vmnet"
pkgdrivers "k8s.io/minikube/pkg/drivers"
commonutil "k8s.io/minikube/pkg/util"
)
@ -46,6 +45,7 @@ const (
type Driver struct {
*drivers.BaseDriver
*pkgdrivers.CommonDriver
Boot2DockerURL string
DiskSize int
CPU int
@ -58,19 +58,16 @@ func NewDriver(hostName, storePath string) *Driver {
BaseDriver: &drivers.BaseDriver{
SSHUser: "docker",
},
CommonDriver: &pkgdrivers.CommonDriver{},
}
}
func (d *Driver) Create() error {
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
// TODO: handle different disk types.
if err := pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize); err != nil {
return errors.Wrap(err, "making disk image")
}
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return err
}
isoPath := d.ResolveStorePath(isoFilename)
if err := d.extractKernel(isoPath); err != nil {
return err
@ -84,12 +81,6 @@ func (d *Driver) DriverName() string {
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
func (d *Driver) GetSSHHostname() (string, error) {
return d.IPAddress, nil
@ -131,11 +122,6 @@ func (d *Driver) Kill() error {
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
func (d *Driver) Remove() error {
s, err := d.GetState()
@ -150,37 +136,12 @@ func (d *Driver) Remove() error {
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 {
for _, f := range []func() error{d.Stop, d.Start} {
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
return pkgdrivers.Restart(d)
}
// Start a host
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))
if err != nil {
return err
@ -206,10 +167,9 @@ func (d *Driver) Start() error {
// Need to strip 0's
mac = trimMacAddress(mac)
log.Infof("Generated MAC %s", mac)
h.Disks = []hyperkit.DiskConfig{
{
Path: diskPath,
Path: pkgdrivers.GetDiskPath(d.BaseDriver),
Size: d.DiskSize,
Driver: "virtio-blk",
},
@ -256,10 +216,6 @@ func (d *Driver) extractKernel(isoPath string) error {
return nil
}
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}
func (d *Driver) sendSignal(s os.Signal) error {
pid := d.getPid()
proc, err := os.FindProcess(pid)

View File

@ -28,11 +28,10 @@ import (
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/state"
libvirt "github.com/libvirt/libvirt-go"
"github.com/pkg/errors"
pkgdrivers "k8s.io/minikube/pkg/drivers"
)
const (
@ -42,6 +41,7 @@ const (
type Driver struct {
*drivers.BaseDriver
*pkgdrivers.CommonDriver
// How much memory, in MB, to allocate to the VM
Memory int
@ -73,28 +73,20 @@ func NewDriver(hostName, storePath string) *Driver {
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
SSHUser: "docker",
},
CommonDriver: &pkgdrivers.CommonDriver{},
Boot2DockerURL: constants.DefaultIsoUrl,
CPU: constants.DefaultCPUS,
PrivateNetwork: defaultPrivateNetworkName,
DiskSize: util.CalculateDiskSizeInMB(constants.DefaultDiskSize),
Memory: constants.DefaultMemory,
PrivateNetwork: defaultPrivateNetworkName,
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"),
}
}
//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 {
conn, err := getConnection()
if err != nil {
@ -171,30 +163,10 @@ func (d *Driver) GetIP() (string, error) {
return ip, nil
}
func (d *Driver) GetMachineName() string {
return d.MachineName
}
func (d *Driver) GetSSHHostname() (string, error) {
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 {
return "kvm"
}
@ -210,16 +182,7 @@ func (d *Driver) Kill() error {
}
func (d *Driver) Restart() error {
dom, conn, err := d.getDomain()
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()
return pkgdrivers.Restart(d)
}
func (d *Driver) Start() error {
@ -269,13 +232,6 @@ func (d *Driver) Start() error {
func (d *Driver) Create() error {
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...")
err := d.createNetwork()
if err != nil {
@ -301,8 +257,7 @@ func (d *Driver) Create() error {
}
log.Info("Building disk image...")
err = d.buildDiskImage()
if err != nil {
if err = pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize); err != nil {
return errors.Wrap(err, "Error creating disk")
}

View File

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

View File

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

View File

@ -22,9 +22,9 @@ import (
"path/filepath"
"github.com/docker/machine/libmachine/drivers"
"k8s.io/minikube/pkg/drivers/none"
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine/drivers/none"
)
type kvmDriver struct {
@ -47,6 +47,7 @@ func createKVMHost(config MachineConfig) *kvmDriver {
BaseDriver: &drivers.BaseDriver{
MachineName: cfg.GetMachineName(),
StorePath: constants.GetMinipath(),
SSHUser: "docker",
},
Memory: config.Memory,
CPU: config.CPUs,
@ -54,7 +55,7 @@ func createKVMHost(config MachineConfig) *kvmDriver {
PrivateNetwork: "docker-machines",
Boot2DockerURL: config.Downloader.GetISOFileURI(config.MinikubeISO),
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"),
CacheMode: "default",
IOMode: "threads",

View File

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