fix: New cleanup when using --cleanup flag (#1406)

This commit is contained in:
dadurex 2023-10-09 18:51:35 +02:00 committed by Matt Benson
parent d070187005
commit a0615c88aa
2 changed files with 26 additions and 1 deletions

View File

@ -787,7 +787,7 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
}
}
if opts.Cleanup {
if err = util.DeleteFilesystem(); err != nil {
if err = util.CleanupFilesystem(); err != nil {
return nil, err
}
}

View File

@ -26,6 +26,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
@ -228,6 +229,30 @@ func GetFSFromLayers(root string, layers []v1.Layer, opts ...FSOpt) ([]string, e
return extractedFiles, nil
}
// CleanupFilesystem deletes the extracted image file system, snapshots and stage dependent files
func CleanupFilesystem() error {
DeleteFilesystem()
logrus.Info("Deleting snapshots and layer dependet files...")
numericPattern := regexp.MustCompile(`^\d+$`)
return filepath.Walk(config.KanikoDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
// ignore errors when deleting.
return nil //nolint:nilerr
}
if numericPattern.MatchString(info.Name()) && isExist(path) {
err := os.RemoveAll(path)
if err != nil {
logrus.Debugf("Error deleting path: %s\n", path)
return nil //nolint:nilerr
}
logrus.Debugf("Deleted path: %s\n", path)
}
return nil //nolint:nilerr
})
}
// DeleteFilesystem deletes the extracted image file system
func DeleteFilesystem() error {
logrus.Info("Deleting filesystem...")