Make the Digest calculation faster for locally-cached images. (#534)

Right now when we find a v1.Tarball in the local disk cache, we
recompute the digest. This is very expensive and redundant, because
we store tarballs by their digest and use that as a key to look them up.
This commit is contained in:
dlorenc
2019-01-22 13:28:21 -06:00
committed by GitHub
parent b0e7c0e8cd
commit fcd1976d3b
4 changed files with 64 additions and 37 deletions
+24 -3
View File
@@ -20,6 +20,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"time"
"github.com/GoogleContainerTools/kaniko/pkg/config"
@@ -114,11 +115,31 @@ func LocalSource(opts *config.KanikoOptions, cacheKey string) (v1.Image, error)
return nil, nil
}
imgTar, err := tarball.ImageFromPath(path, nil)
logrus.Infof("Found %s in local cache", cacheKey)
return cachedImageFromPath(path)
}
// cachedImage represents a v1.Tarball that is cached locally in a CAS.
// Computing the digest for a v1.Tarball is very expensive. If the tarball
// is named with the digest we can store this and return it directly rather
// than recompute it.
type cachedImage struct {
digest string
v1.Image
}
func (c *cachedImage) Digest() (v1.Hash, error) {
return v1.NewHash(c.digest)
}
func cachedImageFromPath(p string) (v1.Image, error) {
imgTar, err := tarball.ImageFromPath(p, nil)
if err != nil {
return nil, errors.Wrap(err, "getting image from path")
}
logrus.Infof("Found %s in local cache", cacheKey)
return imgTar, nil
return &cachedImage{
digest: filepath.Base(p),
Image: imgTar,
}, nil
}