From 8358a0ecbb20533aca0e8f9ee5a4a55e1bea1d17 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 10 Feb 2022 19:21:49 -0800 Subject: [PATCH] libct: StartInitialization: decouple from factory StartInitialization does not have to be a method of Factory (while it is clear why it was done that way initially, now we only have Linux containers so it does not make sense). Fix callers and docs accordingly. No change in functionality. Also, since this was the only user of libcontainer.New with the empty string as an argument, the corresponding check can now be removed from it. Signed-off-by: Kir Kolyshkin --- init.go | 3 +-- libcontainer/README.md | 3 +-- libcontainer/factory_linux.go | 13 ++++++------- libcontainer/integration/init_test.go | 6 +----- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/init.go b/init.go index bddc237f6..2f44ce8a5 100644 --- a/init.go +++ b/init.go @@ -32,8 +32,7 @@ func init() { logrus.SetFormatter(new(logrus.JSONFormatter)) logrus.Debug("child process in init()") - factory, _ := libcontainer.New("") - if err := factory.StartInitialization(); err != nil { + if err := libcontainer.StartInitialization(); err != nil { // as the error is sent back to the parent there is no need to log // or write it to stderr because the parent process will handle this os.Exit(1) diff --git a/libcontainer/README.md b/libcontainer/README.md index 2850db2aa..4210eacc5 100644 --- a/libcontainer/README.md +++ b/libcontainer/README.md @@ -32,8 +32,7 @@ func init() { if len(os.Args) > 1 && os.Args[1] == "init" { runtime.GOMAXPROCS(1) runtime.LockOSThread() - factory, _ := libcontainer.New("") - if err := factory.StartInitialization(); err != nil { + if err := libcontainer.StartInitialization(); err != nil { logrus.Fatal(err) } panic("--this line should have never been executed, congratulations--") diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index d15a21bac..93dc4812f 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -29,10 +29,8 @@ var idRegex = regexp.MustCompile(`^[\w+-\.]+$`) // New returns a linux based container factory based in the root directory. func New(root string) (*LinuxFactory, error) { - if root != "" { - if err := os.MkdirAll(root, 0o700); err != nil { - return nil, err - } + if err := os.MkdirAll(root, 0o700); err != nil { + return nil, err } return &LinuxFactory{ Root: root, @@ -169,9 +167,10 @@ func (l *LinuxFactory) Load(id string) (Container, error) { return c, nil } -// StartInitialization loads a container by opening the pipe fd from the parent to read the configuration and state -// This is a low level implementation detail of the reexec and should not be consumed externally -func (l *LinuxFactory) StartInitialization() (err error) { +// StartInitialization loads a container by opening the pipe fd from the parent +// to read the configuration and state. This is a low level implementation +// detail of the reexec and should not be consumed externally. +func StartInitialization() (err error) { // Get the INITPIPE. envInitPipe := os.Getenv("_LIBCONTAINER_INITPIPE") pipefd, err := strconv.Atoi(envInitPipe) diff --git a/libcontainer/integration/init_test.go b/libcontainer/integration/init_test.go index effcde06d..b6a3714d4 100644 --- a/libcontainer/integration/init_test.go +++ b/libcontainer/integration/init_test.go @@ -19,11 +19,7 @@ func init() { } runtime.GOMAXPROCS(1) runtime.LockOSThread() - factory, err := libcontainer.New("") - if err != nil { - logrus.Fatalf("unable to initialize for container: %s", err) - } - if err := factory.StartInitialization(); err != nil { + if err := libcontainer.StartInitialization(); err != nil { logrus.Fatal(err) } }