Skip to content

Commit 7fec6aa

Browse files
committed
Do not make hidden directory inside
1 parent fec54f1 commit 7fec6aa

6 files changed

Lines changed: 54 additions & 115 deletions

File tree

pkg/build/build.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/moby/buildkit/client"
3131
"github.com/moby/buildkit/cmd/buildctl/build"
3232
"github.com/moby/buildkit/session"
33-
"github.com/sirupsen/logrus"
3433
"github.com/tonistiigi/go-csvvalue"
3534
"google.golang.org/grpc"
3635
)
@@ -51,7 +50,6 @@ func Build(ctx context.Context, opts *BOpts) error {
5150

5251
buildkit, err := client.New(ctx, "", clientOpts...)
5352
if err != nil {
54-
logrus.Debugf("failed to connect to buildkit")
5553
return err
5654
}
5755
defer buildkit.Close()
@@ -135,8 +133,8 @@ func Build(ctx context.Context, opts *BOpts) error {
135133
KeyContentStoreName: opts.ContentStore,
136134
}
137135

138-
if opts.HiddenDirName != "" {
139-
solveOpt.FrontendAttrs["filename"] = filepath.Join(opts.HiddenDirName, "Dockerfile")
136+
if opts.HiddenDockerDir != "" {
137+
solveOpt.FrontendAttrs["filename"] = filepath.Join(opts.HiddenDockerDir, "Dockerfile")
140138
}
141139

142140
if opts.NoCache {

pkg/build/buildopts.go

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ import (
2020
"bytes"
2121
"context"
2222
"encoding/base64"
23-
"fmt"
2423
"path/filepath"
2524
"strings"
2625

2726
"github.com/containerd/platforms"
28-
"github.com/google/uuid"
2927
"github.com/moby/buildkit/frontend/dockerfile/instructions"
3028
"github.com/moby/buildkit/frontend/dockerfile/parser"
3129
"github.com/moby/buildkit/util/progress/progresswriter"
@@ -43,8 +41,9 @@ const (
4341
KeyContentStoreName = "container"
4442
// Base64-encoded Dockerfile contents.
4543
KeyDockerfile = "dockerfile"
46-
// Base64-encoded docker specific ignore file contents.
47-
KeyDockerignore = "dockerignore"
44+
// Hidden directory for the dockerfile to be placed.
45+
// Used when docker specific ignore file is found.
46+
KeyHiddenDockerDir = "hidden-docker-dir"
4847
// Image reference (name:tag) to assign to the built image.
4948
KeyTag = "tag"
5049
// Target platforms to build the image for.
@@ -78,21 +77,21 @@ const (
7877
var keyBOpts = struct{}{}
7978

8079
type BOpts struct {
81-
BuildID string
82-
Dockerfile []byte
83-
Tag string
84-
ContextDir string
85-
HiddenDirName string
86-
BuildPlatforms []ocispecs.Platform
87-
Platforms []ocispecs.Platform
88-
NoCache bool
89-
Target string
90-
BuildArgs map[string]string
91-
CacheIn []string
92-
CacheOut []string
93-
Outputs []string
94-
Labels map[string]string
95-
ProgressWriter progresswriter.Writer
80+
BuildID string
81+
Dockerfile []byte
82+
Tag string
83+
ContextDir string
84+
HiddenDockerDir string
85+
BuildPlatforms []ocispecs.Platform
86+
Platforms []ocispecs.Platform
87+
NoCache bool
88+
Target string
89+
BuildArgs map[string]string
90+
CacheIn []string
91+
CacheOut []string
92+
Outputs []string
93+
Labels map[string]string
94+
ProgressWriter progresswriter.Writer
9695

9796
ContentStore *content.ContentStoreProxy
9897
Resolver *resolver.ResolverProxy
@@ -126,19 +125,7 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
126125
return nil, err
127126
}
128127

129-
var dockerignoreBytes = []byte(nil)
130-
var hiddenDirName = ""
131-
132-
dockerignoreBase64Bytes, ok := first(KeyDockerignore)
133-
if ok {
134-
dockerignoreBytes, err = base64.StdEncoding.DecodeString(dockerignoreBase64Bytes)
135-
if err != nil {
136-
return nil, err
137-
}
138-
139-
hiddenDirName = ".tmp-" + uuid.NewString()
140-
dockerignoreBytes = append(dockerignoreBytes, fmt.Sprintf("\n%s", hiddenDirName)...)
141-
}
128+
hiddenDockerDir, _ := first(KeyHiddenDockerDir)
142129

143130
progress, ok := first(KeyProgress)
144131
if !ok {
@@ -278,7 +265,7 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
278265
}
279266
}
280267

281-
fssyncProxy, err := fssync.NewFSSyncProxy(".", basePath, hiddenDirName, dockerfileBytes, dockerignoreBytes, addedGlobs)
268+
fssyncProxy, err := fssync.NewFSSyncProxy(".", basePath, addedGlobs)
282269
if err != nil {
283270
return nil, err
284271
}
@@ -289,26 +276,26 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
289276
}
290277

291278
bopts := &BOpts{
292-
BuildID: buildID,
293-
Dockerfile: dockerfileBytes,
294-
Tag: tag,
295-
BuildPlatforms: bps,
296-
Platforms: pls,
297-
ContextDir: ctxDir,
298-
HiddenDirName: hiddenDirName,
299-
ContentStore: contentProxy,
300-
FSSync: fssyncProxy,
301-
NoCache: noCache,
302-
Resolver: resolver.NewResolverProxy(),
303-
ProgressWriter: pw,
304-
Stdio: stdioProxy,
305-
Target: target,
306-
Labels: labels,
307-
BuildArgs: buildArgs,
308-
CacheIn: cacheIn,
309-
CacheOut: cacheOut,
310-
Outputs: outputs,
311-
basePath: filepath.Join(basePath, buildID),
279+
BuildID: buildID,
280+
Dockerfile: dockerfileBytes,
281+
Tag: tag,
282+
BuildPlatforms: bps,
283+
Platforms: pls,
284+
ContextDir: ctxDir,
285+
HiddenDockerDir: hiddenDockerDir,
286+
ContentStore: contentProxy,
287+
FSSync: fssyncProxy,
288+
NoCache: noCache,
289+
Resolver: resolver.NewResolverProxy(),
290+
ProgressWriter: pw,
291+
Stdio: stdioProxy,
292+
Target: target,
293+
Labels: labels,
294+
BuildArgs: buildArgs,
295+
CacheIn: cacheIn,
296+
CacheOut: cacheOut,
297+
Outputs: outputs,
298+
basePath: filepath.Join(basePath, buildID),
312299
}
313300

314301
return bopts, nil

pkg/fileutils/tarxfer.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ func NewTarReceiver(cacheBase string, demux *stream.Demultiplexer) *Receiver {
4040
return &Receiver{demux: demux, cacheBase: cacheBase}
4141
}
4242

43-
func (r *Receiver) Receive(
44-
ctx context.Context,
45-
hiddenDirName string,
46-
dockerfileBytes []byte,
47-
dockerignoreBytes []byte,
48-
fn fs.WalkDirFunc) (string, error) {
49-
43+
func (r *Receiver) Receive(ctx context.Context, fn fs.WalkDirFunc) (string, error) {
5044
errCh := make(chan error, 1)
5145
hashCh := make(chan string, 1)
5246
dataCh := make(chan []byte)
@@ -91,36 +85,6 @@ func (r *Receiver) Receive(
9185
_ = os.Remove(tarFile)
9286
}
9387

94-
if hiddenDirName != "" {
95-
dockerfilePath := filepath.Join(cacheDir, filepath.Join(hiddenDirName, "Dockerfile"))
96-
dockerignorePath := filepath.Join(cacheDir, filepath.Join(hiddenDirName, "Dockerfile.dockerignore"))
97-
98-
if err := os.MkdirAll(filepath.Dir(dockerfilePath), 0o755); err != nil {
99-
return "", err
100-
}
101-
102-
dockerfile, err := os.OpenFile(dockerfilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
103-
if err != nil {
104-
return "", err
105-
}
106-
if _, err := dockerfile.Write(dockerfileBytes); err != nil {
107-
_ = dockerfile.Close()
108-
return "", err
109-
}
110-
defer dockerfile.Close()
111-
112-
dockerignore, err := os.OpenFile(dockerignorePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
113-
if err != nil {
114-
return "", err
115-
}
116-
defer dockerignore.Close()
117-
118-
if _, err := dockerignore.Write(dockerignoreBytes); err != nil {
119-
_ = dockerignore.Close()
120-
return "", err
121-
}
122-
}
123-
12488
return checksum, filepath.Walk(cacheDir, func(p string, info os.FileInfo, _ error) error {
12589
rel, err := filepath.Rel(cacheDir, p)
12690
if err != nil || rel == "." {

pkg/fileutils/tarxfer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestReceiver_Receive_Success(t *testing.T) {
9595
return nil
9696
}
9797

98-
checksum, err := r.Receive(ctx, "", nil, nil, walkFn)
98+
checksum, err := r.Receive(ctx, walkFn)
9999
if err != nil {
100100
t.Fatalf("Receive returned error: %v", err)
101101
}
@@ -125,7 +125,7 @@ func TestReceiver_Receive_ServerError(t *testing.T) {
125125
tmpDir := t.TempDir()
126126
r := NewTarReceiver(tmpDir, demux)
127127

128-
_, err := r.Receive(ctx, "", nil, nil, func(string, fs.DirEntry, error) error { return nil })
128+
_, err := r.Receive(ctx, func(string, fs.DirEntry, error) error { return nil })
129129
if err == nil {
130130
t.Fatalf("expected server error, got nil")
131131
}

pkg/fssync/fssync.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,14 @@ type FSSyncProxy struct {
4545
contextDir string
4646
basePath string
4747

48-
hiddenDirName string
49-
dockerfile []byte
50-
dockerignore []byte
51-
5248
addedGlobs []string
5349
}
5450

55-
func NewFSSyncProxy(
56-
contextDir string, basePath string, hiddenDirName string,
57-
dockerfile []byte, dockerignore []byte, addedGlobs []string) (*FSSyncProxy, error) {
51+
func NewFSSyncProxy(contextDir string, basePath string, addedGlobs []string) (*FSSyncProxy, error) {
5852

5953
f := new(FSSyncProxy)
6054
f.contextDir = contextDir
6155
f.basePath = filepath.Join(basePath, f.String())
62-
f.hiddenDirName = hiddenDirName
63-
f.dockerfile = dockerfile
64-
f.dockerignore = dockerignore
6556
f.addedGlobs = addedGlobs
6657
return f, nil
6758
}

pkg/fssync/walk.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,14 @@ func (f *FS) Walk(ctx context.Context, target string, fn fs.WalkDirFunc) error {
124124
switch walkMeta.Mode {
125125
case ModeTAR:
126126
receiver := fileutils.NewTarReceiver(f.fsPath, demux)
127-
checksum, err := receiver.Receive(ctx, f.proxy.hiddenDirName, f.proxy.dockerfile, f.proxy.dockerignore,
128-
func(path string, d fs.DirEntry, err error) error {
129-
excluded, err := excludeMatcher.MatchesOrParentMatches(path)
130-
if excluded {
131-
return nil
132-
}
133-
134-
return fn(path, d, err)
135-
})
127+
checksum, err := receiver.Receive(ctx, func(path string, d fs.DirEntry, err error) error {
128+
excluded, err := excludeMatcher.MatchesOrParentMatches(path)
129+
if excluded {
130+
return nil
131+
}
132+
133+
return fn(path, d, err)
134+
})
136135
if err != nil {
137136
return err
138137
}

0 commit comments

Comments
 (0)