@@ -17,52 +17,32 @@ limitations under the License.
1717package cmd
1818
1919import (
20- "errors"
2120 "os"
2221 "path/filepath"
2322 "strings"
2423
2524 "github.com/GoogleContainerTools/kaniko/pkg/buildcontext"
2625 "github.com/GoogleContainerTools/kaniko/pkg/constants"
2726 "github.com/GoogleContainerTools/kaniko/pkg/executor"
27+ "github.com/GoogleContainerTools/kaniko/pkg/options"
2828 "github.com/GoogleContainerTools/kaniko/pkg/util"
2929 "github.com/genuinetools/amicontained/container"
30+ "github.com/pkg/errors"
3031 "github.com/sirupsen/logrus"
3132 "github.com/spf13/cobra"
3233)
3334
3435var (
35- dockerfilePath string
36- destinations multiArg
37- srcContext string
38- snapshotMode string
39- bucket string
40- dockerInsecureSkipTLSVerify bool
41- logLevel string
42- force bool
43- buildArgs multiArg
44- tarPath string
45- singleSnapshot bool
46- reproducible bool
47- target string
48- noPush bool
36+ opts = & options.KanikoOptions {}
37+ logLevel string
38+ force bool
4939)
5040
5141func init () {
52- RootCmd .PersistentFlags ().StringVarP (& dockerfilePath , "dockerfile" , "f" , "Dockerfile" , "Path to the dockerfile to be built." )
53- RootCmd .PersistentFlags ().StringVarP (& srcContext , "context" , "c" , "/workspace/" , "Path to the dockerfile build context." )
54- RootCmd .PersistentFlags ().StringVarP (& bucket , "bucket" , "b" , "" , "Name of the GCS bucket from which to access build context as tarball." )
55- RootCmd .PersistentFlags ().VarP (& destinations , "destination" , "d" , "Registry the final image should be pushed to. Set it repeatedly for multiple destinations." )
56- RootCmd .PersistentFlags ().StringVarP (& snapshotMode , "snapshotMode" , "" , "full" , "Set this flag to change the file attributes inspected during snapshotting" )
57- RootCmd .PersistentFlags ().VarP (& buildArgs , "build-arg" , "" , "This flag allows you to pass in ARG values at build time. Set it repeatedly for multiple values." )
58- RootCmd .PersistentFlags ().BoolVarP (& dockerInsecureSkipTLSVerify , "insecure-skip-tls-verify" , "" , false , "Push to insecure registry ignoring TLS verify" )
5942 RootCmd .PersistentFlags ().StringVarP (& logLevel , "verbosity" , "v" , constants .DefaultLogLevel , "Log level (debug, info, warn, error, fatal, panic" )
6043 RootCmd .PersistentFlags ().BoolVarP (& force , "force" , "" , false , "Force building outside of a container" )
61- RootCmd .PersistentFlags ().StringVarP (& tarPath , "tarPath" , "" , "" , "Path to save the image in as a tarball instead of pushing" )
62- RootCmd .PersistentFlags ().BoolVarP (& singleSnapshot , "single-snapshot" , "" , false , "Set this flag to take a single snapshot at the end of the build." )
63- RootCmd .PersistentFlags ().BoolVarP (& reproducible , "reproducible" , "" , false , "Strip timestamps out of the image to make it reproducible" )
64- RootCmd .PersistentFlags ().StringVarP (& target , "target" , "" , "" , " Set the target build stage to build" )
65- RootCmd .PersistentFlags ().BoolVarP (& noPush , "no-push" , "" , false , "Do not push the image to the registry" )
44+ addKanikoOptionsFlags (RootCmd )
45+ addHiddenFlags (RootCmd )
6646}
6747
6848var RootCmd = & cobra.Command {
@@ -71,109 +51,109 @@ var RootCmd = &cobra.Command{
7151 if err := util .SetLogLevel (logLevel ); err != nil {
7252 return err
7353 }
74- if err := resolveSourceContext (); err != nil {
75- return err
76- }
77- if ! noPush && len (destinations ) == 0 {
54+ if ! opts .NoPush && len (opts .Destinations ) == 0 {
7855 return errors .New ("You must provide --destination, or use --no-push" )
7956 }
80-
81- return checkDockerfilePath ()
57+ if err := resolveSourceContext (); err != nil {
58+ return errors .Wrap (err , "error resolving source context" )
59+ }
60+ return resolveDockerfilePath ()
8261 },
83- Run : func (cmd * cobra.Command , args []string ) {
62+ RunE : func (cmd * cobra.Command , args []string ) error {
8463 if ! checkContained () {
8564 if ! force {
86- logrus .Error ("kaniko should only be run inside of a container, run with the --force flag if you are sure you want to continue." )
87- os .Exit (1 )
65+ return errors .New ("kaniko should only be run inside of a container, run with the --force flag if you are sure you want to continue" )
8866 }
8967 logrus .Warn ("kaniko is being run outside of a container. This can have dangerous effects on your system" )
9068 }
9169 if err := os .Chdir ("/" ); err != nil {
92- logrus .Error (err )
93- os .Exit (1 )
70+ return errors .Wrap (err , "error changing to root dir" )
9471 }
95- image , err := executor .DoBuild (executor.KanikoBuildArgs {
96- DockerfilePath : absouteDockerfilePath (),
97- SrcContext : srcContext ,
98- SnapshotMode : snapshotMode ,
99- Args : buildArgs ,
100- SingleSnapshot : singleSnapshot ,
101- Reproducible : reproducible ,
102- Target : target ,
103- })
72+ image , err := executor .DoBuild (opts )
10473 if err != nil {
105- logrus .Error (err )
106- os .Exit (1 )
107- }
108-
109- if noPush {
110- logrus .Info ("Skipping push to container registry due to --no-push flag" )
111- os .Exit (0 )
74+ return errors .Wrap (err , "error building image" )
11275 }
76+ return executor .DoPush (image , opts )
77+ },
78+ }
11379
114- if err := executor .DoPush (image , destinations , tarPath , dockerInsecureSkipTLSVerify ); err != nil {
115- logrus .Error (err )
116- os .Exit (1 )
117- }
80+ // addKanikoOptionsFlags configures opts
81+ func addKanikoOptionsFlags (cmd * cobra.Command ) {
82+ RootCmd .PersistentFlags ().StringVarP (& opts .DockerfilePath , "dockerfile" , "f" , "Dockerfile" , "Path to the dockerfile to be built." )
83+ RootCmd .PersistentFlags ().StringVarP (& opts .SrcContext , "context" , "c" , "/workspace/" , "Path to the dockerfile build context." )
84+ RootCmd .PersistentFlags ().StringVarP (& opts .Bucket , "bucket" , "b" , "" , "Name of the GCS bucket from which to access build context as tarball." )
85+ RootCmd .PersistentFlags ().VarP (& opts .Destinations , "destination" , "d" , "Registry the final image should be pushed to. Set it repeatedly for multiple destinations." )
86+ RootCmd .PersistentFlags ().StringVarP (& opts .SnapshotMode , "snapshotMode" , "" , "full" , "Change the file attributes inspected during snapshotting" )
87+ RootCmd .PersistentFlags ().VarP (& opts .BuildArgs , "build-arg" , "" , "This flag allows you to pass in ARG values at build time. Set it repeatedly for multiple values." )
88+ RootCmd .PersistentFlags ().BoolVarP (& opts .DockerInsecureSkipTLSVerify , "insecure-skip-tls-verify" , "" , false , "Push to insecure registry ignoring TLS verify" )
89+ RootCmd .PersistentFlags ().StringVarP (& opts .TarPath , "tarPath" , "" , "" , "Path to save the image in as a tarball instead of pushing" )
90+ RootCmd .PersistentFlags ().BoolVarP (& opts .SingleSnapshot , "single-snapshot" , "" , false , "Take a single snapshot at the end of the build." )
91+ RootCmd .PersistentFlags ().BoolVarP (& opts .Reproducible , "reproducible" , "" , false , "Strip timestamps out of the image to make it reproducible" )
92+ RootCmd .PersistentFlags ().StringVarP (& opts .Target , "target" , "" , "" , "Set the target build stage to build" )
93+ RootCmd .PersistentFlags ().BoolVarP (& opts .NoPush , "no-push" , "" , false , "Do not push the image to the registry" )
94+ }
11895
119- },
96+ // addHiddenFlags marks certain flags as hidden from the executor help text
97+ func addHiddenFlags (cmd * cobra.Command ) {
98+ // This flag is added in a vendored directory, hide so that it doesn't come up via --help
99+ RootCmd .PersistentFlags ().MarkHidden ("azure-container-registry-config" )
100+ // Hide this flag as we want to encourage people to use the --context flag instead
101+ RootCmd .PersistentFlags ().MarkHidden ("bucket" )
120102}
121103
122104func checkContained () bool {
123105 _ , err := container .DetectRuntime ()
124106 return err == nil
125107}
126108
127- func checkDockerfilePath () error {
128- if util .FilepathExists (dockerfilePath ) {
129- if _ , err := filepath .Abs (dockerfilePath ); err != nil {
130- return err
109+ // resolveDockerfilePath resolves the Dockerfile path to an absolute path
110+ func resolveDockerfilePath () error {
111+ if util .FilepathExists (opts .DockerfilePath ) {
112+ abs , err := filepath .Abs (opts .DockerfilePath )
113+ if err != nil {
114+ return errors .Wrap (err , "getting absolute path for dockerfile" )
131115 }
116+ opts .DockerfilePath = abs
132117 return nil
133118 }
134119 // Otherwise, check if the path relative to the build context exists
135- if util .FilepathExists (filepath .Join (srcContext , dockerfilePath )) {
120+ if util .FilepathExists (filepath .Join (opts .SrcContext , opts .DockerfilePath )) {
121+ abs , err := filepath .Abs (filepath .Join (opts .SrcContext , opts .DockerfilePath ))
122+ if err != nil {
123+ return errors .Wrap (err , "getting absolute path for src context/dockerfile path" )
124+ }
125+ opts .DockerfilePath = abs
136126 return nil
137127 }
138- return errors .New ("please provide a valid path to a Dockerfile within the build context" )
139- }
140-
141- func absouteDockerfilePath () string {
142- if util .FilepathExists (dockerfilePath ) {
143- // Ignore error since we already checked it in checkDockerfilePath()
144- abs , _ := filepath .Abs (dockerfilePath )
145- return abs
146- }
147- // Otherwise, return path relative to build context
148- return filepath .Join (srcContext , dockerfilePath )
128+ return errors .New ("please provide a valid path to a Dockerfile within the build context with --dockerfile" )
149129}
150130
151131// resolveSourceContext unpacks the source context if it is a tar in a bucket
152132// it resets srcContext to be the path to the unpacked build context within the image
153133func resolveSourceContext () error {
154- if srcContext == "" && bucket == "" {
134+ if opts . SrcContext == "" && opts . Bucket == "" {
155135 return errors .New ("please specify a path to the build context with the --context flag or a bucket with the --bucket flag" )
156136 }
157- if srcContext != "" && ! strings .Contains (srcContext , "://" ) {
137+ if opts . SrcContext != "" && ! strings .Contains (opts . SrcContext , "://" ) {
158138 return nil
159139 }
160- if bucket != "" {
161- if ! strings .Contains (bucket , "://" ) {
162- srcContext = constants .GCSBuildContextPrefix + bucket
140+ if opts . Bucket != "" {
141+ if ! strings .Contains (opts . Bucket , "://" ) {
142+ opts . SrcContext = constants .GCSBuildContextPrefix + opts . Bucket
163143 } else {
164- srcContext = bucket
144+ opts . SrcContext = opts . Bucket
165145 }
166146 }
167147 // if no prefix use Google Cloud Storage as default for backwards compability
168- contextExecutor , err := buildcontext .GetBuildContext (srcContext )
148+ contextExecutor , err := buildcontext .GetBuildContext (opts . SrcContext )
169149 if err != nil {
170150 return err
171151 }
172- logrus .Debugf ("Getting source context from %s" , srcContext )
173- srcContext , err = contextExecutor .UnpackTarFromBuildContext ()
152+ logrus .Debugf ("Getting source context from %s" , opts . SrcContext )
153+ opts . SrcContext , err = contextExecutor .UnpackTarFromBuildContext ()
174154 if err != nil {
175155 return err
176156 }
177- logrus .Debugf ("Build context located at %s" , srcContext )
157+ logrus .Debugf ("Build context located at %s" , opts . SrcContext )
178158 return nil
179159}
0 commit comments