From adcbcf113b9e6340cfa0015328902217429562c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sat, 23 Mar 2019 16:49:30 +0100 Subject: [PATCH] Refactor: move cache calls into separate functions Need to split out the caching functions, from the loading/copying. Also need to make the lists accessible from outside the functions. --- cmd/minikube/cmd/cache.go | 21 ++++++++++---- pkg/minikube/bootstrapper/kubeadm/kubeadm.go | 30 ++++++++++++++------ pkg/minikube/cluster/cluster.go | 17 ++++++++--- pkg/minikube/constants/constants.go | 5 ++++ 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/cmd/minikube/cmd/cache.go b/cmd/minikube/cmd/cache.go index b6fdb246ca..86926c5d35 100644 --- a/cmd/minikube/cmd/cache.go +++ b/cmd/minikube/cmd/cache.go @@ -66,20 +66,31 @@ var deleteCacheCmd = &cobra.Command{ }, } -// LoadCachedImagesInConfigFile loads the images currently in the config file (minikube start) -func LoadCachedImagesInConfigFile() error { +func imagesInConfigFile() ([]string, error) { configFile, err := config.ReadConfig() if err != nil { - return err + return nil, err } if values, ok := configFile[constants.Cache]; ok { var images []string for key := range values.(map[string]interface{}) { images = append(images, key) } - return machine.CacheAndLoadImages(images) + return images, nil } - return nil + return []string{}, nil +} + +// LoadCachedImagesInConfigFile loads the images currently in the config file (minikube start) +func LoadCachedImagesInConfigFile() error { + images, err := imagesInConfigFile() + if err != nil { + return err + } + if len(images) == 0 { + return nil + } + return machine.CacheAndLoadImages(images) } func init() { diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go index 6be7e6b2ab..2be01ac731 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm.go @@ -466,19 +466,16 @@ func (k *Bootstrapper) UpdateCluster(cfg config.KubernetesConfig) error { } var g errgroup.Group - for _, bin := range []string{"kubelet", "kubeadm"} { + for _, bin := range constants.GetKubeadmCachedBinaries() { bin := bin g.Go(func() error { - path, err := maybeDownloadAndCache(bin, cfg.KubernetesVersion) + path, err := CacheBinary(bin, cfg.KubernetesVersion) if err != nil { return errors.Wrapf(err, "downloading %s", bin) } - f, err := assets.NewFileAsset(path, "/usr/bin", bin, "0641") + err = CopyBinary(k.c, bin, path) if err != nil { - return errors.Wrap(err, "new file asset") - } - if err := k.c.Copy(f); err != nil { - return errors.Wrapf(err, "copy") + return errors.Wrapf(err, "copying %s", bin) } return nil }) @@ -584,13 +581,17 @@ func generateConfig(k8s config.KubernetesConfig, r cruntime.Manager) (string, er return b.String(), nil } -func maybeDownloadAndCache(binary, version string) (string, error) { +// CacheBinary will cache a binary on the host +func CacheBinary(binary, version string) (string, error) { targetDir := constants.MakeMiniPath("cache", version) targetFilepath := path.Join(targetDir, binary) + url := constants.GetKubernetesReleaseURL(binary, version) + _, err := os.Stat(targetFilepath) // If it exists, do no verification and continue if err == nil { + glog.Infof("Not caching binary, using %s", url) return targetFilepath, nil } if !os.IsNotExist(err) { @@ -601,7 +602,6 @@ func maybeDownloadAndCache(binary, version string) (string, error) { return "", errors.Wrapf(err, "mkdir %s", targetDir) } - url := constants.GetKubernetesReleaseURL(binary, version) options := download.FileOptions{ Mkdirs: download.MkdirAll, } @@ -615,3 +615,15 @@ func maybeDownloadAndCache(binary, version string) (string, error) { } return targetFilepath, nil } + +// CopyBinary copies previously cached binaries into the path +func CopyBinary(cr bootstrapper.CommandRunner, binary, path string) error { + f, err := assets.NewFileAsset(path, "/usr/bin", binary, "0641") + if err != nil { + return errors.Wrap(err, "new file asset") + } + if err := cr.Copy(f); err != nil { + return errors.Wrapf(err, "copy") + } + return nil +} diff --git a/pkg/minikube/cluster/cluster.go b/pkg/minikube/cluster/cluster.go index 4ad5aa3d21..cf500b7868 100644 --- a/pkg/minikube/cluster/cluster.go +++ b/pkg/minikube/cluster/cluster.go @@ -58,6 +58,16 @@ func init() { ssh.SetDefaultClient(ssh.Native) } +// CacheISO downloads and caches ISO. +func CacheISO(config cfg.MachineConfig) error { + if config.VMDriver != "none" { + if err := config.Downloader.CacheMinikubeISOFromURL(config.MinikubeISO); err != nil { + return err + } + } + return nil +} + // StartHost starts a host VM. func StartHost(api libmachine.API, config cfg.MachineConfig) (*host.Host, error) { exists, err := api.Exists(cfg.GetMachineName()) @@ -280,10 +290,9 @@ func createHost(api libmachine.API, config cfg.MachineConfig) (*host.Host, error exit.WithError("error getting driver", err) } - if config.VMDriver != "none" { - if err := config.Downloader.CacheMinikubeISOFromURL(config.MinikubeISO); err != nil { - return nil, errors.Wrap(err, "unable to cache ISO") - } + err = CacheISO(config) + if err != nil { + return nil, errors.Wrap(err, "unable to cache ISO") } driver := def.ConfigCreator(config) diff --git a/pkg/minikube/constants/constants.go b/pkg/minikube/constants/constants.go index 82ab828df2..e081019c01 100644 --- a/pkg/minikube/constants/constants.go +++ b/pkg/minikube/constants/constants.go @@ -224,6 +224,11 @@ const DriverNone = "none" // FileScheme is the file scheme const FileScheme = "file" +// GetKubeadmCachedBinaries gets the binaries to cache for kubeadm +func GetKubeadmCachedBinaries() []string { + return []string{"kubelet", "kubeadm"} +} + // GetKubeadmCachedImages gets the images to cache for kubeadm for a version func GetKubeadmCachedImages(imageRepository string, kubernetesVersionStr string) (string, []string) { minikubeRepository := imageRepository