From c9d8f390deecd0de56d48cd3a8775043761d4d33 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 20 Jun 2018 09:23:13 +0200 Subject: [PATCH] Support wildcards in Dockerfiles Fixes 709 Signed-off-by: David Gageot --- pkg/skaffold/docker/parse.go | 29 ++++++++++++++++++++++++- pkg/skaffold/docker/parse_test.go | 36 +++++++++++++++++++++++++------ 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/pkg/skaffold/docker/parse.go b/pkg/skaffold/docker/parse.go index 921f4b14011..05f3bd2b2d6 100644 --- a/pkg/skaffold/docker/parse.go +++ b/pkg/skaffold/docker/parse.go @@ -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, "glob") + } + 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) diff --git a/pkg/skaffold/docker/parse_test.go b/pkg/skaffold/docker/parse_test.go index 86b2dbb8ae1..41538275aac 100644 --- a/pkg/skaffold/docker/parse_test.go +++ b/pkg/skaffold/docker/parse_test.go @@ -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/ @@ -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{}{ @@ -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, @@ -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",