Skip to content

Commit

Permalink
fix more entrypoint situations
Browse files Browse the repository at this point in the history
this was hard to figure out, but it works, and should be close to what
Dagger does. there is probably be a better way to model this, but I
think it achieves the desired semantics.
  • Loading branch information
vito committed Apr 9, 2023
1 parent d97561e commit de08fb9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
35 changes: 29 additions & 6 deletions pkg/bass/thunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,22 +443,45 @@ func (thunk Thunk) WithImage(image ThunkImage) Thunk {

// WithEntrypoint sets the thunk's entrypoint.
func (thunk Thunk) WithEntrypoint(entrypoint []string) Thunk {
if len(entrypoint) == 0 {
thunk.ClearEntrypoint = true
}
thunk = thunk.encapsulate()
thunk.Entrypoint = entrypoint
thunk.ClearEntrypoint = len(entrypoint) == 0
return thunk
}

// WithDefaultArgs sets the thunk's default arguments.
func (thunk Thunk) WithDefaultArgs(args []string) Thunk {
if len(args) == 0 {
thunk.ClearDefaultArgs = true
}
thunk = thunk.encapsulate()
thunk.DefaultArgs = args
thunk.ClearDefaultArgs = len(args) == 0
return thunk
}

// encapsulate is used to transition from a "build-time" thunk to a "run-time"
// thunk by configuring an entrypoint and/or default args. It returns a thunk
// with no args and an image that points to the original thunk. The returned
// thunk also inherits the original thunk's ports, labels, and workdir.
func (thunk Thunk) encapsulate() Thunk {
if len(thunk.Args) == 0 {
// already encapsulated
return thunk
} else {
return Thunk{
Image: &ThunkImage{
Thunk: &thunk,
},
// no args
Dir: thunk.Dir,
Ports: thunk.Ports,
Labels: thunk.Labels,
Entrypoint: thunk.Entrypoint,
ClearEntrypoint: thunk.ClearEntrypoint,
DefaultArgs: thunk.DefaultArgs,
ClearDefaultArgs: thunk.ClearDefaultArgs,
}
}
}

// WithArgs sets the thunk's arg values.
func (thunk Thunk) WithArgs(args []Value) Thunk {
thunk.Args = args
Expand Down
3 changes: 3 additions & 0 deletions pkg/runtimes/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,9 @@ func (b *buildkitBuilder) llb(
if len(cmd.Args) == 0 {
if forceExec {
cmd.Args = append(ib.config.Entrypoint, ib.config.Cmd...)
if len(cmd.Args) == 0 {
return ib, fmt.Errorf("no command specified")
}
} else {
// no command; we're just overriding config
return ib, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/runtimes/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (runtime *Dagger) container(ctx context.Context, thunk bass.Thunk, forceExe
ctr = ctr.WithSecretVariable(env.Name, secret)
}

if cmd.Args != nil {
if len(cmd.Args) > 0 {
ep, err := ctr.Entrypoint(ctx)
if err != nil {
return nil, err
Expand All @@ -337,14 +337,14 @@ func (runtime *Dagger) container(ctx context.Context, thunk bass.Thunk, forceExe
ctr = ctr.WithEntrypoint(ep)
}
} else if forceExec {
ctr = ctr.WithExec(nil)
ctr = ctr.WithExec(append(thunk.Entrypoint, thunk.DefaultArgs...))
}

if thunk.Entrypoint != nil {
if len(thunk.Entrypoint) > 0 || thunk.ClearEntrypoint {
ctr = ctr.WithEntrypoint(thunk.Entrypoint)
}

if thunk.DefaultArgs != nil {
if len(thunk.DefaultArgs) > 0 || thunk.ClearDefaultArgs {
ctr = ctr.WithDefaultArgs(dagger.ContainerWithDefaultArgsOpts{
Args: thunk.DefaultArgs,
})
Expand Down
1 change: 1 addition & 0 deletions pkg/runtimes/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestGRPCRuntime(t *testing.T) {
{
Platform: bass.LinuxPlatform,
Runtime: runtimes.BuildkitName,
Config: config.Scope(),
},
},
})
Expand Down
40 changes: 32 additions & 8 deletions pkg/runtimes/testdata/entrypoints.bass
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,62 @@
(read :raw)
next))

; but it is preserved
; but it is preserved in the published image
(assert = ["git"]
(entrypoint not-respected))

; entrypoint *is* respected when running directly
(run (linux/hello-world))

; or reading
(assert strings:includes?
(-> (linux/hello-world)
(read :raw)
next)
"Hello from Docker")

; or accessing a path
(def create-path
(-> (linux/alpine)
(with-entrypoint ["sh" "-c" "echo hello > index.html"])))

(assert = "hello\n"
(-> create-path/index.html
(read :raw)
next))

; entrypoint is removed when set to empty list
(def unset-image
(-> (linux/alpine/git :2.36.3)
(with-entrypoint [])))

; ; entrypoint is removed when set to empty list
(assert null?
(entrypoint unset-image))

(def set-thunk
; entrypoint can be set by thunk
(def touch-and-run
(-> (from (linux/busybox)
($ touch index.html))
($ sh -c "echo hello from index.html > index.html"))
(with-port :http 8000)
(with-entrypoint ["httpd" "-f" "-p" "8000"])))

; entrypoint can be set by thunk
(assert = ["httpd" "-f" "-p" "8000"]
(entrypoint set-thunk))
(entrypoint touch-and-run))

; entrypoint is preserved from thunk to thunk
(def keeps-entrypoint
(from set-thunk
(from touch-and-run
($ echo "Hello, world!")))

; entrypoint is preserved from thunk to thunk
(assert = ["httpd" "-f" "-p" "8000"]
(entrypoint keeps-entrypoint))

; entrypoint is respected after running a command
(def wget
(from (linux/alpine)
($ wget -O- (addr touch-and-run :http "http://$host:$port/"))))

(assert = "hello from index.html\n"
(-> wget
(read :raw)
next))

0 comments on commit de08fb9

Please sign in to comment.