mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 04:46:31 +02:00
Refactor command line arguments and the executor
In this refactor I: 1. Created KanikoOptions to make it easier to pass around arguments passed in through the command line 2. Reorganized executor.go by putting the logic for pushing the image in a new file push.go 3. Made some error messages clearer 4. Fixed a mistake in the README for pushing to AWS 5. Marked the --bucket flag as hidden since we want people to use --context instead, and marked an aws flag as hidden which is set in a vendored directorya
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
Copyright 2018 Google LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// This type is used to supported passing in multiple flags
|
||||
type multiArg []string
|
||||
|
||||
// Now, for our new type, implement the two methods of
|
||||
// the flag.Value interface...
|
||||
// The first method is String() string
|
||||
func (b *multiArg) String() string {
|
||||
return strings.Join(*b, ",")
|
||||
}
|
||||
|
||||
// The second method is Set(value string) error
|
||||
func (b *multiArg) Set(value string) error {
|
||||
logrus.Infof("appending to multi args %s", value)
|
||||
*b = append(*b, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *multiArg) Type() string {
|
||||
return "multi-arg type"
|
||||
}
|
||||
+72
-88
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -25,44 +24,24 @@ import (
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/buildcontext"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/constants"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/executor"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/options"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
||||
"github.com/genuinetools/amicontained/container"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
dockerfilePath string
|
||||
destinations multiArg
|
||||
srcContext string
|
||||
snapshotMode string
|
||||
bucket string
|
||||
dockerInsecureSkipTLSVerify bool
|
||||
logLevel string
|
||||
force bool
|
||||
buildArgs multiArg
|
||||
tarPath string
|
||||
singleSnapshot bool
|
||||
reproducible bool
|
||||
target string
|
||||
noPush bool
|
||||
opts = &options.KanikoOptions{}
|
||||
logLevel string
|
||||
force bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
|
||||
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "/workspace/", "Path to the dockerfile build context.")
|
||||
RootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
|
||||
RootCmd.PersistentFlags().VarP(&destinations, "destination", "d", "Registry the final image should be pushed to. Set it repeatedly for multiple destinations.")
|
||||
RootCmd.PersistentFlags().StringVarP(&snapshotMode, "snapshotMode", "", "full", "Set this flag to change the file attributes inspected during snapshotting")
|
||||
RootCmd.PersistentFlags().VarP(&buildArgs, "build-arg", "", "This flag allows you to pass in ARG values at build time. Set it repeatedly for multiple values.")
|
||||
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")
|
||||
RootCmd.PersistentFlags().BoolVarP(&singleSnapshot, "single-snapshot", "", false, "Set this flag to take a single snapshot at the end of the build.")
|
||||
RootCmd.PersistentFlags().BoolVarP(&reproducible, "reproducible", "", false, "Strip timestamps out of the image to make it reproducible")
|
||||
RootCmd.PersistentFlags().StringVarP(&target, "target", "", "", " Set the target build stage to build")
|
||||
RootCmd.PersistentFlags().BoolVarP(&noPush, "no-push", "", false, "Do not push the image to the registry")
|
||||
addSetupFlags(RootCmd)
|
||||
addKanikoOptionsFlags(RootCmd)
|
||||
addHiddenFlags(RootCmd)
|
||||
}
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
@@ -71,109 +50,114 @@ var RootCmd = &cobra.Command{
|
||||
if err := util.SetLogLevel(logLevel); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := resolveSourceContext(); err != nil {
|
||||
return err
|
||||
}
|
||||
if !noPush && len(destinations) == 0 {
|
||||
if !opts.NoPush && len(opts.Destinations) == 0 {
|
||||
return errors.New("You must provide --destination, or use --no-push")
|
||||
}
|
||||
|
||||
return checkDockerfilePath()
|
||||
if err := resolveSourceContext(); err != nil {
|
||||
return errors.Wrap(err, "error resolving source context")
|
||||
}
|
||||
return resolveDockerfilePath()
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !checkContained() {
|
||||
if !force {
|
||||
logrus.Error("kaniko should only be run inside of a container, run with the --force flag if you are sure you want to continue.")
|
||||
os.Exit(1)
|
||||
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")
|
||||
}
|
||||
logrus.Warn("kaniko is being run outside of a container. This can have dangerous effects on your system")
|
||||
}
|
||||
if err := os.Chdir("/"); err != nil {
|
||||
logrus.Error(err)
|
||||
os.Exit(1)
|
||||
return errors.Wrap(err, "error changing to root dir")
|
||||
}
|
||||
image, err := executor.DoBuild(executor.KanikoBuildArgs{
|
||||
DockerfilePath: absouteDockerfilePath(),
|
||||
SrcContext: srcContext,
|
||||
SnapshotMode: snapshotMode,
|
||||
Args: buildArgs,
|
||||
SingleSnapshot: singleSnapshot,
|
||||
Reproducible: reproducible,
|
||||
Target: target,
|
||||
})
|
||||
image, err := executor.DoBuild(opts)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
os.Exit(1)
|
||||
return errors.Wrap(err, "error building image")
|
||||
}
|
||||
|
||||
if noPush {
|
||||
logrus.Info("Skipping push to container registry due to --no-push flag")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if err := executor.DoPush(image, destinations, tarPath, dockerInsecureSkipTLSVerify); err != nil {
|
||||
logrus.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return executor.DoPush(image, opts)
|
||||
},
|
||||
}
|
||||
|
||||
func addSetupFlags(cmd *cobra.Command) {
|
||||
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")
|
||||
}
|
||||
|
||||
// addKanikoOptionsFlags configures opts
|
||||
func addKanikoOptionsFlags(cmd *cobra.Command) {
|
||||
RootCmd.PersistentFlags().StringVarP(&opts.DockerfilePath, "dockerfile", "f", "Dockerfile", "Path to the dockerfile to be built.")
|
||||
RootCmd.PersistentFlags().StringVarP(&opts.SrcContext, "context", "c", "/workspace/", "Path to the dockerfile build context.")
|
||||
RootCmd.PersistentFlags().StringVarP(&opts.Bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
|
||||
RootCmd.PersistentFlags().VarP(&opts.Destinations, "destination", "d", "Registry the final image should be pushed to. Set it repeatedly for multiple destinations.")
|
||||
RootCmd.PersistentFlags().StringVarP(&opts.SnapshotMode, "snapshotMode", "", "full", "Change the file attributes inspected during snapshotting")
|
||||
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.")
|
||||
RootCmd.PersistentFlags().BoolVarP(&opts.DockerInsecureSkipTLSVerify, "insecure-skip-tls-verify", "", false, "Push to insecure registry ignoring TLS verify")
|
||||
RootCmd.PersistentFlags().StringVarP(&opts.TarPath, "tarPath", "", "", "Path to save the image in as a tarball instead of pushing")
|
||||
RootCmd.PersistentFlags().BoolVarP(&opts.SingleSnapshot, "single-snapshot", "", false, "Take a single snapshot at the end of the build.")
|
||||
RootCmd.PersistentFlags().BoolVarP(&opts.Reproducible, "reproducible", "", false, "Strip timestamps out of the image to make it reproducible")
|
||||
RootCmd.PersistentFlags().StringVarP(&opts.Target, "target", "", "", "Set the target build stage to build")
|
||||
RootCmd.PersistentFlags().BoolVarP(&opts.NoPush, "no-push", "", false, "Do not push the image to the registry")
|
||||
}
|
||||
|
||||
// addHiddenFlags marks certain flags as hidden from the executor help text
|
||||
func addHiddenFlags(cmd *cobra.Command) {
|
||||
// This flag is added in a vendored directory, hide so that it doesn't come up via --help
|
||||
RootCmd.PersistentFlags().MarkHidden("azure-container-registry-config")
|
||||
// Hide this flag as we want to encourage people to use the --context flag instead
|
||||
RootCmd.PersistentFlags().MarkHidden("bucket")
|
||||
}
|
||||
|
||||
func checkContained() bool {
|
||||
_, err := container.DetectRuntime()
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func checkDockerfilePath() error {
|
||||
if util.FilepathExists(dockerfilePath) {
|
||||
if _, err := filepath.Abs(dockerfilePath); err != nil {
|
||||
return err
|
||||
// resolveDockerfilePath resolves the Dockerfile path to an absolute path
|
||||
func resolveDockerfilePath() error {
|
||||
if util.FilepathExists(opts.DockerfilePath) {
|
||||
abs, err := filepath.Abs(opts.DockerfilePath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting absolute path for dockerfile")
|
||||
}
|
||||
opts.DockerfilePath = abs
|
||||
return nil
|
||||
}
|
||||
// Otherwise, check if the path relative to the build context exists
|
||||
if util.FilepathExists(filepath.Join(srcContext, dockerfilePath)) {
|
||||
if util.FilepathExists(filepath.Join(opts.SrcContext, opts.DockerfilePath)) {
|
||||
abs, err := filepath.Abs(filepath.Join(opts.SrcContext, opts.DockerfilePath))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "getting absolute path for src context/dockerfile path")
|
||||
}
|
||||
opts.DockerfilePath = abs
|
||||
return nil
|
||||
}
|
||||
return errors.New("please provide a valid path to a Dockerfile within the build context")
|
||||
}
|
||||
|
||||
func absouteDockerfilePath() string {
|
||||
if util.FilepathExists(dockerfilePath) {
|
||||
// Ignore error since we already checked it in checkDockerfilePath()
|
||||
abs, _ := filepath.Abs(dockerfilePath)
|
||||
return abs
|
||||
}
|
||||
// Otherwise, return path relative to build context
|
||||
return filepath.Join(srcContext, dockerfilePath)
|
||||
return errors.New("please provide a valid path to a Dockerfile within the build context with --dockerfile")
|
||||
}
|
||||
|
||||
// resolveSourceContext unpacks the source context if it is a tar in a bucket
|
||||
// it resets srcContext to be the path to the unpacked build context within the image
|
||||
func resolveSourceContext() error {
|
||||
if srcContext == "" && bucket == "" {
|
||||
if opts.SrcContext == "" && opts.Bucket == "" {
|
||||
return errors.New("please specify a path to the build context with the --context flag or a bucket with the --bucket flag")
|
||||
}
|
||||
if srcContext != "" && !strings.Contains(srcContext, "://") {
|
||||
if opts.SrcContext != "" && !strings.Contains(opts.SrcContext, "://") {
|
||||
return nil
|
||||
}
|
||||
if bucket != "" {
|
||||
if !strings.Contains(bucket, "://") {
|
||||
srcContext = constants.GCSBuildContextPrefix + bucket
|
||||
if opts.Bucket != "" {
|
||||
if !strings.Contains(opts.Bucket, "://") {
|
||||
opts.SrcContext = constants.GCSBuildContextPrefix + opts.Bucket
|
||||
} else {
|
||||
srcContext = bucket
|
||||
opts.SrcContext = opts.Bucket
|
||||
}
|
||||
}
|
||||
// if no prefix use Google Cloud Storage as default for backwards compability
|
||||
contextExecutor, err := buildcontext.GetBuildContext(srcContext)
|
||||
contextExecutor, err := buildcontext.GetBuildContext(opts.SrcContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("Getting source context from %s", srcContext)
|
||||
srcContext, err = contextExecutor.UnpackTarFromBuildContext()
|
||||
logrus.Debugf("Getting source context from %s", opts.SrcContext)
|
||||
opts.SrcContext, err = contextExecutor.UnpackTarFromBuildContext()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("Build context located at %s", srcContext)
|
||||
logrus.Debugf("Build context located at %s", opts.SrcContext)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user