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

Dockerfiles: add Dagger support, stop respecting workdir #279

Merged
merged 5 commits into from
Apr 15, 2023
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
4 changes: 2 additions & 2 deletions bass/bass.lock
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ memos: {
}
output: {
string: {
value: "cbcb3f550f1208f8804c9f4e52921bd868856cd5"
value: "5fbaf17c298ed8f0f9bd5fdbfb8a05abe3471785"
}
}
}
Expand All @@ -521,7 +521,7 @@ memos: {
}
output: {
string: {
value: "a18944bec00bd72341909da8cf0147a1e59884d5"
value: "9141da72318c09fe61e12c8c2166ffad6e8c6413"
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/runtimes/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ func (b *buildkitBuilder) Build(
llb.AddMount("/dev/shm", llb.Scratch(), llb.Tmpfs()),
llb.AddMount(ioDir, llb.Scratch().File(
llb.Mkfile("in", 0600, cmdPayload),
llb.WithCustomName("[hide] mount command json"),
llb.WithCustomNamef("[hide] mount command json for %s", thunk.String()),
)),
llb.AddMount(shimExePath, shimExe, llb.SourcePath("run")),
llb.With(llb.Dir(workDir)),
Expand Down Expand Up @@ -1476,7 +1476,6 @@ func (b *buildkitBuilder) image(ctx context.Context, image *bass.ThunkImage) (In
}

ib.Output = ib.FS
ib.OutputSourcePath = ib.Config.WorkingDir
ib.NeedsInsecure = needsInsecure
return ib, nil
}
Expand Down Expand Up @@ -1751,10 +1750,6 @@ func (proxy *statusProxy) NiceError(msg string, err error) bass.NiceError {
return proxy.prog.WrapError(msg, err)
}

func getWorkdir(st llb.ExecState, _ string) llb.State {
return st.GetMount(workDir)
}

func dialBuildkit(ctx context.Context, addr string, installation string, certsDir string) (*bkclient.Client, error) {
if addr == "" {
addr = os.Getenv("BUILDKIT_HOST")
Expand Down
105 changes: 79 additions & 26 deletions pkg/runtimes/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,6 @@ var epoch = time.Date(1985, 10, 26, 8, 15, 0, 0, time.UTC)
func (runtime *Dagger) mount(ctx context.Context, client *dagger.Client, ctr *dagger.Container, target string, src bass.ThunkMountSource) (*dagger.Container, error) {
client = client.Pipeline(fmt.Sprintf("mount %s", src.ToValue()))

if !path.IsAbs(target) {
target = path.Join(workDir, target)
}

switch {
case src.ThunkPath != nil:
srcCtr, err := runtime.container(ctx, client, src.ThunkPath.Thunk, true)
Expand Down Expand Up @@ -480,64 +476,121 @@ func (runtime *Dagger) mount(ctx context.Context, client *dagger.Client, ctr *da
}

func (runtime *Dagger) image(ctx context.Context, client *dagger.Client, image *bass.ThunkImage) (string, *dagger.Container, error) {
if image == nil {
switch {
case image == nil:
return "", nil, nil
}

if image.Ref != nil {
case image.Ref != nil:
ref, err := image.Ref.Ref()
if err != nil {
return "", nil, err
}

return ref, nil, nil
}

if image.Thunk != nil {
case image.Thunk != nil:
ctr, err := runtime.container(ctx, client, *image.Thunk, false)
if err != nil {
return "", nil, fmt.Errorf("image thunk: %w", err)
}

return "", ctr, nil
}

if image.Archive != nil {
file, err := runtime.inputFile(ctx, client, image.Archive.File)
case image.Archive != nil:
archive := image.Archive

file, err := runtime.inputFile(ctx, client, archive.File)
if err != nil {
return "", nil, fmt.Errorf("image thunk: %w", err)
}

name := image.Archive.File.ToValue().String()
if image.Archive.Tag != "" {
name += ":" + image.Archive.Tag
name := archive.File.ToValue().String()
if archive.Tag != "" {
name += ":" + archive.Tag
}

client = client.Pipeline(fmt.Sprintf("import %s [platform=%s]", name, image.Archive.Platform))
client = client.Pipeline(fmt.Sprintf("import %s [platform=%s]", name, archive.Platform))

ctr := client.Container(dagger.ContainerOpts{
Platform: dagger.Platform(image.Archive.Platform.String()),
Platform: dagger.Platform(archive.Platform.String()),
}).Import(file)

return "", ctr, nil
}

return "", nil, fmt.Errorf("unsupported image type: %s", image.ToValue())
case image.DockerBuild != nil:
build := image.DockerBuild
context, err := runtime.inputDirectory(ctx, client, build.Context)
if err != nil {
return "", nil, fmt.Errorf("image build input context: %w", err)
}

opts := dagger.ContainerBuildOpts{
Target: build.Target,
}

if build.Dockerfile != nil {
opts.Dockerfile = build.Dockerfile.Slash()
}

if build.Args != nil {
_ = build.Args.Each(func(k bass.Symbol, v bass.Value) error {
var str string
if err := v.Decode(&str); err != nil {
str = v.String()
}

opts.BuildArgs = append(opts.BuildArgs, dagger.BuildArg{
Name: k.String(),
Value: str,
})

return nil
})
}

ctr := client.Container(dagger.ContainerOpts{
Platform: dagger.Platform(build.Platform.String()),
}).Build(context, opts)

return "", ctr, nil

default:
return "", nil, fmt.Errorf("unsupported image type: %s", image.ToValue())
}
}

func (runtime *Dagger) inputFile(ctx context.Context, client *dagger.Client, input bass.ImageBuildInput) (*dagger.File, error) {
root, fsp, err := runtime.inputRoot(ctx, client, input)
if err != nil {
return nil, err
}
return root.File(fsp.Slash()), nil
}

func (runtime *Dagger) inputDirectory(ctx context.Context, client *dagger.Client, input bass.ImageBuildInput) (*dagger.Directory, error) {
root, fsp, err := runtime.inputRoot(ctx, client, input)
if err != nil {
return nil, err
}
return root.Directory(fsp.Slash()), nil
}

func (runtime *Dagger) inputRoot(ctx context.Context, client *dagger.Client, input bass.ImageBuildInput) (interface {
File(string) *dagger.File
Directory(string) *dagger.Directory
}, bass.FilesystemPath, error) {
switch {
case input.Thunk != nil:
srcCtr, err := runtime.container(ctx, client, input.Thunk.Thunk, true) // TODO: or false?
srcCtr, err := runtime.container(ctx, client, input.Thunk.Thunk, true)
if err != nil {
return nil, fmt.Errorf("image thunk: %w", err)
return nil, nil, fmt.Errorf("image thunk: %w", err)
}

return srcCtr.File(input.Thunk.Path.Slash()), nil
return srcCtr, input.Thunk.Path.FilesystemPath(), nil
case input.Host != nil:
dir := client.Host().Directory(input.Host.ContextDir)
fsp := input.Host.Path.FilesystemPath()
return dir.File(fsp.FromSlash()), nil
return dir, fsp, nil
case input.FS != nil:
dir := client.Directory()

Expand All @@ -561,12 +614,12 @@ func (runtime *Dagger) inputFile(ctx context.Context, client *dagger.Client, inp
return nil
})
if err != nil {
return nil, fmt.Errorf("walk %s: %w", root, err)
return nil, nil, fmt.Errorf("walk %s: %w", root, err)
}

fsp := input.FS.Path.FilesystemPath()
return dir.File(fsp.Slash()), nil
return dir, fsp, nil
default:
return nil, fmt.Errorf("unknown input type: %T", input.ToValue())
return nil, nil, fmt.Errorf("unknown input type: %T", input.ToValue())
}
}
1 change: 0 additions & 1 deletion pkg/runtimes/dagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestDaggerRuntime(t *testing.T) {
Runtime: runtimes.DaggerName,
}, runtimes.SkipSuites(
"tls.bass",
"docker-build.bass",
"cache-cmd.bass",
))
}
16 changes: 10 additions & 6 deletions pkg/runtimes/testdata/docker-build.bass
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
(defn read-all [thunk]
(-> thunk (read :raw) next))

(refute = "/wd"
(read-all
(from (docker-build *dir*/docker-build/ {:os "linux"})
($ pwd))))

(assert = "hello from Dockerfile\n"
(read-all
(from (docker-build *dir*/docker-build/ {:os "linux"})
($ pwd)
($ cat ./wd-file))))
($ cat /wd/wd-file))))

(assert = "hello from Dockerfile.alt\n"
(read-all
(from (docker-build *dir*/docker-build/ {:os "linux"}
:dockerfile ./Dockerfile.alt)
($ cat ./wd-file))))
($ cat /wd/wd-file))))


