Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit a37b2a4

Browse files
committed
Merge pull request #476 from hqhq/hq_dont_fail_subsystem
don't fail when subsystem not mounted
2 parents 2a94c82 + 27d3dd3 commit a37b2a4

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

cgroups/fs/apply_raw.go

+5
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ func (raw *data) join(subsystem string) (string, error) {
262262
}
263263

264264
func writeFile(dir, file, data string) error {
265+
// Normally dir should not be empty, one case is that cgroup subsystem
266+
// is not mounted, we will get empty dir, and we want it fail here.
267+
if dir == "" {
268+
return fmt.Errorf("no such directory for %s.", file)
269+
}
265270
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
266271
}
267272

cgroups/fs/cpu.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (s *CpuGroup) Apply(d *data) error {
1717
// We always want to join the cpu group, to allow fair cpu scheduling
1818
// on a container basis
1919
dir, err := d.join("cpu")
20-
if err != nil {
20+
if err != nil && !cgroups.IsNotFound(err) {
2121
return err
2222
}
2323

cgroups/fs/cpuset.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type CpusetGroup struct {
1616

1717
func (s *CpusetGroup) Apply(d *data) error {
1818
dir, err := d.path("cpuset")
19-
if err != nil {
19+
if err != nil && !cgroups.IsNotFound(err) {
2020
return err
2121
}
2222

@@ -48,6 +48,11 @@ func (s *CpusetGroup) GetStats(path string, stats *cgroups.Stats) error {
4848
}
4949

5050
func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) error {
51+
// This might happen if we have no cpuset cgroup mounted.
52+
// Just do nothing and don't fail.
53+
if dir == "" {
54+
return nil
55+
}
5156
if err := s.ensureParent(dir); err != nil {
5257
return err
5358
}

cgroups/fs/devices.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type DevicesGroup struct {
1111
func (s *DevicesGroup) Apply(d *data) error {
1212
dir, err := d.join("devices")
1313
if err != nil {
14+
// We will return error even it's `not found` error, devices
15+
// cgroup is hard requirement for container's security.
1416
return err
1517
}
1618

cgroups/fs/memory.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ type MemoryGroup struct {
1616

1717
func (s *MemoryGroup) Apply(d *data) error {
1818
dir, err := d.join("memory")
19-
// only return an error for memory if it was specified
20-
if err != nil && (d.c.Memory != 0 || d.c.MemoryReservation != 0 || d.c.MemorySwap != 0) {
19+
if err != nil && !cgroups.IsNotFound(err) {
2120
return err
2221
}
2322
defer func() {

cgroups/systemd/apply_systemd.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ func (m *Manager) GetPaths() map[string]string {
256256
}
257257

258258
func writeFile(dir, file, data string) error {
259+
// Normally dir should not be empty, one case is that cgroup subsystem
260+
// is not mounted, we will get empty dir, and we want it fail here.
261+
if dir == "" {
262+
return fmt.Errorf("no such directory for %s.", file)
263+
}
259264
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
260265
}
261266

@@ -276,24 +281,24 @@ func join(c *configs.Cgroup, subsystem string, pid int) (string, error) {
276281

277282
func joinCpu(c *configs.Cgroup, pid int) error {
278283
path, err := getSubsystemPath(c, "cpu")
279-
if err != nil {
284+
if err != nil && !cgroups.IsNotFound(err) {
280285
return err
281286
}
282287
if c.CpuQuota != 0 {
283-
if err = ioutil.WriteFile(filepath.Join(path, "cpu.cfs_quota_us"), []byte(strconv.FormatInt(c.CpuQuota, 10)), 0700); err != nil {
288+
if err = writeFile(path, "cpu.cfs_quota_us", strconv.FormatInt(c.CpuQuota, 10)); err != nil {
284289
return err
285290
}
286291
}
287292
if c.CpuPeriod != 0 {
288-
if err = ioutil.WriteFile(filepath.Join(path, "cpu.cfs_period_us"), []byte(strconv.FormatInt(c.CpuPeriod, 10)), 0700); err != nil {
293+
if err = writeFile(path, "cpu.cfs_period_us", strconv.FormatInt(c.CpuPeriod, 10)); err != nil {
289294
return err
290295
}
291296
}
292297
return nil
293298
}
294299

295300
func joinFreezer(c *configs.Cgroup, pid int) error {
296-
if _, err := join(c, "freezer", pid); err != nil {
301+
if _, err := join(c, "freezer", pid); err != nil && !cgroups.IsNotFound(err) {
297302
return err
298303
}
299304

@@ -393,6 +398,8 @@ func getUnitName(c *configs.Cgroup) string {
393398
// This happens at least for v208 when any sibling unit is started.
394399
func joinDevices(c *configs.Cgroup, pid int) error {
395400
path, err := join(c, "devices", pid)
401+
// Even if it's `not found` error, we'll return err because devices cgroup
402+
// is hard requirement for container security.
396403
if err != nil {
397404
return err
398405
}
@@ -410,19 +417,19 @@ func joinMemory(c *configs.Cgroup, pid int) error {
410417
}
411418

412419
path, err := getSubsystemPath(c, "memory")
413-
if err != nil {
420+
if err != nil && !cgroups.IsNotFound(err) {
414421
return err
415422
}
416423

417-
return ioutil.WriteFile(filepath.Join(path, "memory.memsw.limit_in_bytes"), []byte(strconv.FormatInt(memorySwap, 10)), 0700)
424+
return writeFile(path, "memory.memsw.limit_in_bytes", strconv.FormatInt(memorySwap, 10))
418425
}
419426

420427
// systemd does not atm set up the cpuset controller, so we must manually
421428
// join it. Additionally that is a very finicky controller where each
422429
// level must have a full setup as the default for a new directory is "no cpus"
423430
func joinCpuset(c *configs.Cgroup, pid int) error {
424431
path, err := getSubsystemPath(c, "cpuset")
425-
if err != nil {
432+
if err != nil && !cgroups.IsNotFound(err) {
426433
return err
427434
}
428435

0 commit comments

Comments
 (0)