Skip to content

Commit 46351eb

Browse files
committed
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 <[email protected]>
1 parent dae70e8 commit 46351eb

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

libcontainer/cgroups/systemd/apply_nosystemd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func UseSystemd() bool {
1818
return false
1919
}
2020

21+
func NewSystemdCgroupsManager() (func(config *configs.Cgroup, paths map[string]string) cgroups.Manager, error) {
22+
return nil, fmt.Errorf("Systemd not supported")
23+
}
24+
2125
func (m *Manager) Apply(pid int) error {
2226
return fmt.Errorf("Systemd not supported")
2327
}

libcontainer/cgroups/systemd/apply_systemd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ func UseSystemd() bool {
163163
return hasStartTransientUnit
164164
}
165165

166+
func NewSystemdCgroupsManager() (func(config *configs.Cgroup, paths map[string]string) cgroups.Manager, error) {
167+
if !systemdUtil.IsRunningSystemd() {
168+
return nil, fmt.Errorf("systemd not running on this host, can't use systemd as a cgroups.Manager")
169+
}
170+
return func(config *configs.Cgroup, paths map[string]string) cgroups.Manager {
171+
return &Manager{
172+
Cgroups: config,
173+
Paths: paths,
174+
}
175+
}, nil
176+
}
177+
166178
func (m *Manager) Apply(pid int) error {
167179
var (
168180
c = m.Cgroups

libcontainer/factory_linux.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ func InitArgs(args ...string) func(*LinuxFactory) error {
5151
// SystemdCgroups is an options func to configure a LinuxFactory to return
5252
// containers that use systemd to create and manage cgroups.
5353
func SystemdCgroups(l *LinuxFactory) error {
54-
l.NewCgroupsManager = func(config *configs.Cgroup, paths map[string]string) cgroups.Manager {
55-
return &systemd.Manager{
56-
Cgroups: config,
57-
Paths: paths,
58-
}
54+
systemdCgroupsManager, err := systemd.NewSystemdCgroupsManager()
55+
if err != nil {
56+
return err
5957
}
58+
l.NewCgroupsManager = systemdCgroupsManager
6059
return nil
6160
}
6261

0 commit comments

Comments
 (0)