From b1b0513c05c6584bca0bcfc5774cb01c05a50b8b Mon Sep 17 00:00:00 2001 From: Cole Wippern Date: Fri, 20 Dec 2019 15:36:10 -0800 Subject: [PATCH] Fix #926 cache warmer and method signature --- pkg/cache/cache.go | 44 ++++++++++++++++++++++++++++++++++++++---- pkg/cache/warm.go | 2 +- pkg/util/image_util.go | 11 +++++++++-- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 1f6723535..0f3aa51f3 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -35,6 +35,40 @@ import ( "github.com/sirupsen/logrus" ) +type NotFoundErr struct { + msg string +} + +func (e NotFoundErr) Error() string { + return e.msg +} + +type ExpiredErr struct { + msg string +} + +func (e ExpiredErr) Error() string { + return e.msg +} + +func IsNotFound(e error) bool { + switch e.(type) { + case NotFoundErr: + return true + } + + return false +} + +func IsExpired(e error) bool { + switch e.(type) { + case ExpiredErr: + return true + } + + return false +} + // LayerCache is the layer cache type LayerCache interface { RetrieveLayer(string) (v1.Image, error) @@ -124,15 +158,17 @@ func LocalSource(opts *config.CacheOptions, cacheKey string) (v1.Image, error) { fi, err := os.Stat(path) if err != nil { - logrus.Debugf("No file found for cache key %v %v", cacheKey, err) - return nil, nil + msg := fmt.Sprintf("No file found for cache key %v %v", cacheKey, err) + logrus.Debug(msg) + return nil, NotFoundErr{msg: msg} } // A stale cache is a bad cache expiry := fi.ModTime().Add(opts.CacheTTL) if expiry.Before(time.Now()) { - logrus.Debugf("Cached image is too old: %v", fi.ModTime()) - return nil, nil + msg := fmt.Sprintf("Cached image is too old: %v", fi.ModTime()) + logrus.Debug(msg) + return nil, ExpiredErr{msg: msg} } logrus.Infof("Found %s in local cache", cacheKey) diff --git a/pkg/cache/warm.go b/pkg/cache/warm.go index abdaec76d..254065ea0 100644 --- a/pkg/cache/warm.go +++ b/pkg/cache/warm.go @@ -54,7 +54,7 @@ func WarmCache(opts *config.WarmerOptions) error { if !opts.Force { _, err := LocalSource(&opts.CacheOptions, digest.String()) - if err == nil { + if err == nil || IsExpired(err) { continue } } diff --git a/pkg/util/image_util.go b/pkg/util/image_util.go index 6e7cc5239..4b74c7d38 100644 --- a/pkg/util/image_util.go +++ b/pkg/util/image_util.go @@ -75,12 +75,19 @@ func RetrieveSourceImage(stage config.KanikoStage, opts *config.KanikoOptions) ( if opts.CacheDir != "" { cachedImage, err := cachedImage(opts, currentBaseName) if err != nil { - logrus.Errorf("Error while retrieving image from cache: %v %v", currentBaseName, err) + switch { + case cache.IsNotFound(err): + logrus.Debugf("Image %v not found in cache", currentBaseName) + case cache.IsExpired(err): + logrus.Debugf("Image %v found in cache but was expired", currentBaseName) + default: + logrus.Errorf("Error while retrieving image from cache: %v %v", currentBaseName, err) + } } else if cachedImage != nil { return cachedImage, nil } } - logrus.Infof("Image %v not found in cache", currentBaseName) + // Otherwise, initialize image as usual return RetrieveRemoteImage(currentBaseName, opts) }