mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-09 07:16:02 +02:00
Rework cache key generation a bit. (#375)
* Rework cache key generation a bit. Cache keys are now based on the previous commands, rather than the previous state of the filesystem. * Refactor command interface a bit, only cache the context for commands that use it.
This commit is contained in:
+23
-24
@@ -18,7 +18,6 @@ package executor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -85,23 +84,6 @@ func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage) (*sta
|
||||
}, nil
|
||||
}
|
||||
|
||||
// key will return a string representation of the build at the cmd
|
||||
func (s *stageBuilder) key(cmd string) (string, error) {
|
||||
fsKey, err := s.snapshotter.Key()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
c := bytes.NewBuffer([]byte{})
|
||||
enc := json.NewEncoder(c)
|
||||
enc.Encode(s.cf)
|
||||
cf, err := util.SHA256(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
logrus.Debugf("%s\n%s\n%s\n%s\n", s.baseImageDigest, fsKey, cf, cmd)
|
||||
return util.SHA256(bytes.NewReader([]byte(s.baseImageDigest + fsKey + cf + cmd)))
|
||||
}
|
||||
|
||||
// extractCachedLayer will extract the cached layer and append it to the config file
|
||||
func (s *stageBuilder) extractCachedLayer(layer v1.Image, createdBy string) error {
|
||||
logrus.Infof("Found cached layer, extracting to filesystem")
|
||||
@@ -139,6 +121,15 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error {
|
||||
return err
|
||||
}
|
||||
var volumes []string
|
||||
|
||||
// Set the initial cache key to be the base image digest, the build args and the SrcContext.
|
||||
compositeKey := NewCompositeCache(s.baseImageDigest)
|
||||
contextHash, err := HashDir(opts.SrcContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
compositeKey.AddKey(opts.BuildArgs...)
|
||||
|
||||
args := dockerfile.NewBuildArgs(opts.BuildArgs)
|
||||
for index, cmd := range s.stage.Commands {
|
||||
finalCmd := index == len(s.stage.Commands)-1
|
||||
@@ -149,13 +140,21 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error {
|
||||
if command == nil {
|
||||
continue
|
||||
}
|
||||
logrus.Info(command.String())
|
||||
cacheKey, err := s.key(command.String())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting key")
|
||||
|
||||
// Add the next command to the cache key.
|
||||
compositeKey.AddKey(command.String())
|
||||
if command.UsesContext() {
|
||||
compositeKey.AddKey(contextHash)
|
||||
}
|
||||
logrus.Info(command.String())
|
||||
|
||||
ck, err := compositeKey.Hash()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if command.CacheCommand() && opts.Cache {
|
||||
image, err := cache.RetrieveLayer(opts, cacheKey)
|
||||
image, err := cache.RetrieveLayer(opts, ck)
|
||||
if err == nil {
|
||||
if err := s.extractCachedLayer(image, command.String()); err != nil {
|
||||
return errors.Wrap(err, "extracting cached layer")
|
||||
@@ -222,7 +221,7 @@ func (s *stageBuilder) build(opts *config.KanikoOptions) error {
|
||||
}
|
||||
// Push layer to cache now along with new config file
|
||||
if command.CacheCommand() && opts.Cache {
|
||||
if err := pushLayerToCache(opts, cacheKey, layer, command.String()); err != nil {
|
||||
if err := pushLayerToCache(opts, ck, layer, command.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright 2018 Google LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package executor
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
||||
)
|
||||
|
||||
// NewCompositeCache returns an initialized composite cache object.
|
||||
func NewCompositeCache(initial ...string) *CompositeCache {
|
||||
c := CompositeCache{
|
||||
keys: initial,
|
||||
}
|
||||
return &c
|
||||
}
|
||||
|
||||
// CompositeCache is a type that generates a cache key from a series of keys.
|
||||
type CompositeCache struct {
|
||||
keys []string
|
||||
}
|
||||
|
||||
// AddKey adds the specified key to the sequence.
|
||||
func (s *CompositeCache) AddKey(k ...string) {
|
||||
s.keys = append(s.keys, k...)
|
||||
}
|
||||
|
||||
// Key returns the human readable composite key as a string.
|
||||
func (s *CompositeCache) Key() string {
|
||||
return strings.Join(s.keys, "-")
|
||||
}
|
||||
|
||||
// Hash returns the composite key in a string SHA256 format.
|
||||
func (s *CompositeCache) Hash() (string, error) {
|
||||
return util.SHA256(strings.NewReader(s.Key()))
|
||||
}
|
||||
|
||||
// HashDir returns a hash of the directory.
|
||||
func HashDir(p string) (string, error) {
|
||||
sha := sha256.New()
|
||||
if err := filepath.Walk(p, func(path string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileHash, err := util.CacheHasher()(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := sha.Write([]byte(fileHash)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(sha.Sum(nil)), nil
|
||||
}
|
||||
Reference in New Issue
Block a user