Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerui: allow passing sessionID for specific local source #5174

Merged
merged 2 commits into from
Jul 24, 2024
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
75 changes: 75 additions & 0 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
statuspb "google.golang.org/genproto/googleapis/rpc/status"
)
Expand Down Expand Up @@ -197,6 +198,7 @@ var allTests = integration.TestFuncs(
testHistoryError,
testHistoryFinalizeTrace,
testEmptyStages,
testLocalCustomSessionID,
)

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

func testLocalCustomSessionID(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
ctx := sb.Context()

c, err := client.New(ctx, sb.Address())
require.NoError(t, err)
defer c.Close()

dockerfile := []byte(`
FROM scratch AS base
FROM scratch
COPY out /out1
COPY --from=base /another /out2
`)

dir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
)

dir2 := integration.Tmpdir(
t,
fstest.CreateFile("out", []byte("contents1"), 0600),
)

dir3 := integration.Tmpdir(
t,
fstest.CreateFile("another", []byte("contents2"), 0600),
)

f := getFrontend(t, sb)

destDir := t.TempDir()

dirs := filesync.NewFSSyncProvider(filesync.StaticDirSource{
dockerui.DefaultLocalNameDockerfile: dir,
dockerui.DefaultLocalNameContext: dir2,
"basedir": dir3,
})

s, err := session.NewSession(ctx, "hint")
require.NoError(t, err)
s.Allow(dirs)
go func() {
err := s.Run(ctx, c.Dialer())
assert.NoError(t, err)
}()

_, err = f.Solve(sb.Context(), c, client.SolveOpt{
FrontendAttrs: map[string]string{
"context:base": "local:basedir",
"local-sessionid:" + dockerui.DefaultLocalNameDockerfile: s.ID(),
"local-sessionid:" + dockerui.DefaultLocalNameContext: s.ID(),
"local-sessionid:basedir": s.ID(),
},
Exports: []client.ExportEntry{
{
Type: client.ExporterLocal,
OutputDir: destDir,
},
},
}, nil)
require.NoError(t, err)

dt, err := os.ReadFile(filepath.Join(destDir, "out1"))
require.NoError(t, err)
require.Equal(t, "contents1", string(dt))

dt, err = os.ReadFile(filepath.Join(destDir, "out2"))
require.NoError(t, err)
require.Equal(t, "contents2", string(dt))
}

func testNamedOCILayoutContext(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
workers.CheckFeatureCompat(t, sb, workers.FeatureOCIExporter, workers.FeatureOCILayout)
Expand Down
10 changes: 10 additions & 0 deletions frontend/dockerui/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ func parseSourceDateEpoch(v string) (*time.Time, error) {
return &tm, nil
}

func parseLocalSessionIDs(opt map[string]string) map[string]string {
m := map[string]string{}
for k, v := range opt {
if strings.HasPrefix(k, localSessionIDPrefix) {
m[strings.TrimPrefix(k, localSessionIDPrefix)] = v
}
}
return m
}

func filter(opt map[string]string, key string) map[string]string {
m := map[string]string{}
for k, v := range opt {
Expand Down
37 changes: 28 additions & 9 deletions frontend/dockerui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
)

const (
buildArgPrefix = "build-arg:"
labelPrefix = "label:"
buildArgPrefix = "build-arg:"
labelPrefix = "label:"
localSessionIDPrefix = "local-sessionid:"

keyTarget = "target"
keyCgroupParent = "cgroup-parent"
Expand Down Expand Up @@ -79,10 +80,11 @@ type Config struct {

type Client struct {
Config
client client.Client
ignoreCache []string
g flightcontrol.CachedGroup[*buildContext]
bopts client.BuildOpts
client client.Client
ignoreCache []string
g flightcontrol.CachedGroup[*buildContext]
bopts client.BuildOpts
localsSessionIDs map[string]string

dockerignore []byte
dockerignoreName string
Expand Down Expand Up @@ -298,6 +300,9 @@ func (bc *Client) init() error {
return errors.Wrapf(err, "failed to parse %s", keyCopyIgnoredCheckEnabled)
}
}

bc.localsSessionIDs = parseLocalSessionIDs(opts)

return nil
}

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

sessionID := bc.bopts.SessionID
if v, ok := bc.localsSessionIDs[bctx.dockerfileLocalName]; ok {
sessionID = v
}

opts = append([]llb.LocalOption{
llb.FollowPaths(filenames),
llb.SessionID(bc.bopts.SessionID),
llb.SessionID(sessionID),
llb.SharedKeyHint(bctx.dockerfileLocalName),
WithInternalName(name),
llb.Differ(llb.DiffNone, false),
Expand Down Expand Up @@ -427,8 +437,13 @@ func (bc *Client) MainContext(ctx context.Context, opts ...llb.LocalOption) (*ll
return nil, errors.Wrapf(err, "failed to read dockerignore patterns")
}

sessionID := bc.bopts.SessionID
if v, ok := bc.localsSessionIDs[bctx.contextLocalName]; ok {
sessionID = v
}

opts = append([]llb.LocalOption{
llb.SessionID(bc.bopts.SessionID),
llb.SessionID(sessionID),
llb.ExcludePatterns(excludes),
llb.SharedKeyHint(bctx.contextLocalName),
WithInternalName("load build context"),
Expand Down Expand Up @@ -500,8 +515,12 @@ func WithInternalName(name string) llb.ConstraintsOpt {

func (bc *Client) dockerIgnorePatterns(ctx context.Context, bctx *buildContext) ([]string, error) {
if bc.dockerignore == nil {
sessionID := bc.bopts.SessionID
if v, ok := bc.localsSessionIDs[bctx.contextLocalName]; ok {
sessionID = v
}
st := llb.Local(bctx.contextLocalName,
llb.SessionID(bc.bopts.SessionID),
llb.SessionID(sessionID),
llb.FollowPaths([]string{DefaultDockerignoreName}),
llb.SharedKeyHint(bctx.contextLocalName+"-"+DefaultDockerignoreName),
WithInternalName("load "+DefaultDockerignoreName),
Expand Down
8 changes: 6 additions & 2 deletions frontend/dockerui/namedcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ func (bc *Client) namedContextRecursive(ctx context.Context, name string, nameWi
}
return &st, &img, nil
case "local":
sessionID := bc.bopts.SessionID
if v, ok := bc.localsSessionIDs[vv[1]]; ok {
sessionID = v
}
st := llb.Local(vv[1],
llb.SessionID(bc.bopts.SessionID),
llb.SessionID(sessionID),
llb.FollowPaths([]string{DefaultDockerignoreName}),
llb.SharedKeyHint("context:"+nameWithPlatform+"-"+DefaultDockerignoreName),
llb.WithCustomName("[context "+nameWithPlatform+"] load "+DefaultDockerignoreName),
Expand Down Expand Up @@ -226,7 +230,7 @@ func (bc *Client) namedContextRecursive(ctx context.Context, name string, nameWi
localOutput := &asyncLocalOutput{
name: vv[1],
nameWithPlatform: nameWithPlatform,
sessionID: bc.bopts.SessionID,
sessionID: sessionID,
excludes: excludes,
extraOpts: opt.AsyncLocalOpts,
}
Expand Down
Loading