forked from apptainer/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroups.go
89 lines (70 loc) · 2.03 KB
/
cgroups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cgroups
import (
"encoding/json"
"github.com/containerd/cgroups"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
const singularity = "/singularity"
// Manager manage container cgroup resources restriction
type Manager struct {
Name string
Pid int
parentCgroup cgroups.Cgroup
childCgroup cgroups.Cgroup
}
// ApplyFromSpec applies cgroups ressources restriction from OCI specification
func (m *Manager) ApplyFromSpec(spec *specs.LinuxResources) (err error) {
path := cgroups.StaticPath(singularity)
// creates singularity group
_, err = cgroups.New(cgroups.V1, path, &specs.LinuxResources{})
if err != nil {
return err
}
m.parentCgroup, err = cgroups.Load(cgroups.V1, path)
if err != nil {
return err
}
s := spec
if s == nil {
s = &specs.LinuxResources{}
}
// creates subgroup in singularity group
m.childCgroup, err = m.parentCgroup.New(m.Name, s)
if err != nil {
return err
}
if err := m.childCgroup.Add(cgroups.Process{Pid: m.Pid}); err != nil {
return err
}
return
}
// ApplyFromFile applies cgroups ressources restriction from TOML configuration
// file
func (m *Manager) ApplyFromFile(path string) error {
var spec specs.LinuxResources
conf, err := LoadConfig(path)
if err != nil {
return err
}
// convert TOML structures to OCI JSON structures
data, err := json.Marshal(conf)
if err != nil {
return err
}
if err := json.Unmarshal(data, &spec); err != nil {
return err
}
return m.ApplyFromSpec(&spec)
}
// Remove removes ressources restriction for current managed process
func (m *Manager) Remove() error {
// removes process from singularity root tasks
// error is ignored because process may not exists
m.parentCgroup.Add(cgroups.Process{Pid: m.Pid})
// deletes subgroup
return m.childCgroup.Delete()
}