Skip to content

Commit 0d3c246

Browse files
authored
Merge pull request #11346 from afbjorklund/kicbase-load
Load kicbase image from right cache and add log
2 parents d122435 + 469d0d5 commit 0d3c246

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

pkg/minikube/download/image.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ var (
4343
}
4444
)
4545

46-
// imageExistsInCache if img exist in local cache directory
47-
func imageExistsInCache(img string) bool {
46+
// imagePathInCache returns path in local cache directory
47+
func imagePathInCache(img string) string {
4848
f := filepath.Join(constants.KICCacheDir, path.Base(img)+".tar")
4949
f = localpath.SanitizeCacheDir(f)
50+
return f
51+
}
52+
53+
// ImageExistsInCache if img exist in local cache directory
54+
func ImageExistsInCache(img string) bool {
55+
f := imagePathInCache(img)
5056

5157
// Check if image exists locally
5258
klog.Infof("Checking for %s in local cache directory", img)
@@ -60,7 +66,7 @@ func imageExistsInCache(img string) bool {
6066
return false
6167
}
6268

63-
var checkImageExistsInCache = imageExistsInCache
69+
var checkImageExistsInCache = ImageExistsInCache
6470

6571
// ImageExistsInDaemon if img exist in local docker daemon
6672
func ImageExistsInDaemon(img string) bool {
@@ -81,8 +87,7 @@ var checkImageExistsInDaemon = ImageExistsInDaemon
8187

8288
// ImageToCache downloads img (if not present in cache) and writes it to the local cache directory
8389
func ImageToCache(img string) error {
84-
f := filepath.Join(constants.KICCacheDir, path.Base(img)+".tar")
85-
f = localpath.SanitizeCacheDir(f)
90+
f := imagePathInCache(img)
8691
fileLock := f + ".lock"
8792

8893
releaser, err := lockDownload(fileLock)
@@ -163,6 +168,26 @@ func ImageToCache(img string) error {
163168
}
164169
}
165170

171+
// CacheToDaemon loads image from tarball in the local cache directory to the local docker daemon
172+
func CacheToDaemon(img string) error {
173+
p := imagePathInCache(img)
174+
175+
ref, err := name.NewDigest(img)
176+
if err != nil {
177+
return errors.Wrap(err, "new ref")
178+
}
179+
180+
tag := ref.Tag()
181+
i, err := tarball.ImageFromPath(p, &tag)
182+
if err != nil {
183+
return errors.Wrap(err, "tarball")
184+
}
185+
186+
resp, err := daemon.Write(ref, i)
187+
klog.V(2).Infof("response: %s", resp)
188+
return err
189+
}
190+
166191
// ImageToDaemon downloads img (if not present in daemon) and writes it to the local docker daemon
167192
func ImageToDaemon(img string) error {
168193
fileLock := filepath.Join(constants.KICCacheDir, path.Base(img)+".d.lock")

pkg/minikube/image/image.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ import (
3333
"github.com/google/go-containerregistry/pkg/v1/daemon"
3434
"github.com/google/go-containerregistry/pkg/v1/mutate"
3535
"github.com/google/go-containerregistry/pkg/v1/remote"
36-
"github.com/google/go-containerregistry/pkg/v1/tarball"
3736

3837
"github.com/pkg/errors"
3938
"k8s.io/klog/v2"
4039
"k8s.io/minikube/pkg/minikube/constants"
41-
"k8s.io/minikube/pkg/minikube/driver"
42-
"k8s.io/minikube/pkg/minikube/localpath"
4340
)
4441

4542
const (
@@ -104,32 +101,6 @@ func DigestByGoLib(imgName string) string {
104101
return cf.Hex
105102
}
106103

107-
// LoadFromTarball checks if the image exists as a tarball and tries to load it to the local daemon
108-
// TODO: Pass in if we are loading to docker or podman so this function can also be used for podman
109-
func LoadFromTarball(binary, img string) error {
110-
p := filepath.Join(constants.ImageCacheDir, img)
111-
p = localpath.SanitizeCacheDir(p)
112-
113-
switch binary {
114-
case driver.Podman:
115-
return fmt.Errorf("not yet implemented, see issue #8426")
116-
default:
117-
tag, err := name.NewTag(Tag(img))
118-
if err != nil {
119-
return errors.Wrap(err, "new tag")
120-
}
121-
122-
i, err := tarball.ImageFromPath(p, &tag)
123-
if err != nil {
124-
return errors.Wrap(err, "tarball")
125-
}
126-
127-
_, err = daemon.Write(tag, i)
128-
return err
129-
}
130-
131-
}
132-
133104
// Tag returns just the image with the tag
134105
// eg image:tag@sha256:digest -> image:tag if there is an associated tag
135106
// if not possible, just return the initial img

pkg/minikube/node/cache.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,22 @@ func beginDownloadKicBaseImage(g *errgroup.Group, cc *config.ClusterConfig, down
149149
}
150150
}
151151

152-
if err := image.LoadFromTarball(cc.Driver, img); err == nil {
153-
klog.Infof("successfully loaded %s from cached tarball", img)
154-
// strip the digest from the img before saving it in the config
155-
// because loading an image from tarball to daemon doesn't load the digest
156-
finalImg = img
157-
return nil
152+
if cc.Driver == driver.Podman {
153+
return fmt.Errorf("not yet implemented, see issue #8426")
154+
}
155+
if driver.IsDocker(cc.Driver) {
156+
klog.Infof("Loading %s from local cache", img)
157+
err = download.CacheToDaemon(img)
158+
if err == nil {
159+
klog.Infof("successfully loaded %s from cached tarball", img)
160+
finalImg = img
161+
return nil
162+
}
158163
}
159164

160165
if driver.IsDocker(cc.Driver) {
166+
klog.Infof("failed to load %s, will try remote image if available: %v", img, err)
167+
161168
klog.Infof("Downloading %s to local daemon", img)
162169
err = download.ImageToDaemon(img)
163170
if err == nil {

0 commit comments

Comments
 (0)