Merge pull request #3690 from crazy-max/policy-session

build: reuse build session for policy source resolution
This commit is contained in:
Tõnis Tiigi
2026-03-04 09:58:04 -08:00
committed by GitHub
3 changed files with 54 additions and 2 deletions
+1 -1
View File
@@ -619,7 +619,7 @@ func configureSourcePolicy(ctx context.Context, np *noderesolver.ResolvedNode, o
if err != nil { if err != nil {
return nil, err return nil, err
} }
sourceResolver := sourcemeta.NewResolver(c, sourcemeta.WithProgressWriter(pw)) sourceResolver := sourcemeta.NewResolver(c, sourcemeta.WithProgressWriter(pw), sourcemeta.WithSession(so.Session))
defers = []func(error){ defers = []func(error){
func(error) { func(error) {
_ = sourceResolver.Close() _ = sourceResolver.Close()
+36
View File
@@ -48,6 +48,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
testBuildAlias, testBuildAlias,
testBuildStdin, testBuildStdin,
testBuildRemote, testBuildRemote,
testBuildRemoteAuth,
testBuildLocalState, testBuildLocalState,
testBuildLocalStateStdin, testBuildLocalStateStdin,
testBuildLocalStateRemote, testBuildLocalStateRemote,
@@ -267,6 +268,41 @@ COPY foo /foo
}) })
} }
func testBuildRemoteAuth(t *testing.T, sb integration.Sandbox) {
dockerfile := []byte(`
FROM busybox:latest
COPY foo /foo
`)
dir := tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("foo", []byte("foo"), 0600),
)
dirDest := t.TempDir()
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
require.NoError(t, err)
gittestutil.GitInit(git, t)
gittestutil.GitAdd(git, t, "Dockerfile", "foo")
gittestutil.GitCommit(git, t, "initial commit")
token := identity.NewID()
addr := gittestutil.GitServeHTTP(git, t, gittestutil.WithAccessToken(token))
out, err := buildCmd(sb, withDir(dir),
withEnv("GIT_AUTH_TOKEN="+token),
withArgs(
"--secret", "id=GIT_AUTH_TOKEN,env=GIT_AUTH_TOKEN",
"--output=type=local,dest="+dirDest,
addr,
),
)
require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "foo"))
}
func testBuildLocalState(t *testing.T, sb integration.Sandbox) { func testBuildLocalState(t *testing.T, sb integration.Sandbox) {
dockerfile := []byte(` dockerfile := []byte(`
FROM busybox:latest AS base FROM busybox:latest AS base
+17 -1
View File
@@ -3,6 +3,7 @@ package sourcemeta
import ( import (
"context" "context"
"errors" "errors"
"slices"
"sync" "sync"
"sync/atomic" "sync/atomic"
@@ -11,6 +12,7 @@ import (
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/client/llb/sourceresolver" "github.com/moby/buildkit/client/llb/sourceresolver"
gwclient "github.com/moby/buildkit/frontend/gateway/client" gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/solver/pb" "github.com/moby/buildkit/solver/pb"
) )
@@ -44,6 +46,7 @@ type Option func(*newResolverOpts)
type newResolverOpts struct { type newResolverOpts struct {
progressWriter progress.Writer progressWriter progress.Writer
session []session.Attachable
} }
func WithProgressWriter(pw progress.Writer) Option { func WithProgressWriter(pw progress.Writer) Option {
@@ -52,6 +55,12 @@ func WithProgressWriter(pw progress.Writer) Option {
} }
} }
func WithSession(session []session.Attachable) Option {
return func(o *newResolverOpts) {
o.session = slices.Clone(session)
}
}
func NewResolver(c *client.Client, opts ...Option) *Resolver { func NewResolver(c *client.Client, opts ...Option) *Resolver {
var cfg newResolverOpts var cfg newResolverOpts
for _, opt := range opts { for _, opt := range opts {
@@ -72,7 +81,14 @@ func NewResolver(c *client.Client, opts ...Option) *Resolver {
}() }()
} }
_, err := c.Build(ctx, client.SolveOpt{Internal: true}, "buildx", func(ctx context.Context, gw gwclient.Client) (*gwclient.Result, error) { solveOpt := client.SolveOpt{
Internal: true,
}
if len(cfg.session) > 0 {
solveOpt.Session = cfg.session
}
_, err := c.Build(ctx, solveOpt, "buildx", func(ctx context.Context, gw gwclient.Client) (*gwclient.Result, error) {
ready <- gw ready <- gw
<-ctx.Done() <-ctx.Done()
return nil, context.Cause(ctx) return nil, context.Cause(ctx)