From 347d835781c2a0c8e6a3ad03696b2ebd9f6e6576 Mon Sep 17 00:00:00 2001 From: dlorenc Date: Tue, 15 May 2018 15:32:27 -0700 Subject: [PATCH] Add a mode to save to a tarball instead of pushing. (#178) --- cmd/executor/cmd/root.go | 4 +++- pkg/executor/executor.go | 15 +++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index bbfeb0102..63e42831d 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -39,6 +39,7 @@ var ( logLevel string force bool buildArgs buildArg + tarPath string ) func init() { @@ -52,6 +53,7 @@ func init() { RootCmd.PersistentFlags().BoolVarP(&dockerInsecureSkipTLSVerify, "insecure-skip-tls-verify", "", false, "Push to insecure registry ignoring TLS verify") RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic") RootCmd.PersistentFlags().BoolVarP(&force, "force", "", false, "Force building outside of a container") + RootCmd.PersistentFlags().StringVarP(&tarPath, "tarPath", "", "", "Path to save the image in as a tarball instead of pushing") } var RootCmd = &cobra.Command{ @@ -82,7 +84,7 @@ var RootCmd = &cobra.Command{ logrus.Error(err) os.Exit(1) } - if err := executor.DoPush(ref, image, destination); err != nil { + if err := executor.DoPush(ref, image, destination, tarPath); err != nil { logrus.Error(err) os.Exit(1) } diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index e8a3908f0..b3c697859 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -19,13 +19,14 @@ package executor import ( "bytes" "fmt" - "github.com/GoogleContainerTools/kaniko/pkg/snapshot" "io" "net/http" "os" "path/filepath" "strconv" + "github.com/GoogleContainerTools/kaniko/pkg/snapshot" + "github.com/google/go-containerregistry/v1/empty" "github.com/google/go-containerregistry/v1/tarball" @@ -36,13 +37,14 @@ import ( "github.com/google/go-containerregistry/v1/mutate" "github.com/google/go-containerregistry/v1/remote" + "io/ioutil" + "github.com/GoogleContainerTools/kaniko/pkg/commands" "github.com/GoogleContainerTools/kaniko/pkg/constants" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" "github.com/GoogleContainerTools/kaniko/pkg/util" "github.com/docker/docker/builder/dockerfile/instructions" "github.com/sirupsen/logrus" - "io/ioutil" ) func DoBuild(dockerfilePath, srcContext, snapshotMode string, args []string) (name.Reference, v1.Image, error) { @@ -168,12 +170,17 @@ func DoBuild(dockerfilePath, srcContext, snapshotMode string, args []string) (na return nil, nil, err } -func DoPush(ref name.Reference, image v1.Image, destination string) error { +func DoPush(ref name.Reference, image v1.Image, destination, tarPath string) error { // Push the image - destRef, err := name.ParseReference(destination, name.WeakValidation) + destRef, err := name.NewTag(destination, name.WeakValidation) if err != nil { return err } + + if tarPath != "" { + return tarball.WriteToFile(tarPath, destRef, image, nil) + } + wo := remote.WriteOptions{} if ref != nil { wo.MountPaths = []name.Repository{ref.Context()}