Refactored iso caching code

pull/362/head
Aaron Prindle 2016-07-20 09:50:10 -07:00
parent 072991e2e5
commit 3e06673fc5
7 changed files with 56 additions and 42 deletions

View File

@ -35,8 +35,9 @@ var dirs = [...]string{
constants.Minipath,
constants.MakeMiniPath("certs"),
constants.MakeMiniPath("machines"),
constants.MakeMiniPath("iso"),
constants.MakeMiniPath("localkube"),
constants.MakeMiniPath("cache"),
constants.MakeMiniPath("cache", "iso"),
constants.MakeMiniPath("cache", "localkube"),
constants.MakeMiniPath("config")}
var (

View File

@ -25,6 +25,7 @@ import (
"net"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
@ -296,56 +297,52 @@ func engineOptions(config MachineConfig) *engine.Options {
func createVirtualboxHost(config MachineConfig) drivers.Driver {
d := virtualbox.NewDriver(constants.MachineName, constants.Minipath)
d.Boot2DockerURL = config.MinikubeISO
d.Boot2DockerURL = config.GetISOCacheFileURI()
d.Memory = config.Memory
d.CPU = config.CPUs
d.DiskSize = int(config.DiskSize)
return d
}
func isISONotCachedInMinikubeDir(isoFilePath string) bool {
if _, err := os.Stat(isoFilePath); os.IsNotExist(err) {
return true
}
return false
}
func getISOFilePathFromConfig(config MachineConfig) string {
split := strings.Split(config.MinikubeISO, "/")
isoFilename := split[len(split)-1]
return filepath.Join(constants.Minipath, "iso", isoFilename)
}
func modifyConfigForCachedIso(isoFilePath string, config *MachineConfig) error {
if isISONotCachedInMinikubeDir(isoFilePath) {
// download miniube-iso to .minikube dir
response, err := http.Get(config.MinikubeISO)
func (m *MachineConfig) CacheMinikubeISO() error {
// store the miniube-iso inside the .minikube dir
response, err := http.Get(m.MinikubeISO)
if err != nil {
return err
} else {
out, err := os.Create(m.GetISOCacheFilepath())
if err != nil {
return err
} else {
out, err := os.Create(isoFilePath)
if err != nil {
return err
}
defer out.Close()
defer response.Body.Close()
_, err = io.Copy(out, response.Body)
if err != nil {
return err
}
}
defer out.Close()
defer response.Body.Close()
if _, err = io.Copy(out, response.Body); err != nil {
return err
}
}
config.MinikubeISO = "file://" + isoFilePath
return nil
}
func (m *MachineConfig) GetISOCacheFilepath() string {
return filepath.Join(constants.Minipath, "cache", "iso", filepath.Base(m.MinikubeISO))
}
func (m *MachineConfig) GetISOCacheFileURI() string {
return "file://" + path.Join(constants.Minipath, "cache", "iso", filepath.Base(m.MinikubeISO))
}
func (m *MachineConfig) IsMinikubeISOCached() bool {
if _, err := os.Stat(m.GetISOCacheFilepath()); os.IsNotExist(err) {
return false
}
return true
}
func createHost(api libmachine.API, config MachineConfig) (*host.Host, error) {
var driver interface{}
//check cache for iso and change iso method appropriately
fmt.Println(config.MinikubeISO)
if config.MinikubeISO != "" { // "" is a test param
if err := modifyConfigForCachedIso(getISOFilePathFromConfig(config), &config); err != nil {
if !config.IsMinikubeISOCached() {
if err := config.CacheMinikubeISO(); err != nil {
return nil, err
}
}

View File

@ -24,7 +24,7 @@ import (
func createVMwareFusionHost(config MachineConfig) drivers.Driver {
d := vmwarefusion.NewDriver(constants.MachineName, constants.Minipath).(*vmwarefusion.Driver)
d.Boot2DockerURL = config.MinikubeISO
d.Boot2DockerURL = config.GetISOCacheFileURI()
d.Memory = config.Memory
d.CPU = config.CPUs
@ -59,7 +59,7 @@ func createXhyveHost(config MachineConfig) *xhyveDriver {
},
Memory: config.Memory,
CPU: config.CPUs,
Boot2DockerURL: config.MinikubeISO,
Boot2DockerURL: config.GetISOCacheFileURI(),
BootCmd: "loglevel=3 user=docker console=ttyS0 console=tty0 noembed nomodeset norestore waitusb=10 base host=boot2docker",
DiskSize: int64(config.DiskSize),
}

View File

@ -49,7 +49,7 @@ func createKVMHost(config MachineConfig) *kvmDriver {
CPU: config.CPUs,
Network: "default",
PrivateNetwork: "docker-machines",
Boot2DockerURL: config.MinikubeISO,
Boot2DockerURL: config.GetISOCacheFileURI(),
DiskSize: config.DiskSize,
DiskPath: filepath.Join(constants.Minipath, "machines", constants.MachineName, fmt.Sprintf("%s.img", constants.MachineName)),
ISO: filepath.Join(constants.Minipath, "machines", constants.MachineName, "boot2docker.iso"),

View File

@ -33,7 +33,8 @@ import (
"k8s.io/minikube/pkg/minikube/tests"
)
var defaultMachineConfig = MachineConfig{VMDriver: constants.DefaultVMDriver}
var defaultMachineConfig = MachineConfig{VMDriver: constants.DefaultVMDriver,
MinikubeISO: constants.DefaultIsoUrl}
func TestCreateHost(t *testing.T) {
api := tests.NewMockAPI()
@ -361,6 +362,9 @@ func TestSetupCerts(t *testing.T) {
}
func TestGetHostDockerEnv(t *testing.T) {
tempDir := tests.MakeTempDir()
defer os.RemoveAll(tempDir)
api := tests.NewMockAPI()
h, err := createHost(api, defaultMachineConfig)
if err != nil {

View File

@ -40,8 +40,10 @@ var KubeconfigPath = clientcmd.RecommendedHomeFile
const MinikubeContext = "minikube"
// MakeMiniPath is a utility to calculate a relative path to our directory.
func MakeMiniPath(fileName string) string {
return filepath.Join(Minipath, fileName)
func MakeMiniPath(fileName ...string) string {
args := []string{Minipath}
args = append(args, fileName...)
return filepath.Join(args...)
}
// Only pass along these flags to localkube.

View File

@ -19,6 +19,8 @@ package tests
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"k8s.io/minikube/pkg/minikube/constants"
)
@ -28,6 +30,14 @@ func MakeTempDir() string {
if err != nil {
log.Fatal(err)
}
err = os.MkdirAll(filepath.Join(tempDir, "cache", "iso"), 0777)
if err != nil {
log.Fatal(err)
}
err = os.MkdirAll(filepath.Join(tempDir, "cache", "localkube"), 0777)
if err != nil {
log.Fatal(err)
}
constants.Minipath = tempDir
return tempDir
}