Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update error handling and logging for cache #879

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}