Remove tarball.WithCompressedCaching flag to resolve OOM Killed error (#1722)

* Remove tarball.WithCompressedCaching flag to resolve OOM Killed error

Large images cannot be build as the kaniko container will be killed due to an OOM error. Removing the tarball compression drastically reduces the memory required to push large image layers. Fixes #1680

This change may increase the build time for smaller images. Therefore a command line option to trigger the compression or a more intelligent behaviour may be useful.

* Add new command line flag to toggle compressed caching

* Add unittest for build with --compressed-caching command line flag set to false
This commit is contained in:
Janosch Maier
2021-10-19 00:14:06 -07:00
committed by GitHub
parent 822f72922a
commit 46e01340c2
6 changed files with 57 additions and 2 deletions
+6 -1
View File
@@ -477,7 +477,12 @@ func (s *stageBuilder) saveSnapshotToLayer(tarPath string) (v1.Layer, error) {
return nil, nil
}
layer, err := tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
var layer v1.Layer
if s.opts.CompressedCaching == true {
layer, err = tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
} else {
layer, err = tarball.LayerFromFile(tarPath)
}
if err != nil {
return nil, err
}
+36
View File
@@ -784,6 +784,42 @@ func Test_stageBuilder_build(t *testing.T) {
rootDir: dir,
}
}(),
func() testcase {
dir, files := tempDirAndFile(t)
file := files[0]
filePath := filepath.Join(dir, file)
ch := NewCompositeCache("", "meow")
ch.AddPath(filePath, util.FileContext{})
hash, err := ch.Hash()
if err != nil {
t.Errorf("couldn't create hash %v", err)
}
command := MockDockerCommand{
command: "meow",
contextFiles: []string{filePath},
cacheCommand: MockCachedDockerCommand{
contextFiles: []string{filePath},
},
}
destDir, err := ioutil.TempDir("", "baz")
if err != nil {
t.Errorf("could not create temp dir %v", err)
}
return testcase{
description: "fake command cache enabled with tar compression disabled and key in cache",
opts: &config.KanikoOptions{Cache: true, CompressedCaching: false},
config: &v1.ConfigFile{Config: v1.Config{WorkingDir: destDir}},
layerCache: &fakeLayerCache{
retrieve: true,
},
expectedCacheKeys: []string{hash},
pushedCacheKeys: []string{},
commands: []commands.DockerCommand{command},
rootDir: dir,
}
}(),
{
description: "fake command cache disabled and key not in cache",
opts: &config.KanikoOptions{Cache: false},
+7 -1
View File
@@ -327,7 +327,13 @@ func writeImageOutputs(image v1.Image, destRefs []name.Tag) error {
// pushLayerToCache pushes layer (tagged with cacheKey) to opts.Cache
// if opts.Cache doesn't exist, infer the cache from the given destination
func pushLayerToCache(opts *config.KanikoOptions, cacheKey string, tarPath string, createdBy string) error {
layer, err := tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
var layer v1.Layer
var err error
if opts.CompressedCaching == true {
layer, err = tarball.LayerFromFile(tarPath, tarball.WithCompressedCaching)
} else {
layer, err = tarball.LayerFromFile(tarPath)
}
if err != nil {
return err
}