Fix #926 cache warmer and method signature
This commit is contained in:
parent
48f66e958c
commit
b1b0513c05
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue