fix: create parent directories before writing digest files (#1612)
This commit is contained in:
parent
d40a51f38f
commit
6028f1c5ec
|
|
@ -164,6 +164,18 @@ func getDigest(image v1.Image) ([]byte, error) {
|
||||||
return []byte(digest.String()), nil
|
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
|
// DoPush is responsible for pushing image to the destinations specified in opts
|
||||||
func DoPush(image v1.Image, opts *config.KanikoOptions) error {
|
func DoPush(image v1.Image, opts *config.KanikoOptions) error {
|
||||||
t := timing.Start("Total Push Time")
|
t := timing.Start("Total Push Time")
|
||||||
|
|
@ -178,7 +190,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.DigestFile != "" {
|
if opts.DigestFile != "" {
|
||||||
err := ioutil.WriteFile(opts.DigestFile, digestByteArray, 0644)
|
err := writeDigestFile(opts.DigestFile, digestByteArray)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "writing digest to file failed")
|
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 != "" {
|
if opts.ImageNameDigestFile != "" {
|
||||||
err := ioutil.WriteFile(opts.ImageNameDigestFile, []byte(builder.String()), 0644)
|
err := writeDigestFile(opts.ImageNameDigestFile, []byte(builder.String()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "writing image name with digest to file failed")
|
return errors.Wrap(err, "writing image name with digest to file failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.ImageNameTagDigestFile != "" {
|
if opts.ImageNameTagDigestFile != "" {
|
||||||
err := ioutil.WriteFile(opts.ImageNameTagDigestFile, []byte(builder.String()), 0644)
|
err := writeDigestFile(opts.ImageNameTagDigestFile, []byte(builder.String()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "writing image name with image tag and digest to file failed")
|
return errors.Wrap(err, "writing image name with image tag and digest to file failed")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ import (
|
||||||
"github.com/google/go-containerregistry/pkg/v1/layout"
|
"github.com/google/go-containerregistry/pkg/v1/layout"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/random"
|
"github.com/google/go-containerregistry/pkg/v1/random"
|
||||||
"github.com/google/go-containerregistry/pkg/v1/validate"
|
"github.com/google/go-containerregistry/pkg/v1/validate"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -453,3 +452,25 @@ func TestHelperProcess(t *testing.T) {
|
||||||
fmt.Fprintf(os.Stdout, "fake result")
|
fmt.Fprintf(os.Stdout, "fake result")
|
||||||
os.Exit(0)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue