Skip to content
Open
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
5 changes: 5 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func Build(ctx context.Context, opts *BOpts) error {
solveOpt.OCIStores = map[string]content.Store{
KeyContentStoreName: opts.ContentStore,
}

if opts.DockerfilePath != "" {
solveOpt.FrontendAttrs["filename"] = opts.DockerfilePath
}

if opts.NoCache {
solveOpt.FrontendAttrs["no-cache"] = ""
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/build/buildopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
const (
KeyContentStoreName = "container"
KeyDockerfile = "dockerfile"
KeyDockerfilePath = "dockerfile-path"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is KeyDockerignore not needed also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I haven't updated the code. Thank you!

KeyDockerignore = "dockerignore"
KeyTag = "tag"
KeyPlatforms = "platforms"
KeyProgress = "progress"
Expand All @@ -62,6 +64,7 @@ var keyBOpts = struct{}{}
type BOpts struct {
BuildID string
Dockerfile []byte
DockerfilePath string
Tag string
ContextDir string
BuildPlatforms []ocispecs.Platform
Expand Down Expand Up @@ -108,6 +111,18 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
return nil, err
}

dockerfilePath, _ := first(KeyDockerfilePath)

dockerignoreBase64Bytes, ok := first(KeyDockerignore)

var dockerignoreBytes = []byte(nil)
if ok {
dockerignoreBytes, err = base64.StdEncoding.DecodeString(dockerignoreBase64Bytes)
if err != nil {
dockerignoreBytes = nil
}
}

progress, ok := first(KeyProgress)
if !ok {
progress = "auto"
Expand Down Expand Up @@ -246,7 +261,7 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
}
}

fssyncProxy, err := fssync.NewFSSyncProxy(".", basePath, addedGlobs)
fssyncProxy, err := fssync.NewFSSyncProxy(".", basePath, dockerfilePath, dockerfileBytes, dockerignoreBytes, addedGlobs)
if err != nil {
return nil, err
}
Expand All @@ -259,6 +274,7 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
bopts := &BOpts{
BuildID: buildID,
Dockerfile: dockerfileBytes,
DockerfilePath: dockerfilePath,
Tag: tag,
BuildPlatforms: bps,
Platforms: pls,
Expand Down
38 changes: 37 additions & 1 deletion pkg/fileutils/tarxfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ func NewTarReceiver(cacheBase string, demux *stream.Demultiplexer) *Receiver {
return &Receiver{demux: demux, cacheBase: cacheBase}
}

func (r *Receiver) Receive(ctx context.Context, fn fs.WalkDirFunc) (string, error) {
func (r *Receiver) Receive(
ctx context.Context,
dockerfilePathInContext string,
dockerfileBytes []byte,
dockerignoreBytes []byte,
fn fs.WalkDirFunc) (string, error) {

errCh := make(chan error, 1)
hashCh := make(chan string, 1)
dataCh := make(chan []byte)
Expand Down Expand Up @@ -85,6 +91,36 @@ func (r *Receiver) Receive(ctx context.Context, fn fs.WalkDirFunc) (string, erro
_ = os.Remove(tarFile)
}

if dockerfilePathInContext != "" && dockerignoreBytes != nil {
dockerfilePath := filepath.Join(cacheDir, dockerfilePathInContext)
dockerignorePath := filepath.Join(cacheDir, dockerfilePathInContext+".dockerignore")

if err := os.MkdirAll(filepath.Dir(dockerfilePath), 0o755); err != nil {
return "", err
}

dockerfile, err := os.OpenFile(dockerfilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return "", err
}
if _, err := dockerfile.Write(dockerfileBytes); err != nil {
_ = dockerfile.Close()
return "", err
}
defer dockerfile.Close()

dockerignore, err := os.OpenFile(dockerignorePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return "", err
}
defer dockerignore.Close()

if _, err := dockerignore.Write(dockerignoreBytes); err != nil {
_ = dockerignore.Close()
return "", err
}
}

return checksum, filepath.Walk(cacheDir, func(p string, info os.FileInfo, _ error) error {
rel, err := filepath.Rel(cacheDir, p)
if err != nil || rel == "." {
Expand Down
4 changes: 2 additions & 2 deletions pkg/fileutils/tarxfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestReceiver_Receive_Success(t *testing.T) {
return nil
}

checksum, err := r.Receive(ctx, walkFn)
checksum, err := r.Receive(ctx, "", nil, nil, walkFn)
if err != nil {
t.Fatalf("Receive returned error: %v", err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestReceiver_Receive_ServerError(t *testing.T) {
tmpDir := t.TempDir()
r := NewTarReceiver(tmpDir, demux)

_, err := r.Receive(ctx, func(string, fs.DirEntry, error) error { return nil })
_, err := r.Receive(ctx, "", nil, nil, func(string, fs.DirEntry, error) error { return nil })
if err == nil {
t.Fatalf("expected server error, got nil")
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/fssync/fssync.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ type FSSyncProxy struct {
contextDir string
basePath string

dockerfilePath string
dockerfile []byte
dockerignore []byte

addedGlobs []string
}

func NewFSSyncProxy(contextDir string, basePath string, addedGlobs []string) (*FSSyncProxy, error) {
func NewFSSyncProxy(
contextDir string, basePath string, dockerfilePath string,
dockerfile []byte, dockerignore []byte, addedGlobs []string) (*FSSyncProxy, error) {

f := new(FSSyncProxy)
f.contextDir = contextDir
f.basePath = filepath.Join(basePath, f.String())
f.dockerfilePath = dockerfilePath
f.dockerfile = dockerfile
f.dockerignore = dockerignore
f.addedGlobs = addedGlobs
return f, nil
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/fssync/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,15 @@ func (f *FS) Walk(ctx context.Context, target string, fn fs.WalkDirFunc) error {
switch walkMeta.Mode {
case ModeTAR:
receiver := fileutils.NewTarReceiver(f.fsPath, demux)
checksum, err := receiver.Receive(ctx, func(path string, d fs.DirEntry, err error) error {
excluded, err := excludeMatcher.MatchesOrParentMatches(path)
if excluded {
return nil
}

return fn(path, d, err)
})
checksum, err := receiver.Receive(ctx, f.proxy.dockerfilePath, f.proxy.dockerfile, f.proxy.dockerignore,
func(path string, d fs.DirEntry, err error) error {
excluded, err := excludeMatcher.MatchesOrParentMatches(path)
if excluded {
return nil
}

return fn(path, d, err)
})
if err != nil {
return err
}
Expand Down
Loading