From b5ea487061330a91a43341fa0cbca7e0aa24d750 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Wed, 31 Mar 2021 22:39:12 +0530 Subject: [PATCH] fix: create parent directories before writing digest files --- pkg/executor/push.go | 18 +++++++++++++++--- pkg/executor/push_test.go | 23 ++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/pkg/executor/push.go b/pkg/executor/push.go index 9b5f3bfe3f..63c4548742 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -164,6 +164,18 @@ func getDigest(image v1.Image) ([]byte, error) { return []byte(digest.String()), nil } +func writeDigestFile(path string, digestByteArray []byte) error { + parentDir := filepath.Dir(path) + if _, err := os.Stat(parentDir); os.IsNotExist(err) { + if err := os.MkdirAll(parentDir, 0700); err != nil { + logrus.Debugf("error creating %s, %s", parentDir, err) + return err + } + logrus.Tracef("Created directory %v", parentDir) + } + return ioutil.WriteFile(path, digestByteArray, 0644) +} + // DoPush is responsible for pushing image to the destinations specified in opts func DoPush(image v1.Image, opts *config.KanikoOptions) error { t := timing.Start("Total Push Time") @@ -178,7 +190,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error { } if opts.DigestFile != "" { - err := ioutil.WriteFile(opts.DigestFile, digestByteArray, 0644) + err := writeDigestFile(opts.DigestFile, digestByteArray) if err != nil { return errors.Wrap(err, "writing digest to file failed") } @@ -213,14 +225,14 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error { } if opts.ImageNameDigestFile != "" { - err := ioutil.WriteFile(opts.ImageNameDigestFile, []byte(builder.String()), 0644) + err := writeDigestFile(opts.ImageNameDigestFile, []byte(builder.String())) if err != nil { return errors.Wrap(err, "writing image name with digest to file failed") } } if opts.ImageNameTagDigestFile != "" { - err := ioutil.WriteFile(opts.ImageNameTagDigestFile, []byte(builder.String()), 0644) + err := writeDigestFile(opts.ImageNameTagDigestFile, []byte(builder.String())) if err != nil { return errors.Wrap(err, "writing image name with image tag and digest to file failed") } diff --git a/pkg/executor/push_test.go b/pkg/executor/push_test.go index 5a274df064..940c870a61 100644 --- a/pkg/executor/push_test.go +++ b/pkg/executor/push_test.go @@ -33,7 +33,6 @@ import ( "github.com/google/go-containerregistry/pkg/v1/layout" "github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/validate" - "github.com/spf13/afero" ) @@ -453,3 +452,25 @@ func TestHelperProcess(t *testing.T) { fmt.Fprintf(os.Stdout, "fake result") os.Exit(0) } + +func TestWriteDigestFile(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "*") + if err != nil { + t.Fatalf("could not create temp dir: %s", err) + } + defer os.RemoveAll(tmpDir) + + t.Run("parent directory does not exist", func(t *testing.T) { + err := writeDigestFile(tmpDir+"/test/df", []byte("test")) + if err != nil { + t.Errorf("expected file to be written successfully, but got error: %v", err) + } + }) + + t.Run("parent directory exists", func(t *testing.T) { + err := writeDigestFile(tmpDir+"/df", []byte("test")) + if err != nil { + t.Errorf("expected file to be written successfully, but got error: %v", err) + } + }) +}