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

Support wildcards in Dockerfiles #712

Merged
merged 1 commit into from
Jun 21, 2018
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
29 changes: 28 additions & 1 deletion pkg/skaffold/docker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,35 @@ func readDockerfile(workspace, dockerfilePath string) ([]string, error) {

dispatchInstructions(res)

expandedPaths := make(map[string]bool)
for p := range depMap {
path := filepath.Join(workspace, p)

if _, err := os.Stat(path); err == nil {
expandedPaths[p] = true
continue
}

files, err := filepath.Glob(path)
if err != nil {
return nil, errors.Wrap(err, "invalid glob pattern")
}
if files == nil {
return nil, fmt.Errorf("file pattern must match at least one file %s", path)
}

for _, f := range files {
rel, err := filepath.Rel(workspace, f)
if err != nil {
return nil, fmt.Errorf("getting relative path of %s", f)
}

expandedPaths[rel] = true
}
}

var deps []string
for dep := range depMap {
for dep := range expandedPaths {
deps = append(deps, dep)
}
logrus.Infof("Found dependencies for dockerfile %s", deps)
Expand Down
36 changes: 30 additions & 6 deletions pkg/skaffold/docker/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ COPY test.conf /etc/test2
CMD nginx
`

const wildcards = `
FROM nginx
ADD *.go /tmp
`

const wildcardsMatchesNone = `
FROM nginx
ADD *.none /tmp
`

const multiStageDockerfile = `
FROM golang:1.9.2
WORKDIR /go/src/github.com/r2d4/leeroy/
Expand Down Expand Up @@ -106,9 +116,11 @@ var ImageConfigs = map[string]*v1.ConfigFile{
},
},
},
"ubuntu:14.04": {Config: v1.Config{}},
"nginx": {Config: v1.Config{}},
"busybox": {Config: v1.Config{}},
"golang:1.9.2": {Config: v1.Config{}},
"gcr.io/distroless/base": {Config: v1.Config{}},
"ubuntu:14.04": {Config: v1.Config{}},
"nginx": {Config: v1.Config{}},
"busybox": {Config: v1.Config{}},
"oneport": {
Config: v1.Config{
ExposedPorts: map[string]struct{}{
Expand Down Expand Up @@ -154,6 +166,18 @@ func TestGetDependencies(t *testing.T) {
workspace: "docker",
expected: []string{"Dockerfile", "nginx.conf"},
},
{
description: "wildcards",
dockerfile: wildcards,
workspace: ".",
expected: []string{"Dockerfile", "server.go", "worker.go"},
},
{
description: "wildcards matches none",
dockerfile: wildcardsMatchesNone,
workspace: ".",
shouldErr: true,
},
{
description: "bad read",
badReader: true,
Expand Down Expand Up @@ -212,10 +236,10 @@ func TestGetDependencies(t *testing.T) {
},
{
description: "dockerignore with context in parent directory",
dockerfile: contextDockerfile,
dockerfile: copyDirectory,
workspace: "docker/..",
ignore: "bar\ndocker/*",
expected: []string{"Dockerfile", "file", "server.go", "test.conf", "worker.go"},
ignore: "bar\ndocker/*\n*.go",
expected: []string{"Dockerfile", "file", "test.conf"},
},
{
description: "onbuild test",
Expand Down