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

Fix warmer memory leak. #2763

Merged
merged 3 commits into from
Nov 29, 2023
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
74 changes: 40 additions & 34 deletions pkg/cache/warm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package cache

import (
"bytes"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -58,35 +57,11 @@ func WarmCache(opts *config.WarmerOptions) error {

errs := 0
for _, img := range images {
tarBuf := new(bytes.Buffer)
manifestBuf := new(bytes.Buffer)

cw := &Warmer{
Remote: remote.RetrieveRemoteImage,
Local: LocalSource,
TarWriter: tarBuf,
ManifestWriter: manifestBuf,
}

digest, err := cw.Warm(img, opts)
err := warmToFile(cacheDir, img, opts)
if err != nil {
if !IsAlreadyCached(err) {
logrus.Warnf("Error while trying to warm image: %v %v", img, err)
errs++
}

continue
}

cachePath := path.Join(cacheDir, digest.String())

if err := writeBufsToFile(cachePath, tarBuf, manifestBuf); err != nil {
logrus.Warnf("Error while writing %v to cache: %v", img, err)
logrus.Warnf("Error while trying to warm image: %v %v", img, err)
errs++
continue
}

logrus.Debugf("Wrote %s to cache", img)
}

if len(images) == errs {
Expand All @@ -96,22 +71,53 @@ func WarmCache(opts *config.WarmerOptions) error {
return nil
}

func writeBufsToFile(cachePath string, tarBuf, manifestBuf *bytes.Buffer) error {
f, err := os.Create(cachePath)
// Download image in temporary files then move files to final destination
func warmToFile(cacheDir, img string, opts *config.WarmerOptions) error {
f, err := os.CreateTemp(cacheDir, "warmingImage.*")
if err != nil {
return err
}
// defer called in reverse order
defer os.Remove(f.Name())
defer f.Close()

if _, err := f.Write(tarBuf.Bytes()); err != nil {
return errors.Wrap(err, "Failed to save tar to file")
mtfsFile, err := os.CreateTemp(cacheDir, "warmingManifest.*")
if err != nil {
return err
}
defer os.Remove(mtfsFile.Name())
defer mtfsFile.Close()

cw := &Warmer{
Remote: remote.RetrieveRemoteImage,
Local: LocalSource,
TarWriter: f,
ManifestWriter: mtfsFile,
}

mfstPath := cachePath + ".json"
if err := os.WriteFile(mfstPath, manifestBuf.Bytes(), 0666); err != nil {
return errors.Wrap(err, "Failed to save manifest to file")
digest, err := cw.Warm(img, opts)
if err != nil {
if !IsAlreadyCached(err) {
logrus.Warnf("Error while trying to warm image: %v %v", img, err)
}

return err
}

finalCachePath := path.Join(cacheDir, digest.String())
finalMfstPath := finalCachePath + ".json"

err = os.Rename(f.Name(), finalCachePath)
if err != nil {
return err
}

err = os.Rename(mtfsFile.Name(), finalMfstPath)
if err != nil {
return errors.Wrap(err, "Failed to rename manifest file")
}

logrus.Debugf("Wrote %s to cache", img)
return nil
}

Expand Down
31 changes: 31 additions & 0 deletions scripts/boxed_warm_in_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# 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.

# Test the warmer in boxed memory conditions.
# Attempt to run the warmer inside a container limited to 16MB of RAM. Use gcr.io/kaniko-project/warmer:latest image."
# Example: ./boxed_warm_in_docker.sh --image debian:trixie-slim
#
set -e

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to this script explaining the purpose (eg: test warmer in boxed memory conditions) and an example of how to use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Added a documentation. I also fixed the swallowed error.

rc=0
docker run \
--memory=16m --memory-swappiness=0 \
gcr.io/kaniko-project/warmer:latest \
"$@" || rc=$?

>&2 echo "RC=$rc"
exit $rc

Loading