From ebd745f91a702f0d437e59e9e1ee66f9c205efee Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Tue, 24 Mar 2020 17:05:20 +0800 Subject: [PATCH] ctr: do not assume runc options by default If runtime is not runc, it doesn't make sense to send runc Options as container create options, which will confuse other runtimes and it actually causes kata shimv2 to fail to unmarshal the requset. Signed-off-by: Peng Tao --- cmd/ctr/commands/run/run_unix.go | 51 +++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/cmd/ctr/commands/run/run_unix.go b/cmd/ctr/commands/run/run_unix.go index ac4e8e9cb..920edbfd6 100644 --- a/cmd/ctr/commands/run/run_unix.go +++ b/cmd/ctr/commands/run/run_unix.go @@ -205,24 +205,9 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli } } - runtimeOpts := &options.Options{} - if runcBinary := context.String("runc-binary"); runcBinary != "" { - if context.String("runtime") == "io.containerd.runc.v2" { - runtimeOpts.BinaryName = runcBinary - } else { - return nil, errors.New("specifying runc-binary is only supported for \"io.containerd.runc.v2\" runtime") - } - } - if context.Bool("runc-systemd-cgroup") { - if context.String("runtime") == "io.containerd.runc.v2" { - if context.String("cgroup") == "" { - // runc maps "machine.slice:foo:deadbeef" to "/machine.slice/foo-deadbeef.scope" - return nil, errors.New("option --runc-systemd-cgroup requires --cgroup to be set, e.g. \"machine.slice:foo:deadbeef\"") - } - runtimeOpts.SystemdCgroup = true - } else { - return nil, errors.New("specifying runc-systemd-cgroup is only supported for \"io.containerd.runc.v2\" runtime") - } + runtimeOpts, err := getRuntimeOptions(context) + if err != nil { + return nil, err } cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), runtimeOpts)) @@ -237,6 +222,36 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli return client.NewContainer(ctx, id, cOpts...) } +func getRuncOptions(context *cli.Context) (*options.Options, error) { + runtimeOpts := &options.Options{} + if runcBinary := context.String("runc-binary"); runcBinary != "" { + runtimeOpts.BinaryName = runcBinary + } + if context.Bool("runc-systemd-cgroup") { + if context.String("cgroup") == "" { + // runc maps "machine.slice:foo:deadbeef" to "/machine.slice/foo-deadbeef.scope" + return nil, errors.New("option --runc-systemd-cgroup requires --cgroup to be set, e.g. \"machine.slice:foo:deadbeef\"") + } + runtimeOpts.SystemdCgroup = true + } + + return runtimeOpts, nil +} + +func getRuntimeOptions(context *cli.Context) (interface{}, error) { + // validate first + if (context.String("runc-binary") != "" || context.Bool("runc-systemd-cgroup")) && + context.String("runtime") != "io.containerd.runc.v2" { + return nil, errors.New("specifying runc-binary and runc-systemd-cgroup is only supported for \"io.containerd.runc.v2\" runtime") + } + + if context.String("runtime") == "io.containerd.runc.v2" { + return getRuncOptions(context) + } + + return nil, nil +} + func getNewTaskOpts(context *cli.Context) []containerd.NewTaskOpts { var ( tOpts []containerd.NewTaskOpts