Skip to content

Commit

Permalink
Add support for .dockerignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya Wadhwa committed Oct 19, 2018
1 parent adac8d4 commit 88f601c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 61 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,6 @@ _This flag must be used in conjunction with the `--cache=true` flag._

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)

#### --ignore

Set this flag to ignore files in your build context. For examples, set `--ignore pkg/*` to ignore all files in the `pkg` directory.

### Debug Image

Expand Down
29 changes: 20 additions & 9 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/buildcontext"
"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/executor"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/genuinetools/amicontained/container"
Expand Down Expand Up @@ -94,7 +95,6 @@ func addKanikoOptionsFlags(cmd *cobra.Command) {
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().VarP(&opts.Ignore, "ignore", "", "Set this flag to ignore files in the build context. Set it repeatedly for multiple values.")
RootCmd.PersistentFlags().BoolVarP(&opts.InsecurePush, "insecure", "", false, "Push to insecure registry using plain HTTP")
RootCmd.PersistentFlags().BoolVarP(&opts.SkipTLSVerify, "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")
Expand Down Expand Up @@ -187,23 +187,34 @@ func resolveSourceContext() error {
}

func removeIgnoredFiles() error {
logrus.Infof("Removing ignored files from build context: %s", opts.Ignore)
for r, i := range opts.Ignore {
opts.Ignore[r] = filepath.Clean(filepath.Join(opts.SrcContext, i))
if !dockerfile.DockerignoreExists(opts) {
return nil
}
ignore, err := dockerfile.ParseDockerignore(opts)
if err != nil {
return err
}
err := filepath.Walk(opts.SrcContext, func(path string, fi os.FileInfo, _ error) error {
if ignoreFile(path) {
logrus.Infof("Removing ignored files from build context: %s", ignore)
for r, i := range ignore {
ignore[r] = filepath.Clean(filepath.Join(opts.SrcContext, i))
}
err = filepath.Walk(opts.SrcContext, func(path string, fi os.FileInfo, _ error) error {
if ignoreFile(path, ignore) {
if err := os.RemoveAll(path); err != nil {
logrus.Debugf("error removing %s from buildcontext", path)
}
}
return nil
})
return err
if err != nil {
return err
}
path := filepath.Join(opts.SrcContext, ".dockerignore")
return os.Remove(path)
}

func ignoreFile(path string) bool {
for _, i := range opts.Ignore {
func ignoreFile(path string, ignore []string) bool {
for _, i := range ignore {
matched, err := filepath.Match(i, path)
if err != nil {
return false
Expand Down
61 changes: 13 additions & 48 deletions integration/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ var argsMap = map[string][]string{

var filesToIgnore = []string{"test/*"}

func ignoreFlags() []string {
var f []string
for _, i := range filesToIgnore {
f = append(f, fmt.Sprintf("--ignore=%s", i))
}
return f
}

// Arguments to build Dockerfiles with when building with docker
var additionalDockerFlagsMap = map[string][]string{
"Dockerfile_test_target": {"--target=second"},
Expand All @@ -75,7 +67,6 @@ var additionalKanikoFlagsMap = map[string][]string{
"Dockerfile_test_add": {"--single-snapshot"},
"Dockerfile_test_scratch": {"--single-snapshot"},
"Dockerfile_test_target": {"--target=second"},
"Dockerfile_test_ignore": ignoreFlags(),
}

var bucketContextTests = []string{"Dockerfile_test_copy_bucket"}
Expand Down Expand Up @@ -122,10 +113,9 @@ func FindDockerFiles(dockerfilesPath string) ([]string, error) {
// keeps track of which files have been built.
type DockerFileBuilder struct {
// Holds all available docker files and whether or not they've been built
FilesBuilt map[string]bool
DockerfilesToIgnore map[string]struct{}
TestCacheDockerfiles map[string]struct{}
TestIgnoreDockerfiles map[string]struct{}
FilesBuilt map[string]bool
DockerfilesToIgnore map[string]struct{}
TestCacheDockerfiles map[string]struct{}
}

// NewDockerFileBuilder will create a DockerFileBuilder initialized with dockerfiles, which
Expand All @@ -143,9 +133,6 @@ func NewDockerFileBuilder(dockerfiles []string) *DockerFileBuilder {
"Dockerfile_test_cache": {},
"Dockerfile_test_cache_install": {},
}
d.TestIgnoreDockerfiles = map[string]struct{}{
"Dockerfile_test_ignore": {},
}
return &d
}

Expand Down Expand Up @@ -174,29 +161,24 @@ func (d *DockerFileBuilder) BuildImage(imageRepo, gcsBucket, dockerfilesPath, do
"."},
additionalFlags...)...,
)
if d.includeDockerIgnore(dockerfile) {
if err := setupTestDir(); err != nil {
return err
}
if err := generateDockerIgnore(); err != nil {
return err
}
if err := setupTestDir(); err != nil {
return err
}
if err := generateDockerIgnore(); err != nil {
return err
}

_, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
return fmt.Errorf("Failed to build image %s with docker command \"%s\": %s", dockerImage, dockerCmd.Args, err)
}

if d.includeDockerIgnore(dockerfilesPath) {
if err := deleteDockerIgnore(); err != nil {
return err
}
if err := setupTestDir(); err != nil {
return err
}
if err := setupTestDir(); err != nil {
return err
}
if err := generateDockerIgnore(); err != nil {
return err
}
defer removeTestDir()

contextFlag := "-c"
contextPath := buildContextPath
Expand Down Expand Up @@ -288,23 +270,10 @@ func (d *DockerFileBuilder) buildCachedImages(imageRepo, cacheRepo, dockerfilesP
return nil
}

func (d *DockerFileBuilder) includeDockerIgnore(dockerfile string) bool {
for i := range d.TestIgnoreDockerfiles {
if i == dockerfile {
return true
}
}
return false
}

func setupTestDir() error {
return os.MkdirAll(testDirPath, 0644)
}

func removeTestDir() error {
return os.RemoveAll(testDirPath)
}

func generateDockerIgnore() error {
f, err := os.Create(".dockerignore")
if err != nil {
Expand All @@ -317,7 +286,3 @@ func generateDockerIgnore() error {
}
return nil
}

func deleteDockerIgnore() error {
return os.Remove(".dockerignore")
}
1 change: 0 additions & 1 deletion pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type KanikoOptions struct {
CacheDir string
Destinations multiArg
BuildArgs multiArg
Ignore multiArg
InsecurePush bool
SkipTLSVerify bool
SingleSnapshot bool
Expand Down
21 changes: 21 additions & 0 deletions pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -168,3 +169,23 @@ func saveStage(index int, stages []instructions.Stage) bool {
}
return false
}

// DockerignoreExists returns true if .dockerignore exists in the source context
func DockerignoreExists(opts *config.KanikoOptions) bool {
path := filepath.Join(opts.SrcContext, ".dockerignore")
return util.FilepathExists(path)
}

// ParseDockerignore returns a list of all paths in .dockerignore
func ParseDockerignore(opts *config.KanikoOptions) ([]string, error) {
path := filepath.Join(opts.SrcContext, ".dockerignore")
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "parsing .dockerignore")
}
return strings.FieldsFunc(string(contents), split), nil
}

func split(r rune) bool {
return r == '\n' || r == ' '
}

0 comments on commit 88f601c

Please sign in to comment.