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

create parent directory before writing digest files #1612

Merged
merged 2 commits into from
Apr 13, 2021
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
18 changes: 15 additions & 3 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/executor/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
})
}