Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ uses the v1 implementation of cgroups.

```go
shares := uint64(100)
control, err := cgroup1.New(cgroup1.Default, cgroup1.StaticPath("/test"), &specs.LinuxResources{
control, err := cgroup1.New(cgroup1.StaticPath("/test"), &specs.LinuxResources{
CPU: &specs.LinuxCPU{
Shares: &shares,
},
Expand Down
8 changes: 4 additions & 4 deletions cgroup1/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import (
)

// New returns a new control via the cgroup cgroups interface
func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources, opts ...InitOpts) (Cgroup, error) {
func New(path Path, resources *specs.LinuxResources, opts ...InitOpts) (Cgroup, error) {
config := newInitConfig()
for _, o := range opts {
if err := o(config); err != nil {
return nil, err
}
}
subsystems, err := hierarchy()
subsystems, err := config.hiearchy()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -71,15 +71,15 @@ func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources, opts .

// Load will load an existing cgroup and allow it to be controlled
// All static path should not include `/sys/fs/cgroup/` prefix, it should start with your own cgroups name
func Load(hierarchy Hierarchy, path Path, opts ...InitOpts) (Cgroup, error) {
func Load(path Path, opts ...InitOpts) (Cgroup, error) {
config := newInitConfig()
for _, o := range opts {
if err := o(config); err != nil {
return nil, err
}
}
var activeSubsystems []Subsystem
subsystems, err := hierarchy()
subsystems, err := config.hiearchy()
if err != nil {
return nil, err
}
Expand Down
32 changes: 16 additions & 16 deletions cgroup1/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCreate(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand All @@ -61,7 +61,7 @@ func TestStat(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand All @@ -83,7 +83,7 @@ func TestAdd(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand All @@ -106,7 +106,7 @@ func TestAddFilteredSubsystems(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestAddTask(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand All @@ -186,7 +186,7 @@ func TestAddTaskFilteredSubsystems(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestListPids(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestListTasksPids(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -350,12 +350,12 @@ func TestLoad(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
}
if control, err = Load(mock.hierarchy, StaticPath("test")); err != nil {
if control, err = Load(StaticPath("test"), WithHiearchy(mock.hierarchy)); err != nil {
t.Error(err)
return
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func TestLoadWithMissingSubsystems(t *testing.T) {
t.Error("control is nil")
return
}
if control, err = Load(mock.hierarchy, StaticPath("test")); err != nil {
if control, err = Load(StaticPath("test"), WithHiearchy(mock.hierarchy)); err != nil {
t.Error(err)
return
}
Expand All @@ -405,7 +405,7 @@ func TestDelete(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand All @@ -421,7 +421,7 @@ func TestCreateSubCgroup(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestFreezeThaw(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestSubsystems(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand All @@ -511,7 +511,7 @@ func TestCpusetParent(t *testing.T) {
t.Fatal(err)
}
defer mock.delete()
control, err := New(mock.hierarchy, StaticPath("/parent/child"), &specs.LinuxResources{})
control, err := New(StaticPath("/parent/child"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
if err != nil {
t.Error(err)
return
Expand Down
11 changes: 11 additions & 0 deletions cgroup1/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +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
}

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

Expand All @@ -59,3 +61,12 @@ func RequireDevices(s Subsystem, _ Path, _ error) error {
}
return ErrIgnoreSubsystem
}

// WithHiearchy sets a list of cgroup subsystems.
// The default list is coming from /proc/self/mountinfo.
func WithHiearchy(h Hierarchy) InitOpts {
return func(c *InitConfig) error {
c.hiearchy = h
return nil
}
}
28 changes: 25 additions & 3 deletions cgroup2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,35 @@ func NewManager(mountpoint string, group string, resources *Resources) (*Manager
return &m, nil
}

func LoadManager(mountpoint string, group string) (*Manager, error) {
type InitConfig struct {
mountpoint string
}

type InitOpts func(c *InitConfig) error

// WithMountpoint sets the unified mountpoint. The deault path is /sys/fs/cgroup.
func WithMountpoint(path string) InitOpts {
return func(c *InitConfig) error {
c.mountpoint = path
return nil
}
}

// Load a cgroup.
func Load(group string, opts ...InitOpts) (*Manager, error) {
c := InitConfig{mountpoint: defaultCgroup2Path}
for _, opt := range opts {
if err := opt(&c); err != nil {
return nil, err
}
}

if err := VerifyGroupPath(group); err != nil {
return nil, err
}
path := filepath.Join(mountpoint, group)
path := filepath.Join(c.mountpoint, group)
return &Manager{
unifiedMountpoint: mountpoint,
unifiedMountpoint: c.mountpoint,
path: path,
}, nil
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/cgctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var delCommand = cli.Command{
Usage: "delete a cgroup",
Action: func(clix *cli.Context) error {
path := clix.Args().First()
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
if err != nil {
return err
}
Expand All @@ -112,7 +112,7 @@ var listCommand = cli.Command{
Usage: "list processes in a cgroup",
Action: func(clix *cli.Context) error {
path := clix.Args().First()
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
if err != nil {
return err
}
Expand All @@ -132,7 +132,7 @@ var listControllersCommand = cli.Command{
Usage: "list controllers in a cgroup",
Action: func(clix *cli.Context) error {
path := clix.Args().First()
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
if err != nil {
return err
}
Expand All @@ -152,7 +152,7 @@ var statCommand = cli.Command{
Usage: "stat a cgroup",
Action: func(clix *cli.Context) error {
path := clix.Args().First()
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
if err != nil {
return err
}
Expand Down