From e61dbc46b293a0ec14978ab55af36951b9ec9bd0 Mon Sep 17 00:00:00 2001 From: Martin Zihlmann Date: Sun, 1 Jun 2025 20:58:28 +0100 Subject: [PATCH] move restoreFilesystem to subroutine --- pkg/executor/build.go | 45 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 0a3c3a87e..638acbcb5 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -695,6 +695,23 @@ func CalculateDependencies(stages []config.KanikoStage, opts *config.KanikoOptio return depGraph, nil } +func restoreFilesystem(tarball string, opts *config.KanikoOptions) error { + if err := util.DeleteFilesystem(); err != nil { + return err + } + if opts.PreserveContext { + if tarball == "" { + return fmt.Errorf("context snapshot is missing") + } + _, err := util.UnpackLocalTarArchive(tarball, config.RootDir) + if err != nil { + return errors.Wrap(err, "failed to unpack context snapshot") + } + logrus.Info("Context restored") + } + return nil +} + // DoBuild executes building the Dockerfile func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { t := timing.Start("Total Build Time") @@ -812,19 +829,10 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { } } if opts.Cleanup { - if err = util.DeleteFilesystem(); err != nil { + err = restoreFilesystem(tarball, opts) + if err != nil { return nil, err } - if opts.PreserveContext { - if tarball == "" { - return nil, fmt.Errorf("context snapshot is missing") - } - _, err := util.UnpackLocalTarArchive(tarball, config.RootDir) - if err != nil { - return nil, errors.Wrap(err, "failed to unpack context snapshot") - } - logrus.Info("Context restored") - } } timing.DefaultRun.Stop(t) return sourceImage, nil @@ -854,18 +862,9 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { } // Delete the filesystem - if err := util.DeleteFilesystem(); err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("deleting file system after stage %d", index)) - } - if opts.PreserveContext { - if tarball == "" { - return nil, fmt.Errorf("context snapshot is missing") - } - _, err := util.UnpackLocalTarArchive(tarball, config.RootDir) - if err != nil { - return nil, errors.Wrap(err, "failed to unpack context snapshot") - } - logrus.Info("Context restored") + err = restoreFilesystem(tarball, opts) + if err != nil { + return nil, err } }