Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func Build(ctx context.Context, opts *BOpts) error {
KeyContentStoreName: opts.ContentStore,
}

if opts.HiddenDirName != "" {
solveOpt.FrontendAttrs["filename"] = filepath.Join(opts.HiddenDirName, "Dockerfile")
if opts.HiddenDockerDir != "" {
solveOpt.FrontendAttrs["filename"] = filepath.Join(opts.HiddenDockerDir, "Dockerfile")
}

if opts.NoCache {
Expand Down
97 changes: 42 additions & 55 deletions pkg/build/buildopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
"bytes"
"context"
"encoding/base64"
"fmt"
"path/filepath"
"strings"

"github.com/containerd/platforms"
"github.com/google/uuid"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/util/progress/progresswriter"
Expand All @@ -43,8 +41,9 @@ const (
KeyContentStoreName = "container"
// Base64-encoded Dockerfile contents.
KeyDockerfile = "dockerfile"
// Base64-encoded docker specific ignore file contents.
KeyDockerignore = "dockerignore"
// Hidden directory for the dockerfile and dockerignore to be placed.
// This is provided when docker specific ignore file is found, which might live outside the build context.
KeyHiddenDockerDir = "hidden-docker-dir"
// Image reference (name:tag) to assign to the built image.
KeyTag = "tag"
// Target platforms to build the image for.
Expand Down Expand Up @@ -80,22 +79,22 @@ const (
var keyBOpts = struct{}{}

type BOpts struct {
BuildID string
Dockerfile []byte
Tag string
ContextDir string
HiddenDirName string
BuildPlatforms []ocispecs.Platform
Platforms []ocispecs.Platform
NoCache bool
Target string
BuildArgs map[string]string
Secrets map[string][]byte
CacheIn []string
CacheOut []string
Outputs []string
Labels map[string]string
ProgressWriter progresswriter.Writer
BuildID string
Dockerfile []byte
Tag string
ContextDir string
HiddenDockerDir string
BuildPlatforms []ocispecs.Platform
Platforms []ocispecs.Platform
NoCache bool
Target string
BuildArgs map[string]string
Secrets map[string][]byte
CacheIn []string
CacheOut []string
Outputs []string
Labels map[string]string
ProgressWriter progresswriter.Writer

ContentStore *content.ContentStoreProxy
Resolver *resolver.ResolverProxy
Expand Down Expand Up @@ -129,19 +128,7 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
return nil, err
}

var dockerignoreBytes = []byte(nil)
var hiddenDirName = ""

dockerignoreBase64Bytes, ok := first(KeyDockerignore)
if ok {
dockerignoreBytes, err = base64.StdEncoding.DecodeString(dockerignoreBase64Bytes)
if err != nil {
return nil, err
}

hiddenDirName = ".tmp-" + uuid.NewString()
dockerignoreBytes = append(dockerignoreBytes, fmt.Sprintf("\n%s", hiddenDirName)...)
}
hiddenDockerDir, _ := first(KeyHiddenDockerDir)

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

fssyncProxy, err := fssync.NewFSSyncProxy(".", basePath, hiddenDirName, dockerfileBytes, dockerignoreBytes, addedGlobs)
fssyncProxy, err := fssync.NewFSSyncProxy(".", basePath, addedGlobs)
if err != nil {
return nil, err
}
Expand All @@ -317,27 +304,27 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
}

bopts := &BOpts{
BuildID: buildID,
Dockerfile: dockerfileBytes,
Tag: tag,
BuildPlatforms: bps,
Platforms: pls,
ContextDir: ctxDir,
HiddenDirName: hiddenDirName,
ContentStore: contentProxy,
FSSync: fssyncProxy,
NoCache: noCache,
Resolver: resolver.NewResolverProxy(),
ProgressWriter: pw,
Stdio: stdioProxy,
Target: target,
Labels: labels,
BuildArgs: buildArgs,
Secrets: secrets,
CacheIn: cacheIn,
CacheOut: cacheOut,
Outputs: outputs,
basePath: filepath.Join(basePath, buildID),
BuildID: buildID,
Dockerfile: dockerfileBytes,
Tag: tag,
BuildPlatforms: bps,
Platforms: pls,
ContextDir: ctxDir,
HiddenDockerDir: hiddenDockerDir,
ContentStore: contentProxy,
FSSync: fssyncProxy,
NoCache: noCache,
Resolver: resolver.NewResolverProxy(),
ProgressWriter: pw,
Stdio: stdioProxy,
Target: target,
Labels: labels,
BuildArgs: buildArgs,
Secrets: secrets,
CacheIn: cacheIn,
CacheOut: cacheOut,
Outputs: outputs,
basePath: filepath.Join(basePath, buildID),
}

return bopts, nil
Expand Down
38 changes: 1 addition & 37 deletions pkg/fileutils/tarxfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ func NewTarReceiver(cacheBase string, demux *stream.Demultiplexer) *Receiver {
return &Receiver{demux: demux, cacheBase: cacheBase}
}

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

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

if hiddenDirName != "" {
dockerfilePath := filepath.Join(cacheDir, filepath.Join(hiddenDirName, "Dockerfile"))
dockerignorePath := filepath.Join(cacheDir, filepath.Join(hiddenDirName, "Dockerfile.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, "", nil, nil, walkFn)
checksum, err := r.Receive(ctx, 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, "", nil, nil, func(string, fs.DirEntry, error) error { return nil })
_, err := r.Receive(ctx, func(string, fs.DirEntry, error) error { return nil })
if err == nil {
t.Fatalf("expected server error, got nil")
}
Expand Down
11 changes: 1 addition & 10 deletions pkg/fssync/fssync.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,14 @@ type FSSyncProxy struct {
contextDir string
basePath string

hiddenDirName string
dockerfile []byte
dockerignore []byte

addedGlobs []string
}

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

f := new(FSSyncProxy)
f.contextDir = contextDir
f.basePath = filepath.Join(basePath, f.String())
f.hiddenDirName = hiddenDirName
f.dockerfile = dockerfile
f.dockerignore = dockerignore
f.addedGlobs = addedGlobs
return f, nil
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/fssync/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ 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, f.proxy.hiddenDirName, 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)
})
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)
})
if err != nil {
return err
}
Expand Down
Loading