Skip to content

Commit

Permalink
Merge pull request #296 from singholt/main
Browse files Browse the repository at this point in the history
cgroup1 delete: proceed to the next subsystem when a cgroup is not found
  • Loading branch information
estesp authored Jun 20, 2023
2 parents f4638b4 + 280dc07 commit 8fa6424
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
8 changes: 6 additions & 2 deletions cgroup1/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func New(path Path, resources *specs.LinuxResources, opts ...InitOpts) (Cgroup,
return nil, err
}
}
subsystems, err := config.hiearchy()
subsystems, err := config.hierarchy()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func Load(path Path, opts ...InitOpts) (Cgroup, error) {
}
}
var activeSubsystems []Subsystem
subsystems, err := config.hiearchy()
subsystems, err := config.hierarchy()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -259,6 +259,10 @@ func (c *cgroup) Delete() error {
// kernel prevents cgroups with running process from being removed, check the tree is empty
procs, err := c.processes(s.Name(), true, cgroupProcs)
if err != nil {
// if the control group does not exist within a subsystem, then proceed to the next subsystem
if errors.Is(err, os.ErrNotExist) {
continue
}
return err
}
if len(procs) > 0 {
Expand Down
7 changes: 7 additions & 0 deletions cgroup1/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ func TestDelete(t *testing.T) {
t.Error(err)
return
}

err = mock.symLink()
if err != nil {
t.Error(err)
return
}

if err := control.Delete(); err != nil {
t.Error(err)
}
Expand Down
18 changes: 18 additions & 0 deletions cgroup1/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,21 @@ func (m *mockCgroup) delete() error {
func (m *mockCgroup) hierarchy() ([]Subsystem, error) {
return m.subsystems, nil
}

// symLink() creates a symlink between net_cls and net_prio for testing
// On certain Linux systems, there's a symlink from both net_cls and net_prio to "net_cls,net_prio"
// Since we don't have a subsystem defined for "net_cls,net_prio",
// we mock this behavior by creating a symlink directly between net_cls and net_prio
func (m *mockCgroup) symLink() error {
netCLS := filepath.Join(m.root, string(NetCLS))
netPrio := filepath.Join(m.root, string(NetPrio))
// remove netCLS before creating a symlink
if err := os.RemoveAll(netCLS); err != nil {
return err
}
// create symlink between net_cls and net_prio
if err := os.Symlink(netPrio, netCLS); err != nil {
return err
}
return nil
}
6 changes: 3 additions & 3 deletions cgroup1/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ type InitOpts func(*InitConfig) error
type InitConfig struct {
// InitCheck can be used to check initialization errors from the subsystem
InitCheck InitCheck
hiearchy Hierarchy
hierarchy Hierarchy
}

func newInitConfig() *InitConfig {
return &InitConfig{
InitCheck: RequireDevices,
hiearchy: Default,
hierarchy: Default,
}
}

Expand All @@ -66,7 +66,7 @@ func RequireDevices(s Subsystem, _ Path, _ error) error {
// The default list is coming from /proc/self/mountinfo.
func WithHiearchy(h Hierarchy) InitOpts {
return func(c *InitConfig) error {
c.hiearchy = h
c.hierarchy = h
return nil
}
}
2 changes: 1 addition & 1 deletion cgroup1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Default() ([]Subsystem, error) {
}

// v1MountPoint returns the mount point where the cgroup
// mountpoints are mounted in a single hiearchy
// mountpoints are mounted in a single hierarchy
func v1MountPoint() (string, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
Expand Down

0 comments on commit 8fa6424

Please sign in to comment.