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.
pull/3737/head
Anders F Björklund 2019-03-23 16:49:30 +01:00
parent adc8cf079c
commit adcbcf113b
4 changed files with 55 additions and 18 deletions

View File

@ -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() {

View File

@ -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
}

View File

@ -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)

View File

@ -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