From 9bb16bb383ae0d19acadbefc0d0c2fa22d1b063a Mon Sep 17 00:00:00 2001 From: tripolkaandrey Date: Tue, 23 Mar 2021 11:03:15 +0200 Subject: [PATCH 1/2] introduced specific error type for case when image does not exist --- pkg/minikube/image/cache.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/minikube/image/cache.go b/pkg/minikube/image/cache.go index 8157ab8a27c1..9e688e198603 100644 --- a/pkg/minikube/image/cache.go +++ b/pkg/minikube/image/cache.go @@ -34,6 +34,17 @@ import ( "k8s.io/minikube/pkg/util/lock" ) +type cacheError struct { + Err error +} + +func (f *cacheError) Error() string { + return f.Err.Error() +} + +// errCacheImageDoesntExist is thrown when image that user is trying to add does not exist +var errCacheImageDoesntExist = &cacheError{errors.New("the image you are trying to add does not exist")} + // DeleteFromCacheDir deletes tar files stored in cache dir func DeleteFromCacheDir(images []string) error { for _, image := range images { @@ -116,7 +127,7 @@ func saveToTarFile(iname, rawDest string) error { img, err := retrieveImage(ref) if err != nil { - klog.Warningf("unable to retrieve image: %v", err) + return errCacheImageDoesntExist } if img == nil { return errors.Wrapf(err, "nil image for %s", iname) From b7976f632dcaac8d778e8d32006d90a123c0bee4 Mon Sep 17 00:00:00 2001 From: tripolkaandrey Date: Tue, 23 Mar 2021 11:03:37 +0200 Subject: [PATCH 2/2] warn user instead of error when image does not exist --- pkg/minikube/image/cache.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/minikube/image/cache.go b/pkg/minikube/image/cache.go index 9e688e198603..448dec6ee8e6 100644 --- a/pkg/minikube/image/cache.go +++ b/pkg/minikube/image/cache.go @@ -31,6 +31,7 @@ import ( "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/localpath" + "k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/util/lock" ) @@ -71,8 +72,11 @@ func SaveToDir(images []string, cacheDir string) error { dst := filepath.Join(cacheDir, image) dst = localpath.SanitizeCacheDir(dst) if err := saveToTarFile(image, dst); err != nil { - klog.Errorf("save image to file %q -> %q failed: %v", image, dst, err) - return errors.Wrapf(err, "caching image %q", dst) + if err == errCacheImageDoesntExist { + out.WarningT("The image you are trying to add {{.imageName}} doesn't exist!", out.V{"imageName": image}) + } else { + return errors.Wrapf(err, "caching image %q", dst) + } } klog.Infof("save to tar file %s -> %s succeeded", image, dst) return nil