From 4c9a800e1218ef57fad68e6ab84bc71f6e09d181 Mon Sep 17 00:00:00 2001 From: Will Ripley Date: Mon, 21 Oct 2019 11:55:55 -0500 Subject: [PATCH 1/5] Added image digest file path --- cmd/executor/cmd/root.go | 5 +++++ pkg/config/options.go | 1 + pkg/executor/push.go | 26 ++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 199f3fa7a9..ca3a43ba5e 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -70,6 +70,9 @@ var RootCmd = &cobra.Command{ if err := resolveDockerfilePath(); err != nil { return errors.Wrap(err, "error resolving dockerfile path") } + if len(opts.Destinations) == 0 && opts.ImageNameDigestFile != "" { + return errors.New("You must provide --destination if setting ImageNameDigestFile") + } } return nil }, @@ -134,6 +137,7 @@ func addKanikoOptionsFlags(cmd *cobra.Command) { RootCmd.PersistentFlags().StringVarP(&opts.CacheRepo, "cache-repo", "", "", "Specify a repository to use as a cache, otherwise one will be inferred from the destination provided") RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "", "/cache", "Specify a local directory to use as a cache.") RootCmd.PersistentFlags().StringVarP(&opts.DigestFile, "digest-file", "", "", "Specify a file to save the digest of the built image to.") + RootCmd.PersistentFlags().StringVarP(&opts.ImageNameDigestFile, "image-name-with-digest-file", "", "", "Specify a file to save the image name w/ digest of the built image to.") RootCmd.PersistentFlags().StringVarP(&opts.OCILayoutPath, "oci-layout-path", "", "", "Path to save the OCI image layout of the built image.") RootCmd.PersistentFlags().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image") RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end") @@ -240,6 +244,7 @@ func resolveRelativePaths() error { &opts.CacheDir, &opts.TarPath, &opts.DigestFile, + &opts.ImageNameDigestFile, } for _, p := range optsPaths { diff --git a/pkg/config/options.go b/pkg/config/options.go index 44af681ecd..268c6390f0 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -37,6 +37,7 @@ type KanikoOptions struct { Target string CacheRepo string DigestFile string + ImageNameDigestFile string OCILayoutPath string Destinations multiArg BuildArgs multiArg diff --git a/pkg/executor/push.go b/pkg/executor/push.go index 565ac52a47..772febcb39 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -96,16 +96,27 @@ func CheckPushPermissions(opts *config.KanikoOptions) error { return nil } +func getDigest(image v1.Image) ([]byte, error){ + digest, err := image.Digest() + if err != nil { + return nil, err + } + return []byte(digest.String()), nil +} + // 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") if opts.DigestFile != "" { - digest, err := image.Digest() + // digest, err := image.Digest() + // if err != nil { + // return errors.Wrap(err, "error fetching digest") + // } + digestByteArray, err := getDigest(image) if err != nil { return errors.Wrap(err, "error fetching digest") } - digestByteArray := []byte(digest.String()) err = ioutil.WriteFile(opts.DigestFile, digestByteArray, 0644) if err != nil { return errors.Wrap(err, "writing digest to file failed") @@ -128,6 +139,17 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error { if err != nil { return errors.Wrap(err, "getting tag for destination") } + if opts.ImageNameDigestFile != "" { + digestByteArray, err := getDigest(image) + if err != nil { + return errors.Wrap(err, "error fetching digest") + } + imageName := []byte(destination + "@") + err = ioutil.WriteFile(opts.ImageNameDigestFile, append(imageName, digestByteArray...), 0644) + if err != nil { + return errors.Wrap(err, "writing digest to file failed") + } + } destRefs = append(destRefs, destRef) } From f38c47daa0e50cff6e30cf26cbaddd2f59a6ff3d Mon Sep 17 00:00:00 2001 From: Will Ripley Date: Tue, 29 Oct 2019 10:55:49 -0500 Subject: [PATCH 2/5] Made changes to --image-name-with-digest-file to support multiple destinations --- pkg/executor/push.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/executor/push.go b/pkg/executor/push.go index 772febcb39..bf3158a06c 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -107,17 +107,18 @@ func getDigest(image v1.Image) ([]byte, error){ // 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") - - if opts.DigestFile != "" { - // digest, err := image.Digest() - // if err != nil { - // return errors.Wrap(err, "error fetching digest") - // } - digestByteArray, err := getDigest(image) + var digestByteArray []byte + var builder strings.Builder + if opts.DigestFile != "" || opts.ImageNameDigestFile != "" { + var err error + digestByteArray, err = getDigest(image) if err != nil { return errors.Wrap(err, "error fetching digest") } - err = ioutil.WriteFile(opts.DigestFile, digestByteArray, 0644) + } + + if opts.DigestFile != "" { + err := ioutil.WriteFile(opts.DigestFile, digestByteArray, 0644) if err != nil { return errors.Wrap(err, "writing digest to file failed") } @@ -140,19 +141,20 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error { return errors.Wrap(err, "getting tag for destination") } if opts.ImageNameDigestFile != "" { - digestByteArray, err := getDigest(image) - if err != nil { - return errors.Wrap(err, "error fetching digest") - } imageName := []byte(destination + "@") - err = ioutil.WriteFile(opts.ImageNameDigestFile, append(imageName, digestByteArray...), 0644) - if err != nil { - return errors.Wrap(err, "writing digest to file failed") - } + builder.Write(append(imageName, digestByteArray...)) + builder.WriteString("\n") } destRefs = append(destRefs, destRef) } + if opts.ImageNameDigestFile != "" { + err := ioutil.WriteFile(opts.ImageNameDigestFile, []byte(builder.String()), 0644) + if err != nil { + return errors.Wrap(err, "writing digest to file failed") + } + } + if opts.TarPath != "" { tagToImage := map[name.Tag]v1.Image{} for _, destRef := range destRefs { From c2faa4e5e677358a1ce289e3159bf3a5ca6cc87a Mon Sep 17 00:00:00 2001 From: Will Ripley Date: Tue, 29 Oct 2019 11:04:33 -0500 Subject: [PATCH 3/5] Fixed spacing issue in config --- pkg/config/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/options.go b/pkg/config/options.go index 268c6390f0..ee5206ef2a 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -37,7 +37,7 @@ type KanikoOptions struct { Target string CacheRepo string DigestFile string - ImageNameDigestFile string + ImageNameDigestFile string OCILayoutPath string Destinations multiArg BuildArgs multiArg From a0651436b52ae4b913d6b7582d36e8a402e5b581 Mon Sep 17 00:00:00 2001 From: Will Ripley Date: Tue, 29 Oct 2019 12:55:50 -0500 Subject: [PATCH 4/5] Changed image name to be full image name minus tag. Also added test for new ImageNameDigestFile flag --- pkg/executor/push.go | 2 +- pkg/executor/push_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/executor/push.go b/pkg/executor/push.go index bf3158a06c..bfdacaa039 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -141,7 +141,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error { return errors.Wrap(err, "getting tag for destination") } if opts.ImageNameDigestFile != "" { - imageName := []byte(destination + "@") + imageName := []byte(destRef.Repository.Name() + "@") builder.Write(append(imageName, digestByteArray...)) builder.WriteString("\n") } diff --git a/pkg/executor/push_test.go b/pkg/executor/push_test.go index f771e400ed..6bc5dcbef0 100644 --- a/pkg/executor/push_test.go +++ b/pkg/executor/push_test.go @@ -192,3 +192,34 @@ func TestOCILayoutPath(t *testing.T) { got, err := layoutImage.Manifest() testutil.CheckErrorAndDeepEqual(t, false, err, want, got) } + +func TestImageNameDigestFile(t *testing.T) { + image, err := random.Image(1024, 4) + if err != nil { + t.Fatalf("could not create image: %s", err) + } + + digest, err := image.Digest() + if err != nil { + t.Fatalf("could not get image digest: %s", err) + } + + opts := config.KanikoOptions{ + NoPush: true, + Destinations: []string{"gcr.io/foo/bar:latest", "bob/image"}, + ImageNameDigestFile: "tmpFile", + } + + defer os.Remove("tmpFile") + + if err := DoPush(image, &opts); err != nil { + t.Fatalf("could not push image: %s", err) + } + + want := []byte("gcr.io/foo/bar@" + digest.String() + "\nindex.docker.io/bob/image@" + digest.String() +"\n") + + got, err := ioutil.ReadFile("tmpFile") + + testutil.CheckErrorAndDeepEqual(t, false, err, want, got) + +} From c8f089fdbbf3445ec2752e00eefc803214487dfa Mon Sep 17 00:00:00 2001 From: Will Ripley Date: Tue, 29 Oct 2019 13:09:27 -0500 Subject: [PATCH 5/5] Fixed formatting errors with push.go and push_test.go --- pkg/executor/push.go | 2 +- pkg/executor/push_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/executor/push.go b/pkg/executor/push.go index bfdacaa039..c5ea7c05b3 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -96,7 +96,7 @@ func CheckPushPermissions(opts *config.KanikoOptions) error { return nil } -func getDigest(image v1.Image) ([]byte, error){ +func getDigest(image v1.Image) ([]byte, error) { digest, err := image.Digest() if err != nil { return nil, err diff --git a/pkg/executor/push_test.go b/pkg/executor/push_test.go index 6bc5dcbef0..838561dde5 100644 --- a/pkg/executor/push_test.go +++ b/pkg/executor/push_test.go @@ -205,19 +205,19 @@ func TestImageNameDigestFile(t *testing.T) { } opts := config.KanikoOptions{ - NoPush: true, - Destinations: []string{"gcr.io/foo/bar:latest", "bob/image"}, - ImageNameDigestFile: "tmpFile", + NoPush: true, + Destinations: []string{"gcr.io/foo/bar:latest", "bob/image"}, + ImageNameDigestFile: "tmpFile", } defer os.Remove("tmpFile") - + if err := DoPush(image, &opts); err != nil { t.Fatalf("could not push image: %s", err) } - want := []byte("gcr.io/foo/bar@" + digest.String() + "\nindex.docker.io/bob/image@" + digest.String() +"\n") - + want := []byte("gcr.io/foo/bar@" + digest.String() + "\nindex.docker.io/bob/image@" + digest.String() + "\n") + got, err := ioutil.ReadFile("tmpFile") testutil.CheckErrorAndDeepEqual(t, false, err, want, got)