From 27eecc5695827bb80049a7e2339ec4da15584d85 Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Wed, 31 Jul 2019 22:49:44 +0200 Subject: [PATCH] Fix handling of whitelisted directories in dockerignore This code snippet closely follows the reference implementation from github.com/docker/docker/pkg/archive/archive.go Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- pkg/skaffold/docker/dependencies.go | 21 ++++++++++++++++++++- pkg/skaffold/docker/dependencies_test.go | 16 +++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pkg/skaffold/docker/dependencies.go b/pkg/skaffold/docker/dependencies.go index f45ad75883a..e1f5b2a85d8 100644 --- a/pkg/skaffold/docker/dependencies.go +++ b/pkg/skaffold/docker/dependencies.go @@ -146,9 +146,28 @@ func WalkWorkspace(workspace string, excludes, deps []string) (map[string]bool, } if info.IsDir() { - if ignored { + if !ignored { + return nil + } + // exclusion handling closely follows vendor/github.com/docker/docker/pkg/archive/archive.go + // No exceptions (!...) in patterns so just skip dir + if !pExclude.Exclusions() { return filepath.SkipDir } + + dirSlash := relPath + string(filepath.Separator) + + for _, pat := range pExclude.Patterns() { + if !pat.Exclusion() { + continue + } + if strings.HasPrefix(pat.String()+string(filepath.Separator), dirSlash) { + // found a match - so can't skip this dir + return nil + } + } + + return filepath.SkipDir } else if !ignored { files[relPath] = true } diff --git a/pkg/skaffold/docker/dependencies_test.go b/pkg/skaffold/docker/dependencies_test.go index 17ab86b3797..b23fc5d41d8 100644 --- a/pkg/skaffold/docker/dependencies_test.go +++ b/pkg/skaffold/docker/dependencies_test.go @@ -357,7 +357,7 @@ func TestGetDependencies(t *testing.T) { description: "dockerignore with context in parent directory", dockerfile: copyDirectory, workspace: "docker/..", - ignore: "bar\ndocker/*\n*.go", + ignore: "bar\ndocker\n*.go", expected: []string{".dot", "Dockerfile", "file", "test.conf"}, }, { @@ -484,6 +484,20 @@ func TestGetDependencies(t *testing.T) { buildArgs: map[string]*string{"FOO": util.StringPtr("{{")}, shouldErr: true, }, + { + description: "ignore with whitelisting", + dockerfile: copyAll, + workspace: ".", + ignore: "**\n!docker/**", + expected: []string{"Dockerfile", "docker/bar", "docker/nginx.conf"}, + }, + { + description: "ignore with whitelisting files", + dockerfile: copyAll, + workspace: ".", + ignore: "**\n!server.go", + expected: []string{"Dockerfile", "server.go"}, + }, } for _, test := range tests {