Skip to content

Commit 3212333

Browse files
authored
Merge pull request moby#5174 from tonistiigi/dockerui-local-sessionid
dockerui: allow passing sessionID for specific local source
2 parents ffadc9f + 85f9c41 commit 3212333

File tree

4 files changed

+119
-11
lines changed

4 files changed

+119
-11
lines changed

frontend/dockerfile/dockerfile_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import (
6262
digest "github.com/opencontainers/go-digest"
6363
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
6464
"github.com/pkg/errors"
65+
"github.com/stretchr/testify/assert"
6566
"github.com/stretchr/testify/require"
6667
statuspb "google.golang.org/genproto/googleapis/rpc/status"
6768
)
@@ -197,6 +198,7 @@ var allTests = integration.TestFuncs(
197198
testHistoryError,
198199
testHistoryFinalizeTrace,
199200
testEmptyStages,
201+
testLocalCustomSessionID,
200202
)
201203

202204
// Tests that depend on the `security.*` entitlements
@@ -6073,6 +6075,79 @@ COPY --from=base /o* /
60736075
require.True(t, errors.Is(err, os.ErrNotExist))
60746076
}
60756077

6078+
func testLocalCustomSessionID(t *testing.T, sb integration.Sandbox) {
6079+
integration.SkipOnPlatform(t, "windows")
6080+
ctx := sb.Context()
6081+
6082+
c, err := client.New(ctx, sb.Address())
6083+
require.NoError(t, err)
6084+
defer c.Close()
6085+
6086+
dockerfile := []byte(`
6087+
FROM scratch AS base
6088+
FROM scratch
6089+
COPY out /out1
6090+
COPY --from=base /another /out2
6091+
`)
6092+
6093+
dir := integration.Tmpdir(
6094+
t,
6095+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
6096+
)
6097+
6098+
dir2 := integration.Tmpdir(
6099+
t,
6100+
fstest.CreateFile("out", []byte("contents1"), 0600),
6101+
)
6102+
6103+
dir3 := integration.Tmpdir(
6104+
t,
6105+
fstest.CreateFile("another", []byte("contents2"), 0600),
6106+
)
6107+
6108+
f := getFrontend(t, sb)
6109+
6110+
destDir := t.TempDir()
6111+
6112+
dirs := filesync.NewFSSyncProvider(filesync.StaticDirSource{
6113+
dockerui.DefaultLocalNameDockerfile: dir,
6114+
dockerui.DefaultLocalNameContext: dir2,
6115+
"basedir": dir3,
6116+
})
6117+
6118+
s, err := session.NewSession(ctx, "hint")
6119+
require.NoError(t, err)
6120+
s.Allow(dirs)
6121+
go func() {
6122+
err := s.Run(ctx, c.Dialer())
6123+
assert.NoError(t, err)
6124+
}()
6125+
6126+
_, err = f.Solve(sb.Context(), c, client.SolveOpt{
6127+
FrontendAttrs: map[string]string{
6128+
"context:base": "local:basedir",
6129+
"local-sessionid:" + dockerui.DefaultLocalNameDockerfile: s.ID(),
6130+
"local-sessionid:" + dockerui.DefaultLocalNameContext: s.ID(),
6131+
"local-sessionid:basedir": s.ID(),
6132+
},
6133+
Exports: []client.ExportEntry{
6134+
{
6135+
Type: client.ExporterLocal,
6136+
OutputDir: destDir,
6137+
},
6138+
},
6139+
}, nil)
6140+
require.NoError(t, err)
6141+
6142+
dt, err := os.ReadFile(filepath.Join(destDir, "out1"))
6143+
require.NoError(t, err)
6144+
require.Equal(t, "contents1", string(dt))
6145+
6146+
dt, err = os.ReadFile(filepath.Join(destDir, "out2"))
6147+
require.NoError(t, err)
6148+
require.Equal(t, "contents2", string(dt))
6149+
}
6150+
60766151
func testNamedOCILayoutContext(t *testing.T, sb integration.Sandbox) {
60776152
integration.SkipOnPlatform(t, "windows")
60786153
workers.CheckFeatureCompat(t, sb, workers.FeatureOCIExporter, workers.FeatureOCILayout)

frontend/dockerui/attr.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ func parseSourceDateEpoch(v string) (*time.Time, error) {
125125
return &tm, nil
126126
}
127127

128+
func parseLocalSessionIDs(opt map[string]string) map[string]string {
129+
m := map[string]string{}
130+
for k, v := range opt {
131+
if strings.HasPrefix(k, localSessionIDPrefix) {
132+
m[strings.TrimPrefix(k, localSessionIDPrefix)] = v
133+
}
134+
}
135+
return m
136+
}
137+
128138
func filter(opt map[string]string, key string) map[string]string {
129139
m := map[string]string{}
130140
for k, v := range opt {

frontend/dockerui/config.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import (
2626
)
2727

2828
const (
29-
buildArgPrefix = "build-arg:"
30-
labelPrefix = "label:"
29+
buildArgPrefix = "build-arg:"
30+
labelPrefix = "label:"
31+
localSessionIDPrefix = "local-sessionid:"
3132

3233
keyTarget = "target"
3334
keyCgroupParent = "cgroup-parent"
@@ -79,10 +80,11 @@ type Config struct {
7980

8081
type Client struct {
8182
Config
82-
client client.Client
83-
ignoreCache []string
84-
g flightcontrol.CachedGroup[*buildContext]
85-
bopts client.BuildOpts
83+
client client.Client
84+
ignoreCache []string
85+
g flightcontrol.CachedGroup[*buildContext]
86+
bopts client.BuildOpts
87+
localsSessionIDs map[string]string
8688

8789
dockerignore []byte
8890
dockerignoreName string
@@ -298,6 +300,9 @@ func (bc *Client) init() error {
298300
return errors.Wrapf(err, "failed to parse %s", keyCopyIgnoredCheckEnabled)
299301
}
300302
}
303+
304+
bc.localsSessionIDs = parseLocalSessionIDs(opts)
305+
301306
return nil
302307
}
303308

@@ -331,9 +336,14 @@ func (bc *Client) ReadEntrypoint(ctx context.Context, lang string, opts ...llb.L
331336
filenames = append(filenames, path.Join(path.Dir(bctx.filename), strings.ToLower(DefaultDockerfileName)))
332337
}
333338

339+
sessionID := bc.bopts.SessionID
340+
if v, ok := bc.localsSessionIDs[bctx.dockerfileLocalName]; ok {
341+
sessionID = v
342+
}
343+
334344
opts = append([]llb.LocalOption{
335345
llb.FollowPaths(filenames),
336-
llb.SessionID(bc.bopts.SessionID),
346+
llb.SessionID(sessionID),
337347
llb.SharedKeyHint(bctx.dockerfileLocalName),
338348
WithInternalName(name),
339349
llb.Differ(llb.DiffNone, false),
@@ -427,8 +437,13 @@ func (bc *Client) MainContext(ctx context.Context, opts ...llb.LocalOption) (*ll
427437
return nil, errors.Wrapf(err, "failed to read dockerignore patterns")
428438
}
429439

440+
sessionID := bc.bopts.SessionID
441+
if v, ok := bc.localsSessionIDs[bctx.contextLocalName]; ok {
442+
sessionID = v
443+
}
444+
430445
opts = append([]llb.LocalOption{
431-
llb.SessionID(bc.bopts.SessionID),
446+
llb.SessionID(sessionID),
432447
llb.ExcludePatterns(excludes),
433448
llb.SharedKeyHint(bctx.contextLocalName),
434449
WithInternalName("load build context"),
@@ -500,8 +515,12 @@ func WithInternalName(name string) llb.ConstraintsOpt {
500515

501516
func (bc *Client) dockerIgnorePatterns(ctx context.Context, bctx *buildContext) ([]string, error) {
502517
if bc.dockerignore == nil {
518+
sessionID := bc.bopts.SessionID
519+
if v, ok := bc.localsSessionIDs[bctx.contextLocalName]; ok {
520+
sessionID = v
521+
}
503522
st := llb.Local(bctx.contextLocalName,
504-
llb.SessionID(bc.bopts.SessionID),
523+
llb.SessionID(sessionID),
505524
llb.FollowPaths([]string{DefaultDockerignoreName}),
506525
llb.SharedKeyHint(bctx.contextLocalName+"-"+DefaultDockerignoreName),
507526
WithInternalName("load "+DefaultDockerignoreName),

frontend/dockerui/namedcontext.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,12 @@ func (bc *Client) namedContextRecursive(ctx context.Context, name string, nameWi
187187
}
188188
return &st, &img, nil
189189
case "local":
190+
sessionID := bc.bopts.SessionID
191+
if v, ok := bc.localsSessionIDs[vv[1]]; ok {
192+
sessionID = v
193+
}
190194
st := llb.Local(vv[1],
191-
llb.SessionID(bc.bopts.SessionID),
195+
llb.SessionID(sessionID),
192196
llb.FollowPaths([]string{DefaultDockerignoreName}),
193197
llb.SharedKeyHint("context:"+nameWithPlatform+"-"+DefaultDockerignoreName),
194198
llb.WithCustomName("[context "+nameWithPlatform+"] load "+DefaultDockerignoreName),
@@ -226,7 +230,7 @@ func (bc *Client) namedContextRecursive(ctx context.Context, name string, nameWi
226230
localOutput := &asyncLocalOutput{
227231
name: vv[1],
228232
nameWithPlatform: nameWithPlatform,
229-
sessionID: bc.bopts.SessionID,
233+
sessionID: sessionID,
230234
excludes: excludes,
231235
extraOpts: opt.AsyncLocalOpts,
232236
}

0 commit comments

Comments
 (0)