Skip to content

Commit

Permalink
Fix #926 cache warmer and method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
cvgw committed Jan 10, 2020
1 parent 48f66e9 commit b1b0513
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
44 changes: 40 additions & 4 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/warm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b1b0513

Please sign in to comment.