Download preloaded images tarball with --download-only flag

pull/6863/head
Priya Wadhwa 2020-02-19 21:53:44 -08:00
parent ea71480799
commit d506aa18b4
5 changed files with 104 additions and 2 deletions

41
pkg/drivers/kic/cache.go Normal file
View File

@ -0,0 +1,41 @@
/*
Copyright 2019 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 kic
import (
"fmt"
"os"
"path"
"github.com/golang/glog"
"github.com/jimmidyson/go-download"
"k8s.io/minikube/pkg/minikube/localpath"
)
func CachePreloadedTarball(k8sVersion string) error {
targetDir := localpath.MakeMiniPath("cache", "preloaded-tarball")
targetFilepath := path.Join(targetDir, fmt.Sprintf("%s-k8s-%s.tar", Version, 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)
glog.Infof("Downloading %s to %s", url, targetFilepath)
return download.ToFile(url, targetFilepath, download.FileOptions{Mkdirs: download.MkdirAll})
}

View File

@ -24,6 +24,9 @@ const (
// DefaultPodCIDR is The CIDR to be used for pods inside the node.
DefaultPodCIDR = "10.244.0.0/16"
// Version is the current version of kic
Version = "v0.0.5"
// BaseImage is the base image is used to spin up kic containers. it uses same base-image as kind.
BaseImage = "gcr.io/k8s-minikube/kicbase:v0.0.5@sha256:3ddd8461dfb5c3e452ccc44d87750b87a574ec23fc425da67dccc1f0c57d428a"
// OverlayImage is the cni plugin used for overlay image, created by kind.

View File

@ -21,6 +21,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/docker/docker/client"
@ -30,6 +31,8 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/constants"
)
@ -69,6 +72,33 @@ func DigestByGoLib(imgName string) string {
return cf.Hex
}
// WriteImageToDaemon write img to the local docker daemon
func WriteImageToDaemon(img string) error {
glog.Infof("Writing %s to local daemon", img)
if err := oci.PointToHostDockerDaemon(); err != nil {
return errors.Wrap(err, "point host docker-daemon")
}
// Check if image exists locally
ref, err := name.ParseReference(img)
if err != nil {
return errors.Wrap(err, "parsing reference")
}
if _, err := daemon.Image(ref); err == nil {
glog.Infof("Found %s in local docker daemon, skipping pull", img)
return nil
}
i, err := remote.Image(ref)
if err != nil {
return errors.Wrap(err, "getting remote image")
}
tag, err := name.NewTag(strings.Split(img, "@")[0])
if err != nil {
return errors.Wrap(err, "getting tag")
}
_, err = daemon.Write(tag, i)
return err
}
func retrieveImage(ref name.Reference) (v1.Image, error) {
glog.Infof("retrieving image: %+v", ref)
img, err := daemon.Image(ref)

View File

@ -24,8 +24,10 @@ import (
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/drivers/kic"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/image"
"k8s.io/minikube/pkg/minikube/localpath"
@ -44,7 +46,7 @@ func beginCacheRequiredImages(g *errgroup.Group, imageRepository string, k8sVers
})
}
func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion string) {
func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion, driverName string) {
// If --download-only, complete the remaining downloads and exit.
if !viper.GetBool("download-only") {
return
@ -55,7 +57,13 @@ func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion string) {
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 {
exit.WithError("Failed to cache images to tar", err)
}
@ -79,6 +87,26 @@ func doCacheBinaries(k8sVersion string) error {
return machine.CacheBinariesForBootstrapper(k8sVersion, viper.GetString(cmdcfg.Bootstrapper))
}
func beginDownloadKicArtifacts(g *errgroup.Group, k8sVersion string) {
glog.Info("Beginning downloading kic artifacts")
g.Go(func() error {
glog.Infof("Downloading %s to local daemon", kic.BaseImage)
return image.WriteImageToDaemon(kic.BaseImage)
})
g.Go(func() error {
glog.Info("Caching tarball of preloaded images")
return kic.CachePreloadedTarball(k8sVersion)
})
}
func waitDownloadKicArtifacts(g *errgroup.Group) {
if err := g.Wait(); err != nil {
glog.Errorln("Error downloading kic artifacts: ", err)
}
glog.Info("Succesfully downloaded all kic artifacts")
}
// waitCacheRequiredImages blocks until the required images are all cached.
func waitCacheRequiredImages(g *errgroup.Group) {
if !viper.GetBool(cacheImages) {

View File

@ -47,7 +47,7 @@ func Start(mc config.MachineConfig, n config.Node, primary bool, existingAddons
k8sVersion := mc.KubernetesConfig.KubernetesVersion
driverName := mc.Driver
// exits here in case of --download-only option.
handleDownloadOnly(&cacheGroup, k8sVersion)
handleDownloadOnly(&cacheGroup, k8sVersion, driverName)
mRunner, preExists, machineAPI, host := startMachine(&mc, &n)
defer machineAPI.Close()
// configure the runtime (docker, containerd, crio)