Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of whitelisted directories in dockerignore #2589

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change means the same, but increases test coverage.

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", filepath.Join("docker", "bar"), filepath.Join("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