Skip to content

Commit

Permalink
chore: switch to LocalMounts implementation
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Feb 10, 2024
1 parent d0177c6 commit ceb2486
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
30 changes: 24 additions & 6 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/tonistiigi/fsutil"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -220,7 +221,7 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
Ref: opt.Ref,
Frontend: "dockerfile.v0",
FrontendAttrs: map[string]string{},
LocalDirs: map[string]string{},
LocalMounts: map[string]fsutil.FS{},
CacheExports: cacheTo,
CacheImports: cacheFrom,
AllowedEntitlements: opt.Allow,
Expand Down Expand Up @@ -385,7 +386,8 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op
}
defers = append(defers, releaseLoad)

if sharedKey := so.LocalDirs["context"]; sharedKey != "" {
if lm, ok := so.LocalMounts["context"]; ok {
sharedKey := lm.(fs).root
if p, err := filepath.Abs(sharedKey); err == nil {
sharedKey = filepath.Base(p)
}
Expand Down Expand Up @@ -1145,11 +1147,19 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog
dockerfileReader = buf
inp.ContextPath, _ = os.MkdirTemp("", "empty-dir")
toRemove = append(toRemove, inp.ContextPath)
target.LocalDirs["context"] = inp.ContextPath
if lm, err := newFS(inp.ContextPath); err != nil {
return nil, err
} else {
target.LocalMounts["context"] = lm
}
}
}
case isLocalDir(inp.ContextPath):
target.LocalDirs["context"] = inp.ContextPath
if lm, err := newFS(inp.ContextPath); err != nil {
return nil, err
} else {
target.LocalMounts["context"] = lm
}
switch inp.DockerfilePath {
case "-":
dockerfileReader = inp.InStream
Expand Down Expand Up @@ -1201,7 +1211,11 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog
}

if dockerfileDir != "" {
target.LocalDirs["dockerfile"] = dockerfileDir
if lm, err := newFS(dockerfileDir); err != nil {
return nil, err
} else {
target.LocalMounts["dockerfile"] = lm
}
dockerfileName = handleLowercaseDockerfile(dockerfileDir, dockerfileName)
}

Expand Down Expand Up @@ -1296,7 +1310,11 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog
if k == "context" || k == "dockerfile" {
localName = "_" + k // underscore to avoid collisions
}
target.LocalDirs[localName] = v.Path
if lm, err := newFS(v.Path); err != nil {
return nil, err
} else {
target.LocalMounts[localName] = lm
}
target.FrontendAttrs["context:"+k] = "local:" + localName
}

Expand Down
21 changes: 21 additions & 0 deletions build/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package build

import (
"github.com/tonistiigi/fsutil"
)

type fs struct {
fsutil.FS
root string
}

func newFS(root string) (fs, error) {
f, err := fsutil.NewFS(root)
if err != nil {
return fs{}, err
}
return fs{
FS: f,
root: root,
}, nil
}
3 changes: 2 additions & 1 deletion build/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st
if !setGitInfo || root == "" {
return
}
for k, dir := range so.LocalDirs {
for k, lm := range so.LocalMounts {
dir := lm.(fs).root
dir, err = filepath.EvalSymlinks(dir)
if err != nil {
continue
Expand Down
10 changes: 7 additions & 3 deletions build/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonistiigi/fsutil"
)

func setupTest(tb testing.TB) {
Expand Down Expand Up @@ -159,11 +160,14 @@ func TestGetGitAttributesDirty(t *testing.T) {
func TestLocalDirs(t *testing.T) {
setupTest(t)

wdfs, err := newFS(".")
require.NoError(t, err)

so := &client.SolveOpt{
FrontendAttrs: map[string]string{},
LocalDirs: map[string]string{
"context": ".",
"dockerfile": ".",
LocalMounts: map[string]fsutil.FS{
"context": wdfs,
"dockerfile": wdfs,
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/tonistiigi/fsutil v0.0.0-20230825212630-f09800878302
github.com/zclconf/go-cty v1.14.1
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.42.0
Expand Down Expand Up @@ -137,7 +138,6 @@ require (
github.com/secure-systems-lab/go-securesystemslib v0.4.0 // indirect
github.com/shibumi/go-pathspec v1.3.0 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
github.com/tonistiigi/fsutil v0.0.0-20230825212630-f09800878302 // indirect
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
github.com/tonistiigi/vt100 v0.0.0-20230623042737-f9a4f7ef6531 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down

0 comments on commit ceb2486

Please sign in to comment.