Fix #926 cache warmer and method signature

This commit is contained in:
Cole Wippern 2019-12-20 15:36:10 -08:00
parent 48f66e958c
commit b1b0513c05
3 changed files with 50 additions and 7 deletions

44
pkg/cache/cache.go vendored
View File

@ -35,6 +35,40 @@ import (
"github.com/sirupsen/logrus" "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 // LayerCache is the layer cache
type LayerCache interface { type LayerCache interface {
RetrieveLayer(string) (v1.Image, error) RetrieveLayer(string) (v1.Image, error)
@ -124,15 +158,17 @@ func LocalSource(opts *config.CacheOptions, cacheKey string) (v1.Image, error) {
fi, err := os.Stat(path) fi, err := os.Stat(path)
if err != nil { if err != nil {
logrus.Debugf("No file found for cache key %v %v", cacheKey, err) msg := fmt.Sprintf("No file found for cache key %v %v", cacheKey, err)
return nil, nil logrus.Debug(msg)
return nil, NotFoundErr{msg: msg}
} }
// A stale cache is a bad cache // A stale cache is a bad cache
expiry := fi.ModTime().Add(opts.CacheTTL) expiry := fi.ModTime().Add(opts.CacheTTL)
if expiry.Before(time.Now()) { if expiry.Before(time.Now()) {
logrus.Debugf("Cached image is too old: %v", fi.ModTime()) msg := fmt.Sprintf("Cached image is too old: %v", fi.ModTime())
return nil, nil logrus.Debug(msg)
return nil, ExpiredErr{msg: msg}
} }
logrus.Infof("Found %s in local cache", cacheKey) logrus.Infof("Found %s in local cache", cacheKey)

2
pkg/cache/warm.go vendored
View File

@ -54,7 +54,7 @@ func WarmCache(opts *config.WarmerOptions) error {
if !opts.Force { if !opts.Force {
_, err := LocalSource(&opts.CacheOptions, digest.String()) _, err := LocalSource(&opts.CacheOptions, digest.String())
if err == nil { if err == nil || IsExpired(err) {
continue continue
} }
} }

View File

@ -75,12 +75,19 @@ func RetrieveSourceImage(stage config.KanikoStage, opts *config.KanikoOptions) (
if opts.CacheDir != "" { if opts.CacheDir != "" {
cachedImage, err := cachedImage(opts, currentBaseName) cachedImage, err := cachedImage(opts, currentBaseName)
if err != nil { 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 { } else if cachedImage != nil {
return cachedImage, nil return cachedImage, nil
} }
} }
logrus.Infof("Image %v not found in cache", currentBaseName)
// Otherwise, initialize image as usual // Otherwise, initialize image as usual
return RetrieveRemoteImage(currentBaseName, opts) return RetrieveRemoteImage(currentBaseName, opts)
} }