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 dockerfile resolution #4260

Merged
merged 3 commits into from
May 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkg/skaffold/docker/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"path/filepath"
"sort"
"strings"

"github.com/docker/docker/builder/dockerignore"

Expand All @@ -31,14 +30,16 @@ import (

// NormalizeDockerfilePath returns the absolute path to the dockerfile.
func NormalizeDockerfilePath(context, dockerfile string) (string, error) {
if filepath.IsAbs(dockerfile) {
return dockerfile, nil
}

if !strings.HasPrefix(dockerfile, context) {
dockerfile = filepath.Join(context, dockerfile)
// Expected case: should be found relative to the context directory.
// If it does not exist, check if it's found relative to the current directory in case it's shared.
// Otherwise return the path relative to the context directory, where it should have been.
rel := filepath.Join(context, dockerfile)
if _, err := os.Stat(rel); os.IsNotExist(err) {
if _, err := os.Stat(dockerfile); err == nil || !os.IsNotExist(err) {
return filepath.Abs(dockerfile)
}
}
return filepath.Abs(dockerfile)
return filepath.Abs(rel)
}

// GetDependencies finds the sources dependencies for the given docker artifact.
Expand Down
66 changes: 66 additions & 0 deletions pkg/skaffold/docker/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package docker
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -559,3 +560,68 @@ func TestGetDependencies(t *testing.T) {
})
}
}

func TestNormalizeDockerfilePath(t *testing.T) {
tests := []struct {
description string
files []string
dockerfile string

expected string // relative path
}{
{
description: "dockerfile found in context",
files: []string{"Dockerfile", "context/Dockerfile"},
dockerfile: "Dockerfile",
expected: "context/Dockerfile",
},
{
description: "path to dockerfile resolved in context first",
files: []string{"context/context/Dockerfile", "context/Dockerfile"},
dockerfile: "context/Dockerfile",
expected: "context/context/Dockerfile",
},
{
description: "path to dockerfile in working directory",
files: []string{"context/Dockerfile"},
dockerfile: "context/Dockerfile",
expected: "context/Dockerfile",
},
{
description: "workspace dockerfile when missing in context",
files: []string{"Dockerfile", "context/randomfile.txt"},
dockerfile: "Dockerfile",
expected: "Dockerfile",
},
{
description: "explicit dockerfile path",
files: []string{"context/Dockerfile", "elsewhere/Dockerfile"},
dockerfile: "elsewhere/Dockerfile",
expected: "elsewhere/Dockerfile",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
d := t.NewTempDir()
t.Chdir(d.Root())

d.Mkdir("context")
d.Touch(test.files...)

f, err := NormalizeDockerfilePath(d.Path("context"), test.dockerfile)
t.CheckError(false, err)
checkSameFile(t, d.Path(test.expected), f)
})
}
}

func checkSameFile(t *testutil.T, expected, result string) {
t.Helper()
i1, err := os.Stat(expected)
t.CheckError(false, err)
i2, err := os.Stat(result)
t.CheckError(false, err)
if !os.SameFile(i1, i2) {
t.Errorf("returned wrong file\n got: %s\nwanted: %s", result, expected)
}
}