Skip to content

Commit e700ad4

Browse files
committed
kicbase: simplify the image loading logic
This change refactors beginDownloadKicBaseImage() a bit to achieve a more streamlined flow of logic, combining redundant blocks. Signed-off-by: Michael Adam <[email protected]>
1 parent a167d6e commit e700ad4

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

pkg/minikube/node/cache.go

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -154,67 +154,68 @@ func beginDownloadKicBaseImage(g *errgroup.Group, cc *config.ClusterConfig, down
154154
var err error
155155
for _, img := range append([]string{baseImg}, kic.FallbackImages...) {
156156

157-
if driver.IsDocker(cc.Driver) && download.ImageExistsInDaemon(img) && !downloadOnly {
158-
klog.Infof("%s exists in daemon, skipping load", img)
159-
finalImg = img
160-
return nil
161-
}
162-
163-
klog.Infof("Downloading %s to local cache", img)
164-
err = download.ImageToCache(img)
165-
if err == nil {
166-
klog.Infof("successfully saved %s as a tarball", img)
167-
}
168-
if downloadOnly && err == nil {
169-
return nil
170-
}
157+
if driver.IsDocker(cc.Driver) {
158+
if download.ImageExistsInDaemon(img) && !downloadOnly {
159+
klog.Infof("%s exists in daemon, skipping load", img)
160+
finalImg = img
161+
return nil
162+
}
171163

172-
if driver.IsDocker(cc.Driver) && err == nil {
164+
klog.Infof("Downloading %s to local cache", img)
165+
err = download.ImageToCache(img)
166+
if err == nil {
167+
klog.Infof("successfully saved %s as a tarball", img)
168+
if downloadOnly {
169+
return nil
170+
}
171+
}
173172
klog.Infof("Loading %s from local cache", img)
174173
if finalImg, err = download.CacheToDaemon(img); err == nil {
175174
klog.Infof("successfully loaded and using %s from cached tarball", img)
176175
return nil
177176
}
177+
klog.Infof("failed to download %s, will try fallback image if available: %v", img, err)
178178
}
179-
klog.Infof("failed to download %s, will try fallback image if available: %v", img, err)
180-
}
181-
// second if we failed to download any fallback image
182-
// that means probably all registries are blocked by network issues
183-
// we can try to download the image from minikube release page
184-
185-
// if we reach here, that means the user cannot have access to any docker registry
186-
// this means the user is very likely to have a network issue
187-
// downloading from github via http is the last resort, and we should remind the user
188-
// that he should at least get access to github
189-
// print essential warnings
190-
out.WarningT("minikube cannot pull kicbase image from any docker registry, and is trying to download kicbase tarball from github release page via HTTP.")
191-
out.WarningT("It's very likely that you have an internet issue. Please ensure that you can access the internet at least via HTTP, directly or with proxy. Currently your proxy configuration is:")
192-
envs := []string{"HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy", "ALL_PROXY", "NO_PROXY"}
193-
for _, env := range envs {
194-
if v := os.Getenv(env); v != "" {
195-
out.Infof("{{.env}}={{.value}}", out.V{"env": env, "value": v})
179+
// second if we failed to download any fallback image
180+
// that means probably all registries are blocked by network issues
181+
// we can try to download the image from minikube release page
182+
183+
// if we reach here, that means the user cannot have access to any docker registry
184+
// this means the user is very likely to have a network issue
185+
// downloading from github via http is the last resort, and we should remind the user
186+
// that he should at least get access to github
187+
// print essential warnings
188+
out.WarningT("minikube cannot pull kicbase image from any docker registry, and is trying to download kicbase tarball from github release page via HTTP.")
189+
out.WarningT("It's very likely that you have an internet issue. Please ensure that you can access the internet at least via HTTP, directly or with proxy. Currently your proxy configuration is:")
190+
envs := []string{"HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy", "ALL_PROXY", "NO_PROXY"}
191+
for _, env := range envs {
192+
if v := os.Getenv(env); v != "" {
193+
out.Infof("{{.env}}={{.value}}", out.V{"env": env, "value": v})
194+
}
195+
}
196+
out.Ln("")
197+
kicbaseVersion := strings.Split(kic.Version, "-")[0]
198+
_, err = download.GHKicbaseTarballToCache(kicbaseVersion)
199+
if err != nil {
200+
klog.Infof("failed to download kicbase from github")
201+
return fmt.Errorf("failed to download kic base image or any fallback image")
196202
}
197-
}
198-
out.Ln("")
199-
200-
kicbaseVersion := strings.Split(kic.Version, "-")[0]
201-
_, err = download.GHKicbaseTarballToCache(kicbaseVersion)
202-
if err != nil {
203-
klog.Infof("failed to download kicbase from github")
204-
return fmt.Errorf("failed to download kic base image or any fallback image")
205-
}
206203

207-
klog.Infof("successfully downloaded kicbase as fall back image from github")
208-
if !downloadOnly && driver.IsDocker(cc.Driver) {
209-
if finalImg, err = download.CacheToDaemon(fmt.Sprintf("kicbase/stable:%s", kicbaseVersion)); err == nil {
210-
klog.Infof("successfully loaded and using kicbase from tarball on github")
211-
} else {
212-
return fmt.Errorf("failed to load kic base image into docker: %v", err)
204+
klog.Infof("successfully downloaded kicbase as fall back image from github")
205+
if !downloadOnly && driver.IsDocker(cc.Driver) {
206+
if finalImg, err = download.CacheToDaemon(fmt.Sprintf("kicbase/stable:%s", kicbaseVersion)); err == nil {
207+
klog.Infof("successfully loaded and using kicbase from tarball on github")
208+
} else {
209+
return fmt.Errorf("failed to load kic base image into docker: %v", err)
210+
}
213211
}
212+
return nil
213+
214214
}
215215
return nil
216-
217216
})
217+
218+
return
218219
}
219220

220221
// waitDownloadKicBaseImage blocks until the base image for KIC is downloaded.

0 commit comments

Comments
 (0)