Skip to content

Commit

Permalink
fix: Several fixes to arguments in kraftkit and the action (#2069)
Browse files Browse the repository at this point in the history
Reviewed-by: Alexander Jung <[email protected]>
Approved-by: Alexander Jung <[email protected]>
  • Loading branch information
nderjung authored Jan 14, 2025
2 parents 3fe4466 + 5bec354 commit cdd0af5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 32 deletions.
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ inputs:
description: If to run the unikernel.
required: false
default: "false"
memory:
description: Set the memory size. String of format "1M"/"1G"/"1K"
required: false
timeout:
description: Timeout for the unikernel.
required: false
Expand All @@ -88,9 +91,6 @@ inputs:
args:
description: Arguments to pass to the unikernel.
required: false
memory:
description: Set the memory size. String of format "1M"/"1G"/"1K"
required: false
name:
description: Set the name of the output.
required: true
Expand Down
7 changes: 7 additions & 0 deletions initrd/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ func (initrd *dockerfile) Build(ctx context.Context) (string, error) {
)
initrd.env = img.Metadata.Config.Config.Env

// Remove the shell command if it is the first argument
// TODO(craciunoiuc): Remove this once shell scripts are supported[1]
// [1]: https://github.com/unikraft/unikraft/pull/1386
if len(initrd.args) >= 2 && initrd.args[0] == "/bin/sh" && initrd.args[1] == "-c" {
initrd.args = initrd.args[2:]
}

if err := tempgen.Cleanup(); err != nil {
return "", fmt.Errorf("could not cleanup temp dir generator: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/cli/kraft/cloud/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ func NewCmd() *cobra.Command {
# Supply arguments to the instance of the existing image
$ kraft cloud --metro fra0 deploy -p 443:8080 caddy:latest -- /bin/server --debug
# Supply arguments to the instance of the project (overriding the cmd):
# Supply arguments to the instance of the project (appending to the cmd):
$ kraft cloud --metro fra0 deploy -p 443:8080 . -- /bin/server --debug
# Supply arguments to the instance of the project (overwriting the cmd):
$ kraft cloud --metro fra0 deploy -p 443:8080 --entrypoint "/bin/server --debug" .
# Immediately start following the log tail
$ kraft cloud --metro fra0 deploy -p 443:8080 -f caddy:latest
`),
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/kraft/pkg/packager_kraftfile_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ func (p *packagerKraftfileRuntime) Pack(ctx context.Context, opts *PkgOptions, a
if len(opts.Args) == 0 {
if len(opts.Project.Command()) > 0 {
opts.Args = opts.Project.Command()
} else if len(targ.Command()) > 0 {
opts.Args = targ.Command()
} else if cmds != nil {
opts.Args = cmds
} else if len(targ.Command()) > 0 {
opts.Args = targ.Command()
}
}

Expand Down
9 changes: 7 additions & 2 deletions tools/github-action/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"time"

"github.com/mattn/go-shellwords"
"github.com/rancher/wrangler/pkg/signals"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -61,11 +62,15 @@ func (opts *GithubAction) execute(ctx context.Context) error {
targetName = opts.target.Platform().Name() + "/" + opts.target.Architecture().Name()
}

args, err := shellwords.Parse(opts.Args)
if err != nil {
return err
}

machine.Spec.Kernel = "project://" + opts.project.Name() + ":" + targetName
machine.Spec.Architecture = opts.target.Architecture().Name()
machine.Spec.Platform = opts.target.Platform().Name()
machine.Spec.ApplicationArgs = opts.Args

machine.Spec.ApplicationArgs = args
machine.Status.KernelPath = opts.target.Kernel()

if _, err := os.Stat(machine.Status.KernelPath); err != nil && os.IsNotExist(err) {
Expand Down
20 changes: 11 additions & 9 deletions tools/github-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ type GithubAction struct {
Timeout uint64 `long:"timeout" env:"INPUT_TIMEOUT" usage:"Timeout for the unikernel"`

// Packaging flags
Args []string `long:"args" env:"INPUT_ARGS" usage:"Arguments to pass to the unikernel"`
Rootfs string `long:"rootfs" env:"INPUT_ROOTFS" usage:"Include a rootfs at path"`
Memory string `long:"memory" env:"INPUT_MEMORY" usage:"Set the memory size"`
Name string `long:"name" env:"INPUT_NAME" usage:"Set the name of the output"`
Output string `long:"output" env:"INPUT_OUTPUT" usage:"Set the output path"`
Push bool `long:"push" env:"INPUT_PUSH" usage:"Push the output"`
Strategy string `long:"strategy" env:"INPUT_STRATEGY" usage:"Merge strategy to use when packaging"`
Dbg bool `long:"dbg" env:"INPUT_DBG" usage:"Use the debug kernel"`
Args string `long:"args" env:"INPUT_ARGS" usage:"Arguments to pass to the unikernel"`
Rootfs string `long:"rootfs" env:"INPUT_ROOTFS" usage:"Include a rootfs at path"`
Memory string `long:"memory" env:"INPUT_MEMORY" usage:"Set the memory size"`
Name string `long:"name" env:"INPUT_NAME" usage:"Set the name of the output"`
Output string `long:"output" env:"INPUT_OUTPUT" usage:"Set the output path"`
Push bool `long:"push" env:"INPUT_PUSH" usage:"Push the output"`
Strategy string `long:"strategy" env:"INPUT_STRATEGY" usage:"Merge strategy to use when packaging"`
Dbg bool `long:"dbg" env:"INPUT_DBG" usage:"Use the debug kernel"`

// Internal attributes
project app.Application
Expand Down Expand Up @@ -255,7 +255,7 @@ func (opts *GithubAction) Run(ctx context.Context, args []string) (err error) {
opts.Target,
)

if len(targets) != 1 {
if len(targets) > 1 {
// TODO(nderjung): We should support building multiple targets in the
// future, but for now we disable this ability. This is largely to do with
// package management afterwards which does not yet support multi-target
Expand All @@ -264,6 +264,8 @@ func (opts *GithubAction) Run(ctx context.Context, args []string) (err error) {
// unikernel after a successful build via this action, multiple targets
// would also fail at this step.
return fmt.Errorf("cannot build more than one target using action")
} else if len(targets) == 0 {
return fmt.Errorf("no targets found")
}

opts.target = targets[0]
Expand Down
37 changes: 22 additions & 15 deletions tools/github-action/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,23 @@ func (opts *GithubAction) packUnikraft(ctx context.Context, output string, forma
}
}

var cmdShellArgs []string

// If no arguments have been specified, use the ones which are default and
// that have been included in the package.
if len(opts.Args) == 0 {
if len(opts.project.Command()) > 0 {
opts.Args = opts.project.Command()
cmdShellArgs = opts.project.Command()
} else if len(opts.target.Command()) > 0 {
opts.Args = opts.target.Command()
cmdShellArgs = opts.target.Command()
}
}

cmdShellArgs, err := shellwords.Parse(strings.Join(opts.Args, " "))
if err != nil {
return err
cmdShellArgs, err = shellwords.Parse(strings.Join(cmdShellArgs, " "))
if err != nil {
return err
}
} else {
cmdShellArgs = strings.Split(opts.Args, " ")
}

popts := []packmanager.PackOption{
Expand Down Expand Up @@ -454,27 +458,29 @@ func (opts *GithubAction) packRuntime(ctx context.Context, output string, format
return fmt.Errorf("could not build rootfs: %w", err)
}

args := []string{}

// If no arguments have been specified, use the ones which are default and
// that have been included in the package.
if len(opts.Args) == 0 {
if len(opts.project.Command()) > 0 {
opts.Args = opts.project.Command()
} else if len(targ.Command()) > 0 {
opts.Args = targ.Command()
args = opts.project.Command()
} else if cmds != nil {
opts.Args = cmds
args = cmds
} else if len(targ.Command()) > 0 {
args = targ.Command()
}
}

args := []string{}
// Only parse arguments if they have been provided.
if len(opts.Args) > 0 {
args, err = shellwords.Parse(fmt.Sprintf("'%s'", strings.Join(opts.Args, "' '")))
args, err = shellwords.Parse(fmt.Sprintf("'%s'", strings.Join(args, "' '")))
if err != nil {
return err
}
} else {
args = strings.Split(opts.Args, " ")
}

fmt.Println("Packaging args:", args)

labels := opts.project.Labels()

var popts []packmanager.PackOption
Expand Down Expand Up @@ -507,6 +513,7 @@ func (opts *GithubAction) packRuntime(ctx context.Context, output string, format
}

if opts.Push {
fmt.Println("Pushing")
return packaged[0].Push(ctx)
}

Expand Down

0 comments on commit cdd0af5

Please sign in to comment.