Skip to content

Commit d315c08

Browse files
authored
Merge pull request #3690 from crazy-max/policy-session
build: reuse build session for policy source resolution
2 parents 08998a7 + 2ed4ece commit d315c08

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

build/opt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func configureSourcePolicy(ctx context.Context, np *noderesolver.ResolvedNode, o
619619
if err != nil {
620620
return nil, err
621621
}
622-
sourceResolver := sourcemeta.NewResolver(c, sourcemeta.WithProgressWriter(pw))
622+
sourceResolver := sourcemeta.NewResolver(c, sourcemeta.WithProgressWriter(pw), sourcemeta.WithSession(so.Session))
623623
defers = []func(error){
624624
func(error) {
625625
_ = sourceResolver.Close()

tests/build.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
4848
testBuildAlias,
4949
testBuildStdin,
5050
testBuildRemote,
51+
testBuildRemoteAuth,
5152
testBuildLocalState,
5253
testBuildLocalStateStdin,
5354
testBuildLocalStateRemote,
@@ -267,6 +268,41 @@ COPY foo /foo
267268
})
268269
}
269270

271+
func testBuildRemoteAuth(t *testing.T, sb integration.Sandbox) {
272+
dockerfile := []byte(`
273+
FROM busybox:latest
274+
COPY foo /foo
275+
`)
276+
dir := tmpdir(
277+
t,
278+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
279+
fstest.CreateFile("foo", []byte("foo"), 0600),
280+
)
281+
dirDest := t.TempDir()
282+
283+
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
284+
require.NoError(t, err)
285+
286+
gittestutil.GitInit(git, t)
287+
gittestutil.GitAdd(git, t, "Dockerfile", "foo")
288+
gittestutil.GitCommit(git, t, "initial commit")
289+
290+
token := identity.NewID()
291+
addr := gittestutil.GitServeHTTP(git, t, gittestutil.WithAccessToken(token))
292+
293+
out, err := buildCmd(sb, withDir(dir),
294+
withEnv("GIT_AUTH_TOKEN="+token),
295+
withArgs(
296+
"--secret", "id=GIT_AUTH_TOKEN,env=GIT_AUTH_TOKEN",
297+
"--output=type=local,dest="+dirDest,
298+
addr,
299+
),
300+
)
301+
require.NoError(t, err, out)
302+
303+
require.FileExists(t, filepath.Join(dirDest, "foo"))
304+
}
305+
270306
func testBuildLocalState(t *testing.T, sb integration.Sandbox) {
271307
dockerfile := []byte(`
272308
FROM busybox:latest AS base

util/sourcemeta/resolver.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sourcemeta
33
import (
44
"context"
55
"errors"
6+
"slices"
67
"sync"
78
"sync/atomic"
89

@@ -11,6 +12,7 @@ import (
1112
"github.com/moby/buildkit/client/llb"
1213
"github.com/moby/buildkit/client/llb/sourceresolver"
1314
gwclient "github.com/moby/buildkit/frontend/gateway/client"
15+
"github.com/moby/buildkit/session"
1416
"github.com/moby/buildkit/solver/pb"
1517
)
1618

@@ -44,6 +46,7 @@ type Option func(*newResolverOpts)
4446

4547
type newResolverOpts struct {
4648
progressWriter progress.Writer
49+
session []session.Attachable
4750
}
4851

4952
func WithProgressWriter(pw progress.Writer) Option {
@@ -52,6 +55,12 @@ func WithProgressWriter(pw progress.Writer) Option {
5255
}
5356
}
5457

58+
func WithSession(session []session.Attachable) Option {
59+
return func(o *newResolverOpts) {
60+
o.session = slices.Clone(session)
61+
}
62+
}
63+
5564
func NewResolver(c *client.Client, opts ...Option) *Resolver {
5665
var cfg newResolverOpts
5766
for _, opt := range opts {
@@ -72,7 +81,14 @@ func NewResolver(c *client.Client, opts ...Option) *Resolver {
7281
}()
7382
}
7483

75-
_, err := c.Build(ctx, client.SolveOpt{Internal: true}, "buildx", func(ctx context.Context, gw gwclient.Client) (*gwclient.Result, error) {
84+
solveOpt := client.SolveOpt{
85+
Internal: true,
86+
}
87+
if len(cfg.session) > 0 {
88+
solveOpt.Session = cfg.session
89+
}
90+
91+
_, err := c.Build(ctx, solveOpt, "buildx", func(ctx context.Context, gw gwclient.Client) (*gwclient.Result, error) {
7692
ready <- gw
7793
<-ctx.Done()
7894
return nil, context.Cause(ctx)

0 commit comments

Comments
 (0)