From 0fb5a244339cfc99c1f081679a77d2f5170481d7 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 30 Mar 2020 10:53:33 +0200 Subject: [PATCH] Support globs in custom/buildpacks builder deps (#3878) Fixes #3826 Signed-off-by: David Gageot --- examples/custom/skaffold.yaml | 3 +- integration/examples/custom/skaffold.yaml | 3 +- pkg/skaffold/build/buildpacks/dependencies.go | 17 +---- .../build/buildpacks/dependencies_test.go | 14 +++- pkg/skaffold/build/custom/dependencies.go | 13 +--- .../build/custom/dependencies_test.go | 14 +++- pkg/skaffold/build/list/list.go | 64 +++++++++++++++++++ 7 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 pkg/skaffold/build/list/list.go diff --git a/examples/custom/skaffold.yaml b/examples/custom/skaffold.yaml index 8b4ed209df5..9bca1283373 100644 --- a/examples/custom/skaffold.yaml +++ b/examples/custom/skaffold.yaml @@ -7,4 +7,5 @@ build: buildCommand: ./build.sh dependencies: paths: - - . + - "go.mod" + - "**.go" diff --git a/integration/examples/custom/skaffold.yaml b/integration/examples/custom/skaffold.yaml index 8b4ed209df5..9bca1283373 100644 --- a/integration/examples/custom/skaffold.yaml +++ b/integration/examples/custom/skaffold.yaml @@ -7,4 +7,5 @@ build: buildCommand: ./build.sh dependencies: paths: - - . + - "go.mod" + - "**.go" diff --git a/pkg/skaffold/build/buildpacks/dependencies.go b/pkg/skaffold/build/buildpacks/dependencies.go index ea5370bd4d2..0ba98be14a9 100644 --- a/pkg/skaffold/build/buildpacks/dependencies.go +++ b/pkg/skaffold/build/buildpacks/dependencies.go @@ -18,25 +18,12 @@ package buildpacks import ( "context" - "fmt" - "sort" - "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/list" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" ) // GetDependencies returns dependencies listed for a buildpack artifact func GetDependencies(ctx context.Context, workspace string, a *latest.BuildpackArtifact) ([]string, error) { - files, err := docker.WalkWorkspace(workspace, a.Dependencies.Ignore, a.Dependencies.Paths) - if err != nil { - return nil, fmt.Errorf("walking workspace %q: %w", workspace, err) - } - - var dependencies []string - for file := range files { - dependencies = append(dependencies, file) - } - - sort.Strings(dependencies) - return dependencies, nil + return list.Files(workspace, a.Dependencies.Paths, a.Dependencies.Ignore) } diff --git a/pkg/skaffold/build/buildpacks/dependencies_test.go b/pkg/skaffold/build/buildpacks/dependencies_test.go index 1d3b0846ab3..9a53bdaf870 100644 --- a/pkg/skaffold/build/buildpacks/dependencies_test.go +++ b/pkg/skaffold/build/buildpacks/dependencies_test.go @@ -37,14 +37,22 @@ func TestGetDependencies(t *testing.T) { description: "watch everything", paths: []string{"."}, expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"}, - }, { + }, + { description: "watch nothing", - }, { + }, + { description: "ignore some paths", paths: []string{"."}, ignore: []string{"b*"}, expected: []string{"foo"}, - }, { + }, + { + description: "glob", + paths: []string{"**"}, + expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"}, + }, + { description: "error", paths: []string{"unknown"}, shouldErr: true, diff --git a/pkg/skaffold/build/custom/dependencies.go b/pkg/skaffold/build/custom/dependencies.go index cde5d401bc1..94fb8e32f85 100644 --- a/pkg/skaffold/build/custom/dependencies.go +++ b/pkg/skaffold/build/custom/dependencies.go @@ -21,9 +21,9 @@ import ( "encoding/json" "fmt" "os/exec" - "sort" "strings" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/list" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" @@ -50,15 +50,6 @@ func GetDependencies(ctx context.Context, workspace string, a *latest.CustomArti return deps, nil default: - files, err := docker.WalkWorkspace(workspace, a.Dependencies.Ignore, a.Dependencies.Paths) - if err != nil { - return nil, fmt.Errorf("walking workspace %q: %w", workspace, err) - } - var dependencies []string - for file := range files { - dependencies = append(dependencies, file) - } - sort.Strings(dependencies) - return dependencies, nil + return list.Files(workspace, a.Dependencies.Paths, a.Dependencies.Ignore) } } diff --git a/pkg/skaffold/build/custom/dependencies_test.go b/pkg/skaffold/build/custom/dependencies_test.go index 643ef0dca95..c7a88ab789b 100644 --- a/pkg/skaffold/build/custom/dependencies_test.go +++ b/pkg/skaffold/build/custom/dependencies_test.go @@ -88,14 +88,22 @@ func TestGetDependenciesPaths(t *testing.T) { description: "watch everything", paths: []string{"."}, expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"}, - }, { + }, + { description: "watch nothing", - }, { + }, + { description: "ignore some paths", paths: []string{"."}, ignore: []string{"b*"}, expected: []string{"foo"}, - }, { + }, + { + description: "glob", + paths: []string{"**"}, + expected: []string{"bar", filepath.FromSlash("baz/file"), "foo"}, + }, + { description: "error", paths: []string{"unknown"}, shouldErr: true, diff --git a/pkg/skaffold/build/list/list.go b/pkg/skaffold/build/list/list.go new file mode 100644 index 00000000000..66152f9d6ae --- /dev/null +++ b/pkg/skaffold/build/list/list.go @@ -0,0 +1,64 @@ +/* +Copyright 2020 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package list + +import ( + "fmt" + "path/filepath" + "sort" + + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" +) + +// Files list files in a workspace, given a list of patterns and exclusions. +// A pattern that doesn't correspond to any file is an error. +func Files(workspace string, patterns, excludes []string) ([]string, error) { + var paths []string + + for _, pattern := range patterns { + expanded, err := filepath.Glob(filepath.Join(workspace, pattern)) + if err != nil { + return nil, err + } + + if len(expanded) == 0 { + return nil, fmt.Errorf("pattern %q did not match any file", pattern) + } + + for _, e := range expanded { + rel, err := filepath.Rel(workspace, e) + if err != nil { + return nil, err + } + + paths = append(paths, rel) + } + } + + files, err := docker.WalkWorkspace(workspace, excludes, paths) + if err != nil { + return nil, fmt.Errorf("walking workspace %q: %w", workspace, err) + } + + var dependencies []string + for file := range files { + dependencies = append(dependencies, file) + } + + sort.Strings(dependencies) + return dependencies, nil +}