Skip to content

Commit

Permalink
Normalize docker file path
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jul 23, 2018
1 parent 93be2cf commit d83d107
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
14 changes: 2 additions & 12 deletions cmd/skaffold/app/cmd/docker/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package docker
import (
"io"
"path/filepath"
"strings"

cmdutil "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/util"
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags"
Expand Down Expand Up @@ -63,7 +62,7 @@ func runDeps(out io.Writer, filename, dockerfile, context string) error {
return errors.Wrap(err, "parsing skaffold config")
}
// normalize the provided dockerfile path WRT to the context
normalizedPath, err := normalizeDockerfilePath(dockerfile, context)
normalizedPath, err := docker.NormalizeDockerfilePath(context, dockerfile)
if err != nil {
return errors.Wrap(err, "normalizing dockerfile path")
}
Expand All @@ -79,22 +78,13 @@ func runDeps(out io.Writer, filename, dockerfile, context string) error {
return nil
}

func normalizeDockerfilePath(dockerfile, context string) (string, error) {
if !filepath.IsAbs(dockerfile) {
if !strings.HasPrefix(dockerfile, context) {
dockerfile = filepath.Join(context, dockerfile)
}
}
return filepath.Abs(dockerfile)
}

func getBuildArgsForDockerfile(config *config.SkaffoldConfig, dockerfile string) map[string]*string {
var err error
for _, artifact := range config.Build.Artifacts {
if artifact.DockerArtifact != nil {
artifactPath := artifact.DockerArtifact.DockerfilePath
if artifact.Workspace != "" {
artifactPath, err = normalizeDockerfilePath(artifactPath, artifact.Workspace)
artifactPath, err = docker.NormalizeDockerfilePath(artifact.Workspace, artifactPath)
if err != nil {
logrus.Warnf("normalizing artifact dockerfile path: %s\n", err.Error())
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/skaffold/build/local/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"os"
"os/exec"
"path/filepath"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
Expand All @@ -35,12 +34,12 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri
initialTag := util.RandomID()

if b.cfg.UseDockerCLI || b.cfg.UseBuildkit {
absDockerfilePath := a.DockerfilePath
if !filepath.IsAbs(a.DockerfilePath) {
absDockerfilePath = filepath.Join(workspace, a.DockerfilePath)
dockerfilePath, err := docker.NormalizeDockerfilePath(workspace, a.DockerfilePath)
if err != nil {
return "", errors.Wrap(err, "normalizing dockerfile path")
}

args := []string{"build", workspace, "--file", absDockerfilePath, "-t", initialTag}
args := []string{"build", workspace, "--file", dockerfilePath, "-t", initialTag}
args = append(args, docker.GetBuildArgs(a)...)
for _, from := range a.CacheFrom {
args = append(args, "--cache-from", from)
Expand All @@ -53,8 +52,7 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri
cmd.Stdout = out
cmd.Stderr = out

err := util.RunCmd(cmd)
if err != nil {
if err := util.RunCmd(cmd); err != nil {
return "", errors.Wrap(err, "running build")
}
} else {
Expand Down
14 changes: 14 additions & 0 deletions pkg/skaffold/docker/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ package docker
import (
"context"
"io"
"path/filepath"
"strings"

cstorage "cloud.google.com/go/storage"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
)

// NormalizeDockerfilePath returns the abolute 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)
}
return filepath.Abs(dockerfile)
}

func CreateDockerTarContext(buildArgs map[string]*string, w io.Writer, context, dockerfilePath string) error {
paths, err := GetDependencies(buildArgs, context, dockerfilePath)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/docker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func readDockerfile(workspace, absDockerfilePath string, buildArgs map[string]*s
}

func GetDependencies(buildArgs map[string]*string, workspace, dockerfilePath string) ([]string, error) {
absDockerfilePath := dockerfilePath
if !filepath.IsAbs(dockerfilePath) {
absDockerfilePath = filepath.Join(workspace, dockerfilePath)
absDockerfilePath, err := NormalizeDockerfilePath(workspace, dockerfilePath)
if err != nil {
return nil, errors.Wrap(err, "normalizing dockerfile path")
}

deps, err := readDockerfile(workspace, absDockerfilePath, buildArgs)
Expand Down

0 comments on commit d83d107

Please sign in to comment.