Skip to content

Commit

Permalink
Merge pull request #879 from cvgw/u/cvgw/update_error_handling_and_lo…
Browse files Browse the repository at this point in the history
…gging_for_cache_retrieve

Update error handling and logging for cache
  • Loading branch information
cvgw authored Nov 23, 2019
2 parents b057776 + a6e458c commit bfd8562
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/creds"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/pkg/errors"
Expand Down Expand Up @@ -124,7 +124,8 @@ func LocalSource(opts *config.CacheOptions, cacheKey string) (v1.Image, error) {

fi, err := os.Stat(path)
if err != nil {
return nil, errors.Wrap(err, "getting file info")
logrus.Debugf("No file found for cache key %v %v", cacheKey, err)
return nil, nil
}

// A stale cache is a bad cache
Expand Down
31 changes: 18 additions & 13 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,13 @@ func RetrieveSourceImage(stage config.KanikoStage, opts *config.KanikoOptions) (
// If so, look in the local cache before trying the remote registry
if opts.CacheDir != "" {
cachedImage, err := cachedImage(opts, currentBaseName)
if cachedImage != nil {
return cachedImage, nil
}

if err != nil {
logrus.Infof("Error while retrieving image from cache: %v", err)
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 All @@ -93,13 +91,23 @@ func tarballImage(index int) (v1.Image, error) {
return tarball.ImageFromPath(tarPath, nil)
}

// Retrieves the manifest for the specified image from the specified registry
func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
logrus.Infof("Downloading base image %s", image)
logrus.Infof("Retrieving image manifest %s", image)
ref, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return nil, err
}

rOpts, err := prepareRemoteRequest(ref, opts)
if err != nil {
return nil, err
}

return remote.Image(ref, rOpts...)
}

func prepareRemoteRequest(ref name.Reference, opts *config.KanikoOptions) ([]remote.Option, error) {
registryName := ref.Context().RegistryStr()
if opts.InsecurePull || opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewRegistry(registryName, name.WeakValidation, name.Insecure)
Expand All @@ -122,8 +130,7 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
InsecureSkipVerify: true,
}
}

return remote.Image(ref, remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain()))
return []remote.Option{remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain())}, nil
}

func cachedImage(opts *config.KanikoOptions, image string) (v1.Image, error) {
Expand All @@ -136,18 +143,16 @@ func cachedImage(opts *config.KanikoOptions, image string) (v1.Image, error) {
if d, ok := ref.(name.Digest); ok {
cacheKey = d.DigestStr()
} else {
img, err := remoteImage(image, opts)
image, err := remoteImage(image, opts)
if err != nil {
return nil, err
}

d, err := img.Digest()
d, err := image.Digest()
if err != nil {
return nil, err
}

cacheKey = d.String()
}

return cache.LocalSource(&opts.CacheOptions, cacheKey)
}

0 comments on commit bfd8562

Please sign in to comment.