Load kicbase image from right cache and add log

It was silently failing to load from the (wrong) cache directory,
causing the image to be download twice from the network instead.

Add new function to get the path in the cache directory, so that
we don't have to duplicate that both inside and outside module.
pull/11346/head
Anders F Björklund 2021-05-08 09:27:59 +02:00
parent d61989afa9
commit 580161ad1c
3 changed files with 18 additions and 15 deletions

View File

@ -43,10 +43,16 @@ var (
}
)
// imageExistsInCache if img exist in local cache directory
func imageExistsInCache(img string) bool {
// ImagePathInCache returns path in local cache directory
func ImagePathInCache(img string) string {
f := filepath.Join(constants.KICCacheDir, path.Base(img)+".tar")
f = localpath.SanitizeCacheDir(f)
return f
}
// ImageExistsInCache if img exist in local cache directory
func ImageExistsInCache(img string) bool {
f := ImagePathInCache(img)
// Check if image exists locally
klog.Infof("Checking for %s in local cache directory", img)
@ -60,7 +66,7 @@ func imageExistsInCache(img string) bool {
return false
}
var checkImageExistsInCache = imageExistsInCache
var checkImageExistsInCache = ImageExistsInCache
// ImageExistsInDaemon if img exist in local docker daemon
func ImageExistsInDaemon(img string) bool {
@ -81,8 +87,7 @@ var checkImageExistsInDaemon = ImageExistsInDaemon
// ImageToCache downloads img (if not present in cache) and writes it to the local cache directory
func ImageToCache(img string) error {
f := filepath.Join(constants.KICCacheDir, path.Base(img)+".tar")
f = localpath.SanitizeCacheDir(f)
f := ImagePathInCache(img)
fileLock := f + ".lock"
releaser, err := lockDownload(fileLock)

View File

@ -39,7 +39,6 @@ import (
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/localpath"
)
const (
@ -104,12 +103,8 @@ func DigestByGoLib(imgName string) string {
return cf.Hex
}
// LoadFromTarball checks if the image exists as a tarball and tries to load it to the local daemon
// TODO: Pass in if we are loading to docker or podman so this function can also be used for podman
func LoadFromTarball(binary, img string) error {
p := filepath.Join(constants.ImageCacheDir, img)
p = localpath.SanitizeCacheDir(p)
// LoadFromTarball loads image from tarball
func LoadFromTarball(binary, img, p string) error {
switch binary {
case driver.Podman:
return fmt.Errorf("not yet implemented, see issue #8426")
@ -124,10 +119,10 @@ func LoadFromTarball(binary, img string) error {
return errors.Wrap(err, "tarball")
}
_, err = daemon.Write(tag, i)
resp, err := daemon.Write(tag, i)
klog.V(2).Infof("response: %s", resp)
return err
}
}
// Tag returns just the image with the tag

View File

@ -145,7 +145,8 @@ func beginDownloadKicBaseImage(g *errgroup.Group, cc *config.ClusterConfig, down
}
}
if err := image.LoadFromTarball(cc.Driver, img); err == nil {
klog.Infof("Loading %s from local cache", img)
if err := image.LoadFromTarball(cc.Driver, img, download.ImagePathInCache(img)); err == nil {
klog.Infof("successfully loaded %s from cached tarball", img)
// strip the digest from the img before saving it in the config
// because loading an image from tarball to daemon doesn't load the digest
@ -154,6 +155,8 @@ func beginDownloadKicBaseImage(g *errgroup.Group, cc *config.ClusterConfig, down
}
if driver.IsDocker(cc.Driver) {
klog.Infof("failed to load %s, will try remote image if available: %v", img, err)
klog.Infof("Downloading %s to local daemon", img)
err = download.ImageToDaemon(img)
if err == nil {