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: New cleanup when using --cleanup flag (#1406) #3130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -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...")
Expand Down
Loading