(assert = "hello from alt stage in Dockerfile\n"
(read-all
(from (docker-build *dir*/docker-build/ {:os "linux"}
:target "alt")
($ cat ./wd-file))))
($ cat /wd/wd-file))))

(assert = "hello from Dockerfile with message sup\n"
(read-all
(from (docker-build *dir*/docker-build/ {:os "linux"}
:target "arg"
:args {:MESSAGE "sup"})
($ cat ./wd-file))))
($ cat /wd/wd-file))))

(assert = "hello from Dockerfile with env bar\nbar\n"
(read-all
(from (docker-build *dir*/docker-build/ {:os "linux"}
:target "env")
($ sh -c "cat ./wd-file; echo $FOO"))))
($ sh -c "cat /wd/wd-file; echo $FOO"))))
3 changes: 3 additions & 0 deletions pkg/runtimes/testdata/docker-build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ WORKDIR /wd
RUN echo hello from Dockerfile > wd-file

FROM alpine AS alt
WORKDIR /wd
RUN echo hello from alt stage in Dockerfile > wd-file

FROM alpine AS arg
WORKDIR /wd
ARG MESSAGE
RUN echo hello from Dockerfile with message $MESSAGE > wd-file

FROM alpine AS env
WORKDIR /wd
ENV FOO=bar
RUN echo hello from Dockerfile with env $FOO > wd-file

Expand Down