From 46351eb3d14b8b42454787166811a61fe51e28b7 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Wed, 1 May 2019 13:22:19 -0700 Subject: [PATCH] Move systemd.Manager initialization into a function in that module This will permit us to extend the internals of systemd.Manager to include further information about the system, such as whether cgroupv1, cgroupv2 or both are in effect. Furthermore, it allows a future refactor of moving more of UseSystemd() code into the factory initialization function. Signed-off-by: Filipe Brandenburger --- libcontainer/cgroups/systemd/apply_nosystemd.go | 4 ++++ libcontainer/cgroups/systemd/apply_systemd.go | 12 ++++++++++++ libcontainer/factory_linux.go | 9 ++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/libcontainer/cgroups/systemd/apply_nosystemd.go b/libcontainer/cgroups/systemd/apply_nosystemd.go index a65d8e4432d..c171365be69 100644 --- a/libcontainer/cgroups/systemd/apply_nosystemd.go +++ b/libcontainer/cgroups/systemd/apply_nosystemd.go @@ -18,6 +18,10 @@ func UseSystemd() bool { return false } +func NewSystemdCgroupsManager() (func(config *configs.Cgroup, paths map[string]string) cgroups.Manager, error) { + return nil, fmt.Errorf("Systemd not supported") +} + func (m *Manager) Apply(pid int) error { return fmt.Errorf("Systemd not supported") } diff --git a/libcontainer/cgroups/systemd/apply_systemd.go b/libcontainer/cgroups/systemd/apply_systemd.go index a10e3f6a892..3bf723bf964 100644 --- a/libcontainer/cgroups/systemd/apply_systemd.go +++ b/libcontainer/cgroups/systemd/apply_systemd.go @@ -163,6 +163,18 @@ func UseSystemd() bool { return hasStartTransientUnit } +func NewSystemdCgroupsManager() (func(config *configs.Cgroup, paths map[string]string) cgroups.Manager, error) { + if !systemdUtil.IsRunningSystemd() { + return nil, fmt.Errorf("systemd not running on this host, can't use systemd as a cgroups.Manager") + } + return func(config *configs.Cgroup, paths map[string]string) cgroups.Manager { + return &Manager{ + Cgroups: config, + Paths: paths, + } + }, nil +} + func (m *Manager) Apply(pid int) error { var ( c = m.Cgroups diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index e35957c3148..78a8c0a8135 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -51,12 +51,11 @@ func InitArgs(args ...string) func(*LinuxFactory) error { // SystemdCgroups is an options func to configure a LinuxFactory to return // containers that use systemd to create and manage cgroups. func SystemdCgroups(l *LinuxFactory) error { - l.NewCgroupsManager = func(config *configs.Cgroup, paths map[string]string) cgroups.Manager { - return &systemd.Manager{ - Cgroups: config, - Paths: paths, - } + systemdCgroupsManager, err := systemd.NewSystemdCgroupsManager() + if err != nil { + return err } + l.NewCgroupsManager = systemdCgroupsManager return nil }