From 16abe569dad5060b31970bddf018f8521a25e4bc Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 10 Aug 2021 20:40:10 +0000 Subject: [PATCH] Add extra args for Opts and Runc This allows additional flags to be added to runc binaries without needing to update this package each time. While the typesafe flags are still the primary api for this pkg, this allows an easier transitional state when new flags are added to underlying runtimes following the runc CLI. Signed-off-by: Michael Crosby --- runc.go | 31 +++++++++++++++++++++++++++++-- runc_test.go | 24 +++++++++++++++++++++++- runc_unix.go | 1 + 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/runc.go b/runc.go index e5a0a4a..c5eda63 100644 --- a/runc.go +++ b/runc.go @@ -100,6 +100,7 @@ type CreateOpts struct { NoNewKeyring bool ExtraFiles []*os.File Started chan<- int + ExtraArgs []string } func (o *CreateOpts) args() (out []string, err error) { @@ -125,6 +126,9 @@ func (o *CreateOpts) args() (out []string, err error) { if o.ExtraFiles != nil { out = append(out, "--preserve-fds", strconv.Itoa(len(o.ExtraFiles))) } + if len(o.ExtraArgs) > 0 { + out = append(out, o.ExtraArgs...) + } return out, nil } @@ -182,6 +186,7 @@ type ExecOpts struct { ConsoleSocket ConsoleSocket Detach bool Started chan<- int + ExtraArgs []string } func (o *ExecOpts) args() (out []string, err error) { @@ -198,6 +203,9 @@ func (o *ExecOpts) args() (out []string, err error) { } out = append(out, "--pid-file", abs) } + if len(o.ExtraArgs) > 0 { + out = append(out, o.ExtraArgs...) + } return out, nil } @@ -292,13 +300,17 @@ func (r *Runc) Run(context context.Context, id, bundle string, opts *CreateOpts) // DeleteOpts holds the deletion options for calling `runc delete` type DeleteOpts struct { - Force bool + Force bool + ExtraArgs []string } func (o *DeleteOpts) args() (out []string) { if o.Force { out = append(out, "--force") } + if len(o.ExtraArgs) > 0 { + out = append(out, o.ExtraArgs...) + } return out } @@ -313,13 +325,17 @@ func (r *Runc) Delete(context context.Context, id string, opts *DeleteOpts) erro // KillOpts specifies options for killing a container and its processes type KillOpts struct { - All bool + All bool + ExtraArgs []string } func (o *KillOpts) args() (out []string) { if o.All { out = append(out, "--all") } + if len(o.ExtraArgs) > 0 { + out = append(out, o.ExtraArgs...) + } return out } @@ -461,6 +477,7 @@ type CheckpointOpts struct { LazyPages bool // StatusFile is the file criu writes \0 to once lazy-pages is ready StatusFile *os.File + ExtraArgs []string } // CgroupMode defines the cgroup mode used for checkpointing @@ -509,6 +526,9 @@ func (o *CheckpointOpts) args() (out []string) { if o.LazyPages { out = append(out, "--lazy-pages") } + if len(o.ExtraArgs) > 0 { + out = append(out, o.ExtraArgs...) + } return out } @@ -557,6 +577,7 @@ type RestoreOpts struct { NoSubreaper bool NoPivot bool ConsoleSocket ConsoleSocket + ExtraArgs []string } func (o *RestoreOpts) args() ([]string, error) { @@ -580,6 +601,9 @@ func (o *RestoreOpts) args() ([]string, error) { if o.NoSubreaper { out = append(out, "-no-subreaper") } + if len(o.ExtraArgs) > 0 { + out = append(out, o.ExtraArgs...) + } return out, nil } @@ -695,6 +719,9 @@ func (r *Runc) args() (out []string) { // nil stands for "auto" (differs from explicit "false") out = append(out, "--rootless="+strconv.FormatBool(*r.Rootless)) } + if len(r.ExtraArgs) > 0 { + out = append(out, r.ExtraArgs...) + } return out } diff --git a/runc_test.go b/runc_test.go index f7cf3c4..4b1275a 100644 --- a/runc_test.go +++ b/runc_test.go @@ -30,7 +30,6 @@ import ( ) func TestParseVersion(t *testing.T) { - testParseVersion := func(t *testing.T, input string, expected Version) { actual, err := parseVersion([]byte(input)) if err != nil { @@ -314,3 +313,26 @@ func dummySleepRunc() (_ string, err error) { } return fh.Name(), nil } + +func TestCreateArgs(t *testing.T) { + o := &CreateOpts{} + args, err := o.args() + if err != nil { + t.Fatal(err) + } + if len(args) != 0 { + t.Fatal("args should be empty") + } + o.ExtraArgs = []string{"--other"} + args, err = o.args() + if err != nil { + t.Fatal(err) + } + if len(args) != 1 { + t.Fatal("args should have 1 arg") + } + if a := args[0]; a != "--other" { + t.Fatalf("arg should be --other but got %q", a) + } + +} diff --git a/runc_unix.go b/runc_unix.go index 548ffd6..fc724b5 100644 --- a/runc_unix.go +++ b/runc_unix.go @@ -35,4 +35,5 @@ type Runc struct { Criu string SystemdCgroup bool Rootless *bool // nil stands for "auto" + ExtraArgs []string }