Skip to content

Commit

Permalink
image load loads image even if name:tag already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr committed May 12, 2021
1 parent 46f17ea commit 32a91d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ var loadImageCmd = &cobra.Command{
if imgDaemon || imgRemote {
image.UseDaemon(imgDaemon)
image.UseRemote(imgRemote)
if err := machine.CacheAndLoadImages(args, []*config.Profile{profile}); err != nil {
if err := machine.CacheAndLoadImages(args, []*config.Profile{profile}, true); err != nil {
exit.Error(reason.GuestImageLoad, "Failed to load image", err)
}
} else if local {
Expand Down
13 changes: 9 additions & 4 deletions pkg/minikube/image/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func DeleteFromCacheDir(images []string) error {
// The cache directory currently caches images using the imagename_tag
// For example, k8s.gcr.io/kube-addon-manager:v6.5 would be
// stored at $CACHE_DIR/k8s.gcr.io/kube-addon-manager_v6.5
func SaveToDir(images []string, cacheDir string) error {
func SaveToDir(images []string, cacheDir string, force ...bool) error {
var g errgroup.Group
for _, image := range images {
image := image
g.Go(func() error {
dst := filepath.Join(cacheDir, image)
dst = localpath.SanitizeCacheDir(dst)
if err := saveToTarFile(image, dst); err != nil {
if err := saveToTarFile(image, dst, force...); err != nil {
if err == errCacheImageDoesntExist {
out.WarningT("The image you are trying to add {{.imageName}} doesn't exist!", out.V{"imageName": image})
} else {
Expand All @@ -90,7 +90,7 @@ func SaveToDir(images []string, cacheDir string) error {
}

// saveToTarFile caches an image
func saveToTarFile(iname, rawDest string) error {
func saveToTarFile(iname, rawDest string, force ...bool) error {
iname = normalizeTagName(iname)
start := time.Now()
defer func() {
Expand All @@ -112,7 +112,12 @@ func saveToTarFile(iname, rawDest string) error {
}
defer releaser.Release()

if _, err := os.Stat(dst); err == nil {
f := false
if len(force) > 0 {
f = force[0]
}

if _, err := os.Stat(dst); !f && err == nil {
klog.Infof("%s exists", dst)
return nil
}
Expand Down
31 changes: 21 additions & 10 deletions pkg/minikube/machine/cache_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ func CacheImagesForBootstrapper(imageRepository string, version string, clusterB
}

// LoadCachedImages loads previously cached images into the container runtime
func LoadCachedImages(cc *config.ClusterConfig, runner command.Runner, images []string, cacheDir string) error {
func LoadCachedImages(cc *config.ClusterConfig, runner command.Runner, images []string, cacheDir string, force ...bool) error {
cr, err := cruntime.New(cruntime.Config{Type: cc.KubernetesConfig.ContainerRuntime, Runner: runner})
if err != nil {
return errors.Wrap(err, "runtime")
}

f := false
if len(force) > 0 {
f = force[0]
}

// Skip loading images if images already exist
if cr.ImagesPreloaded(images) {
if !f && cr.ImagesPreloaded(images) {
klog.Infof("Images are preloaded, skipping loading")
return nil
}
Expand Down Expand Up @@ -165,7 +170,7 @@ func LoadLocalImages(cc *config.ClusterConfig, runner command.Runner, images []s
for _, image := range images {
image := image
g.Go(func() error {
return transferAndLoadImage(runner, cc.KubernetesConfig, image)
return transferAndLoadImage(runner, cc.KubernetesConfig, image, image)
})
}
if err := g.Wait(); err != nil {
Expand All @@ -176,21 +181,21 @@ func LoadLocalImages(cc *config.ClusterConfig, runner command.Runner, images []s
}

// CacheAndLoadImages caches and loads images to all profiles
func CacheAndLoadImages(images []string, profiles []*config.Profile) error {
func CacheAndLoadImages(images []string, profiles []*config.Profile, force ...bool) error {
if len(images) == 0 {
return nil
}

// This is the most important thing
if err := image.SaveToDir(images, constants.ImageCacheDir); err != nil {
if err := image.SaveToDir(images, constants.ImageCacheDir, force...); err != nil {
return errors.Wrap(err, "save to dir")
}

return DoLoadImages(images, profiles, constants.ImageCacheDir)
return DoLoadImages(images, profiles, constants.ImageCacheDir, force...)
}

// DoLoadImages loads images to all profiles
func DoLoadImages(images []string, profiles []*config.Profile, cacheDir string) error {
func DoLoadImages(images []string, profiles []*config.Profile, cacheDir string, force ...bool) error {
api, err := NewAPIClient()
if err != nil {
return errors.Wrap(err, "api")
Expand Down Expand Up @@ -234,7 +239,7 @@ func DoLoadImages(images []string, profiles []*config.Profile, cacheDir string)
}
if cacheDir != "" {
// loading image names, from cache
err = LoadCachedImages(c, cr, images, cacheDir)
err = LoadCachedImages(c, cr, images, cacheDir, force...)
} else {
// loading image files
err = LoadLocalImages(c, cr, images)
Expand All @@ -259,20 +264,26 @@ func DoLoadImages(images []string, profiles []*config.Profile, cacheDir string)
func transferAndLoadCachedImage(cr command.Runner, k8s config.KubernetesConfig, imgName string, cacheDir string) error {
src := filepath.Join(cacheDir, imgName)
src = localpath.SanitizeCacheDir(src)
return transferAndLoadImage(cr, k8s, src)
return transferAndLoadImage(cr, k8s, src, imgName)
}

// transferAndLoadImage transfers and loads a single image
func transferAndLoadImage(cr command.Runner, k8s config.KubernetesConfig, src string) error {
func transferAndLoadImage(cr command.Runner, k8s config.KubernetesConfig, src string, imgName string) error {
r, err := cruntime.New(cruntime.Config{Type: k8s.ContainerRuntime, Runner: cr})
if err != nil {
return errors.Wrap(err, "runtime")
}

if err := r.RemoveImage(imgName); err != nil {
klog.Warningf("remove image: %v", err)
}

klog.Infof("Loading image from: %s", src)
filename := filepath.Base(src)
if _, err := os.Stat(src); err != nil {
return err
}

dst := path.Join(loadRoot, filename)
f, err := assets.NewFileAsset(src, loadRoot, filename, "0644")
if err != nil {
Expand Down

0 comments on commit 32a91d6

Please sign in to comment.