Skip to content

Commit 3218fcb

Browse files
committed
env: add cgroupControllers to /config and warn when certain cgroup controller is not enabled
1 parent cadf5f4 commit 3218fcb

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

env/env_linux.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func NewBuilder(c Config) (pool.EnvBuilder, map[string]any, error) {
124124
ContainerUID: cUID,
125125
ContainerGID: cGID,
126126
}
127-
cgb, err := newCgroup(c)
127+
cgb, ct, err := newCgroup(c)
128128
if err != nil {
129129
return nil, nil, err
130130
}
@@ -154,16 +154,18 @@ func NewBuilder(c Config) (pool.EnvBuilder, map[string]any, error) {
154154
"workDir": workDir,
155155
"uid": cUID,
156156
"gid": cGID,
157+
158+
"cgroupControllers": ct.Names(),
157159
}, nil
158160
}
159161

160-
func newCgroup(c Config) (cgroup.Cgroup, error) {
162+
func newCgroup(c Config) (cgroup.Cgroup, *cgroup.Controllers, error) {
161163
prefix := c.CgroupPrefix
162164
t := cgroup.DetectedCgroupType
163165
ct, err := cgroup.GetAvailableController()
164166
if err != nil {
165167
c.Error("Failed to get available controllers", err)
166-
return nil, err
168+
return nil, nil, err
167169
}
168170
if t == cgroup.TypeV2 {
169171
// Check if running on a systemd enabled system
@@ -192,32 +194,32 @@ func newCgroup(c Config) (cgroup.Cgroup, error) {
192194
}
193195
ch := make(chan string, 1)
194196
if _, err := conn.StartTransientUnitContext(context.TODO(), scopeName, "replace", properties, ch); err != nil {
195-
return nil, fmt.Errorf("failed to start transient unit: %w", err)
197+
return nil, nil, fmt.Errorf("failed to start transient unit: %w", err)
196198
}
197199
s := <-ch
198200
if s != "done" {
199-
return nil, fmt.Errorf("starting transient unit returns error: %w", err)
201+
return nil, nil, fmt.Errorf("starting transient unit returns error: %w", err)
200202
}
201203
scopeName, err := cgroup.GetCurrentCgroupPrefix()
202204
if err != nil {
203-
return nil, err
205+
return nil, nil, err
204206
}
205207
c.Info("Current cgroup is ", scopeName)
206208
prefix = scopeName
207209
ct, err = cgroup.GetAvailableControllerWithPrefix(prefix)
208210
if err != nil {
209-
return nil, err
211+
return nil, nil, err
210212
}
211213
}
212214
}
213215
cgb, err := cgroup.New(prefix, ct)
214216
if err != nil {
215217
if os.Getuid() == 0 {
216218
c.Error("Failed to create cgroup ", prefix, " ", err)
217-
return nil, err
219+
return nil, nil, err
218220
}
219221
c.Warn("Not running in root and have no permission on cgroup, falling back to rlimit / rusage mode")
220-
return nil, nil
222+
return nil, nil, nil
221223
}
222224
// Create api and migrate current process into it
223225
c.Info("Creating nesting api cgroup ", cgb)
@@ -227,7 +229,7 @@ func newCgroup(c Config) (cgroup.Cgroup, error) {
227229
c.Warn("Creating api cgroup with error: ", err)
228230
c.Warn("As running in non-root mode, falling back back to rlimit / rusage mode")
229231
cgb.Destroy()
230-
return nil, nil
232+
return nil, nil, nil
231233
}
232234
}
233235

@@ -238,7 +240,13 @@ func newCgroup(c Config) (cgroup.Cgroup, error) {
238240
c.Warn("Falling back to rlimit / rusage mode")
239241
cgb = nil
240242
}
241-
return cg, nil
243+
if !ct.Memory {
244+
c.Warn("Memory cgroup is not enabled, failling back to rlimit / rusage mode")
245+
}
246+
if !ct.Pids {
247+
c.Warn("Pid cgroup is not enabled, proc limit does not have effect")
248+
}
249+
return cg, ct, nil
242250
}
243251

244252
func newSystemdProperty(name string, units any) dbus.Property {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23
55
require (
66
github.com/coreos/go-systemd/v22 v22.5.0
77
github.com/creack/pty v1.1.24
8-
github.com/criyle/go-sandbox v0.10.5
8+
github.com/criyle/go-sandbox v0.10.6
99
github.com/elastic/go-seccomp-bpf v1.5.0
1010
github.com/elastic/go-ucfg v0.8.8
1111
github.com/gin-contrib/zap v1.1.4

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8
1818
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
1919
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
2020
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
21-
github.com/criyle/go-sandbox v0.10.5 h1:VIlsXjh7cB+q6Sgm45+YpzQGk7vHQfRicnRzcuJ+6Ko=
22-
github.com/criyle/go-sandbox v0.10.5/go.mod h1:sYJUuTmJ72Jilkc1/PO7eDdpJq3rOZ55o8MxzP80vw0=
21+
github.com/criyle/go-sandbox v0.10.6 h1:vyp+r7V2Vl5xY4Pt/2kYvRF5wu1NhMwLgmPKD0lXZ/o=
22+
github.com/criyle/go-sandbox v0.10.6/go.mod h1:nZLj0b/45fZ/a+xWmWrPrcKSepnXu0iPAVF3xF54UEI=
2323
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2424
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2525
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)