Skip to content

Commit

Permalink
Fix handling of whitelisted directories in dockerignore
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
corneliusweig committed Jul 31, 2019
1 parent 1df9667 commit 27eecc5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 20 additions & 1 deletion pkg/skaffold/docker/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/skaffold/docker/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 27eecc5

Please sign in to comment.