提交 b0f685c0 编写于 作者: P Priya Wadhwa

Move all preloading code into preload package

上级 19fa2966
......@@ -22,6 +22,7 @@ import (
"os/exec"
"strconv"
"strings"
"time"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
......@@ -88,10 +89,13 @@ func (d *Driver) Create() error {
ContainerPort: constants.DockerDaemonPort,
},
)
volumeName, err := oci.CreatePreloadedImagesVolume(d.NodeConfig.KubernetesVersion)
t := time.Now()
glog.Infof("Starting creating preloaded images volume")
volumeName, err := oci.CreatePreloadedImagesVolume(d.NodeConfig.KubernetesVersion, BaseImage)
if err != nil {
glog.Infof("Unable to create preloaded images volume: %v", err)
}
glog.Infof("Finished creating preloaded images volume in %d seconds", time.Since(t).Seconds())
params.PreloadedVolume = volumeName
fmt.Println("Setting params.preloadedvolume = ", volumeName)
err = oci.CreateContainerNode(params)
......
......@@ -21,12 +21,11 @@ import (
"bytes"
"fmt"
"os/exec"
"path"
"strings"
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/drivers/kic/preload"
)
// DeleteAllVolumesByLabel deletes all volumes that have a specific label
......@@ -91,7 +90,7 @@ func allVolumesByLabel(ociBin string, label string) ([]string, error) {
}
// CreatePreloadedImagesVolume creates a volume with preloaded images
func CreatePreloadedImagesVolume(k8sVersion string) (string, error) {
func CreatePreloadedImagesVolume(k8sVersion, baseImage string) (string, error) {
if err := PointToHostDockerDaemon(); err != nil {
return "", errors.Wrap(err, "point host docker-daemon")
}
......@@ -102,10 +101,9 @@ func CreatePreloadedImagesVolume(k8sVersion string) (string, error) {
if err := createDockerVolume(volumeName); err != nil {
return "", errors.Wrap(err, "creating docker volume")
}
targetDir := localpath.MakeMiniPath("cache", "preloaded-tarball")
tarballPath := path.Join(targetDir, fmt.Sprintf("%s.tar", k8sVersion))
tarballPath := preload.TarballPath(k8sVersion)
if err := extractTarballToVolume(tarballPath, volumeName); err != nil {
if err := extractTarballToVolume(tarballPath, volumeName, baseImage); err != nil {
return "", errors.Wrap(err, "extracting tarball to volume")
}
return volumeName, nil
......@@ -129,11 +127,11 @@ func dockerVolumeExists(name string) bool {
return false
}
func extractTarballToVolume(tarballPath, volumeName string) error {
func extractTarballToVolume(tarballPath, volumeName, imageName string) error {
if err := PointToHostDockerDaemon(); err != nil {
return errors.Wrap(err, "point host docker-daemon")
}
cmd := exec.Command(Docker, "run", "--rm", "-v", fmt.Sprintf("%s:/preloaded.tar:ro", tarballPath), "-v", fmt.Sprintf("%s:/extractDir", volumeName), "busybox", "tar", "xvf", "/preloaded.tar", "-C", "/extractDir")
cmd := exec.Command(Docker, "run", "--rm", "--entrypoint", "/bin/bash", "-v", fmt.Sprintf("%s:/preloaded.tar:ro", tarballPath), "-v", fmt.Sprintf("%s:/extractDir", volumeName), imageName, "tar", "-I", "lz4", "-xvf", "/preloaded.tar", "-C", "/extractDir")
fmt.Println(cmd.Args)
if out, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "output %s", string(out))
......
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Copyright 2020 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.
......@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package kic
package preload
import (
"fmt"
......@@ -26,17 +26,23 @@ import (
"k8s.io/minikube/pkg/minikube/localpath"
)
// CachePreloadedTarball caches the preloaded images tarball on the host machine
func CachePreloadedTarball(k8sVersion string) error {
// TarballPath returns the path to the preloaded tarball
func TarballPath(k8sVersion string) string {
targetDir := localpath.MakeMiniPath("cache", "preloaded-tarball")
targetFilepath := path.Join(targetDir, fmt.Sprintf("%s.tar", k8sVersion))
targetFilepath := path.Join(targetDir, fmt.Sprintf("preloaded-images-k8s-%s.tar.lz4", k8sVersion))
return targetFilepath
}
// CacheTarball caches the preloaded images tarball on the host machine
func CacheTarball(k8sVersion string) error {
targetFilepath := TarballPath(k8sVersion)
if _, err := os.Stat(targetFilepath); err == nil {
glog.Infof("Found %s in cache, skipping downloading", targetFilepath)
return nil
}
url := fmt.Sprintf("https://storage.googleapis.com/minikube-docker-volume-tarballs/%s-k8s-%s.tar", Version, k8sVersion)
url := fmt.Sprintf("https://storage.googleapis.com/minikube-docker-volume-tarballs/preloaded-images-k8s-%s.tar", k8sVersion)
glog.Infof("Downloading %s to %s", url, targetFilepath)
return download.ToFile(url, targetFilepath, download.FileOptions{Mkdirs: download.MkdirAll})
}
......@@ -25,6 +25,7 @@ import (
"golang.org/x/sync/errgroup"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/drivers/kic"
"k8s.io/minikube/pkg/drivers/kic/preload"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
......@@ -51,17 +52,17 @@ func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion, driverName strin
if !viper.GetBool("download-only") {
return
}
var kicArtifactsGroup errgroup.Group
if driver.IsKIC(driverName) { // for kic we need to find what port docker/podman chose for us
// Download kic base image and preloaded images tarball
beginDownloadKicArtifacts(&kicArtifactsGroup, k8sVersion)
}
if err := doCacheBinaries(k8sVersion); err != nil {
exit.WithError("Failed to cache binaries", err)
}
if _, err := CacheKubectlBinary(k8sVersion); err != nil {
exit.WithError("Failed to cache kubectl", err)
}
var kicArtifactsGroup errgroup.Group
if driver.IsKIC(driverName) { // for kic we need to find what port docker/podman chose for us
// Download kic base image and preloaded images tarball
beginDownloadKicArtifacts(&kicArtifactsGroup, k8sVersion)
}
waitCacheRequiredImages(cacheGroup)
waitDownloadKicArtifacts(&kicArtifactsGroup)
if err := saveImagesToTarFromConfig(); err != nil {
......@@ -96,7 +97,7 @@ func beginDownloadKicArtifacts(g *errgroup.Group, k8sVersion string) {
g.Go(func() error {
glog.Info("Caching tarball of preloaded images")
return kic.CachePreloadedTarball(k8sVersion)
return preload.CacheTarball(k8sVersion)
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册