From 3989d274befb44a87a858656570cccda35ef71f3 Mon Sep 17 00:00:00 2001 From: Vincent Behar Date: Thu, 27 Sep 2018 14:32:29 +0200 Subject: [PATCH] Add a new flag to cleanup the filesystem at the end Currently, kaniko can only build a single image per container run, because the filesystem is full of the content of the first image. When running kaniko in Jenkins, where we need to start the container "doing nothing" first (using the debug kaniko container), and then exec /kaniko/executor, this is a limitation because it means that if we want to build multiple images, we need to start multiple containers - see https://groups.google.com/forum/#!topic/kaniko-users/_7LivHdMdy0 for more details A solution to fix this issue is to add a new flag to cleanup the filesystem at the end - the same way it is done between stages when building a multi-stages image. This way, the same (debug) container can be used to build multiple images. --- README.md | 4 ++++ cmd/executor/cmd/root.go | 1 + pkg/config/options.go | 1 + pkg/executor/build.go | 5 +++++ 4 files changed, 11 insertions(+) diff --git a/README.md b/README.md index f55cdd59dc..39c24e310b 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,10 @@ If `--destination=gcr.io/kaniko-project/test`, then cached layers will be stored _This flag must be used in conjunction with the `--cache=true` flag._ +#### --cleanup + +Set this flag to cleanup the filesystem at the end, leaving a clean kaniko container (if you want to build multiple images in the same container, using the debug kaniko image) + ### Debug Image The kaniko executor image is based off of scratch and doesn't contain a shell. diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index b2e623f1c8..7f8ba14150 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -97,6 +97,7 @@ func addKanikoOptionsFlags(cmd *cobra.Command) { RootCmd.PersistentFlags().BoolVarP(&opts.NoPush, "no-push", "", false, "Do not push the image to the registry") 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().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image") + RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end") } // addHiddenFlags marks certain flags as hidden from the executor help text diff --git a/pkg/config/options.go b/pkg/config/options.go index c9bad39e66..26fecec292 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -33,4 +33,5 @@ type KanikoOptions struct { Reproducible bool NoPush bool Cache bool + Cleanup bool } diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 29f52d6ff4..5e10d16b54 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -264,6 +264,11 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { return nil, err } } + if opts.Cleanup { + if err = util.DeleteFilesystem(); err != nil { + return nil, err + } + } return sourceImage, nil } if stage.SaveStage {