Skip to content

Commit

Permalink
add entrypoint in custom image (#739)
Browse files Browse the repository at this point in the history
* WIP: add entrypoint

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* fix entrypoint

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* add test

Signed-off-by: Keming <kemingyang@tensorchord.ai>

* make entrypoint public

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy authored Aug 9, 2022
1 parent 4df956b commit b492e83
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion e2e/testdata/custom-image-test/build.envd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def build():
base(language="python", image="python:3.8")
base(language="python", image="python:3.9-slim")
install.python_packages(name=[
"via",
])
config.entrypoint(["date", "-u"])
3 changes: 2 additions & 1 deletion examples/custom-image/build.envd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def build():
base(language="python", image="python:3.8")
base(language="python", image="python:3.9-slim")
install.python_packages(name=[
"via",
])
config.entrypoint(["date", "-u"])
2 changes: 1 addition & 1 deletion pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func up(clicontext *cli.Context) error {

err = dockerClient.CleanEnvdIfExists(clicontext.Context, ctr, force)
if err != nil {
return errors.Wrap(err, "failed to start the envd environment")
return errors.Wrap(err, "failed to clean the envd environment")
}
containerID, containerIP, err := dockerClient.StartEnvd(clicontext.Context,
tag, ctr, buildContext, gpu, numGPUs, sshPortInHost, *ir.DefaultGraph, clicontext.Duration("timeout"),
Expand Down
3 changes: 2 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ func (b generalBuilder) imageConfig(ctx context.Context) (string, error) {
}
labels[types.ImageLabelContext] = b.BuildContextDir

ep, err := ir.Entrypoint(b.BuildContextDir)
ep, err := ir.CompileEntrypoint(b.BuildContextDir)
if err != nil {
return "", errors.Wrap(err, "failed to get entrypoint")
}
b.logger.Debugf("final entrypoint: {%s}\n", ep)
data, err := ImageConfigStr(labels, ports, ep)
if err != nil {
return "", errors.Wrap(err, "failed to get image config")
Expand Down
21 changes: 21 additions & 0 deletions pkg/lang/frontend/starlark/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var Module = &starlarkstruct.Module{
"julia_pkg_server": starlark.NewBuiltin(
ruleJuliaPackageServer, ruleFuncJuliaPackageServer),
"rstudio_server": starlark.NewBuiltin(ruleRStudioServer, ruleFuncRStudioServer),
"entrypoint": starlark.NewBuiltin(ruleEntrypoint, ruleFuncEntrypoint),
},
}

Expand Down Expand Up @@ -196,3 +197,23 @@ func ruleFuncCondaChannel(thread *starlark.Thread, _ *starlark.Builtin,

return starlark.None, nil
}

func ruleFuncEntrypoint(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var argv *starlark.List

if err := starlark.UnpackArgs(ruleEntrypoint, args, kwargs, "name", &argv); err != nil {
return nil, err
}

argList := []string{}
if argv != nil {
for i := 0; i < argv.Len(); i++ {
argList = append(argList, argv.Index(i).(starlark.String).GoString())
}
}

logger.Debugf("user defined entrypoints: {%s}\n", argList)
ir.Entrypoint(argList)
return starlark.None, nil
}
1 change: 1 addition & 0 deletions pkg/lang/frontend/starlark/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ const (
ruleGPU = "config.gpu"
ruleJuliaPackageServer = "config.julia_pkg_server"
ruleRStudioServer = "config.rstudio_server"
ruleEntrypoint = "config.entrypoint"
)
10 changes: 4 additions & 6 deletions pkg/lang/ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func ExposedPorts() (map[string]struct{}, error) {
return DefaultGraph.ExposedPorts()
}

func Entrypoint(buildContextDir string) ([]string, error) {
return DefaultGraph.Entrypoint(buildContextDir)
func CompileEntrypoint(buildContextDir string) ([]string, error) {
return DefaultGraph.GetEntrypoint(buildContextDir)
}

func (g Graph) GPUEnabled() bool {
Expand Down Expand Up @@ -149,11 +149,9 @@ func (g Graph) DefaultCacheImporter() (*string, error) {
return &res, nil
}

func (g Graph) Entrypoint(buildContextDir string) ([]string, error) {
// Do not set entrypoint if the image is customized.
func (g Graph) GetEntrypoint(buildContextDir string) ([]string, error) {
if g.Image != nil {
logrus.Debug("skip entrypoint because the image is customized")
return []string{}, nil
return g.Entrypoint, nil
}

ep := []string{
Expand Down
4 changes: 4 additions & 0 deletions pkg/lang/ir/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func Mount(src, dest string) {
})
}

func Entrypoint(args []string) {
DefaultGraph.Entrypoint = append(DefaultGraph.Entrypoint, args...)
}

func RuntimeCommands(commands map[string]string) {
for k, v := range commands {
DefaultGraph.RuntimeCommands[k] = v
Expand Down
1 change: 1 addition & 0 deletions pkg/lang/ir/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Graph struct {
Exec []string
Copy []CopyInfo
Mount []MountInfo
Entrypoint []string
RuntimeCommands map[string]string

*JupyterConfig
Expand Down

0 comments on commit b492e83

Please sign in to comment.