Skip to content

Commit e8802a1

Browse files
authored
Merge pull request #259 from kzys/load
Make cgroup1.Load and cgroup2.Load closer
2 parents 129bf0a + 49f7d91 commit e8802a1

6 files changed

Lines changed: 61 additions & 28 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ uses the v1 implementation of cgroups.
2525

2626
```go
2727
shares := uint64(100)
28-
control, err := cgroup1.New(cgroup1.Default, cgroup1.StaticPath("/test"), &specs.LinuxResources{
28+
control, err := cgroup1.New(cgroup1.StaticPath("/test"), &specs.LinuxResources{
2929
CPU: &specs.LinuxCPU{
3030
Shares: &shares,
3131
},

cgroup1/cgroup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ import (
3434
)
3535

3636
// New returns a new control via the cgroup cgroups interface
37-
func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources, opts ...InitOpts) (Cgroup, error) {
37+
func New(path Path, resources *specs.LinuxResources, opts ...InitOpts) (Cgroup, error) {
3838
config := newInitConfig()
3939
for _, o := range opts {
4040
if err := o(config); err != nil {
4141
return nil, err
4242
}
4343
}
44-
subsystems, err := hierarchy()
44+
subsystems, err := config.hiearchy()
4545
if err != nil {
4646
return nil, err
4747
}
@@ -71,15 +71,15 @@ func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources, opts .
7171

7272
// Load will load an existing cgroup and allow it to be controlled
7373
// All static path should not include `/sys/fs/cgroup/` prefix, it should start with your own cgroups name
74-
func Load(hierarchy Hierarchy, path Path, opts ...InitOpts) (Cgroup, error) {
74+
func Load(path Path, opts ...InitOpts) (Cgroup, error) {
7575
config := newInitConfig()
7676
for _, o := range opts {
7777
if err := o(config); err != nil {
7878
return nil, err
7979
}
8080
}
8181
var activeSubsystems []Subsystem
82-
subsystems, err := hierarchy()
82+
subsystems, err := config.hiearchy()
8383
if err != nil {
8484
return nil, err
8585
}

cgroup1/cgroup_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCreate(t *testing.T) {
3434
t.Fatal(err)
3535
}
3636
defer mock.delete()
37-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
37+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
3838
if err != nil {
3939
t.Error(err)
4040
return
@@ -61,7 +61,7 @@ func TestStat(t *testing.T) {
6161
t.Fatal(err)
6262
}
6363
defer mock.delete()
64-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
64+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
6565
if err != nil {
6666
t.Error(err)
6767
return
@@ -83,7 +83,7 @@ func TestAdd(t *testing.T) {
8383
t.Fatal(err)
8484
}
8585
defer mock.delete()
86-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
86+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
8787
if err != nil {
8888
t.Error(err)
8989
return
@@ -106,7 +106,7 @@ func TestAddFilteredSubsystems(t *testing.T) {
106106
t.Fatal(err)
107107
}
108108
defer mock.delete()
109-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
109+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
110110
if err != nil {
111111
t.Error(err)
112112
return
@@ -163,7 +163,7 @@ func TestAddTask(t *testing.T) {
163163
t.Fatal(err)
164164
}
165165
defer mock.delete()
166-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
166+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
167167
if err != nil {
168168
t.Error(err)
169169
return
@@ -186,7 +186,7 @@ func TestAddTaskFilteredSubsystems(t *testing.T) {
186186
t.Fatal(err)
187187
}
188188
defer mock.delete()
189-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
189+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
190190
if err != nil {
191191
t.Error(err)
192192
return
@@ -228,7 +228,7 @@ func TestListPids(t *testing.T) {
228228
t.Fatal(err)
229229
}
230230
defer mock.delete()
231-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
231+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
232232
if err != nil {
233233
t.Error(err)
234234
return
@@ -263,7 +263,7 @@ func TestListTasksPids(t *testing.T) {
263263
t.Fatal(err)
264264
}
265265
defer mock.delete()
266-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
266+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
267267
if err != nil {
268268
t.Error(err)
269269
return
@@ -350,12 +350,12 @@ func TestLoad(t *testing.T) {
350350
t.Fatal(err)
351351
}
352352
defer mock.delete()
353-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
353+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
354354
if err != nil {
355355
t.Error(err)
356356
return
357357
}
358-
if control, err = Load(mock.hierarchy, StaticPath("test")); err != nil {
358+
if control, err = Load(StaticPath("test"), WithHiearchy(mock.hierarchy)); err != nil {
359359
t.Error(err)
360360
return
361361
}
@@ -385,7 +385,7 @@ func TestLoadWithMissingSubsystems(t *testing.T) {
385385
t.Error("control is nil")
386386
return
387387
}
388-
if control, err = Load(mock.hierarchy, StaticPath("test")); err != nil {
388+
if control, err = Load(StaticPath("test"), WithHiearchy(mock.hierarchy)); err != nil {
389389
t.Error(err)
390390
return
391391
}
@@ -405,7 +405,7 @@ func TestDelete(t *testing.T) {
405405
t.Fatal(err)
406406
}
407407
defer mock.delete()
408-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
408+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
409409
if err != nil {
410410
t.Error(err)
411411
return
@@ -421,7 +421,7 @@ func TestCreateSubCgroup(t *testing.T) {
421421
t.Fatal(err)
422422
}
423423
defer mock.delete()
424-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
424+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
425425
if err != nil {
426426
t.Error(err)
427427
return
@@ -459,7 +459,7 @@ func TestFreezeThaw(t *testing.T) {
459459
t.Fatal(err)
460460
}
461461
defer mock.delete()
462-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
462+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
463463
if err != nil {
464464
t.Error(err)
465465
return
@@ -488,7 +488,7 @@ func TestSubsystems(t *testing.T) {
488488
t.Fatal(err)
489489
}
490490
defer mock.delete()
491-
control, err := New(mock.hierarchy, StaticPath("test"), &specs.LinuxResources{})
491+
control, err := New(StaticPath("test"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
492492
if err != nil {
493493
t.Error(err)
494494
return
@@ -511,7 +511,7 @@ func TestCpusetParent(t *testing.T) {
511511
t.Fatal(err)
512512
}
513513
defer mock.delete()
514-
control, err := New(mock.hierarchy, StaticPath("/parent/child"), &specs.LinuxResources{})
514+
control, err := New(StaticPath("/parent/child"), &specs.LinuxResources{}, WithHiearchy(mock.hierarchy))
515515
if err != nil {
516516
t.Error(err)
517517
return

cgroup1/opts.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ type InitOpts func(*InitConfig) error
3636
type InitConfig struct {
3737
// InitCheck can be used to check initialization errors from the subsystem
3838
InitCheck InitCheck
39+
hiearchy Hierarchy
3940
}
4041

4142
func newInitConfig() *InitConfig {
4243
return &InitConfig{
4344
InitCheck: RequireDevices,
45+
hiearchy: Default,
4446
}
4547
}
4648

@@ -59,3 +61,12 @@ func RequireDevices(s Subsystem, _ Path, _ error) error {
5961
}
6062
return ErrIgnoreSubsystem
6163
}
64+
65+
// WithHiearchy sets a list of cgroup subsystems.
66+
// The default list is coming from /proc/self/mountinfo.
67+
func WithHiearchy(h Hierarchy) InitOpts {
68+
return func(c *InitConfig) error {
69+
c.hiearchy = h
70+
return nil
71+
}
72+
}

cgroup2/manager.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,35 @@ func NewManager(mountpoint string, group string, resources *Resources) (*Manager
196196
return &m, nil
197197
}
198198

199-
func LoadManager(mountpoint string, group string) (*Manager, error) {
199+
type InitConfig struct {
200+
mountpoint string
201+
}
202+
203+
type InitOpts func(c *InitConfig) error
204+
205+
// WithMountpoint sets the unified mountpoint. The deault path is /sys/fs/cgroup.
206+
func WithMountpoint(path string) InitOpts {
207+
return func(c *InitConfig) error {
208+
c.mountpoint = path
209+
return nil
210+
}
211+
}
212+
213+
// Load a cgroup.
214+
func Load(group string, opts ...InitOpts) (*Manager, error) {
215+
c := InitConfig{mountpoint: defaultCgroup2Path}
216+
for _, opt := range opts {
217+
if err := opt(&c); err != nil {
218+
return nil, err
219+
}
220+
}
221+
200222
if err := VerifyGroupPath(group); err != nil {
201223
return nil, err
202224
}
203-
path := filepath.Join(mountpoint, group)
225+
path := filepath.Join(c.mountpoint, group)
204226
return &Manager{
205-
unifiedMountpoint: mountpoint,
227+
unifiedMountpoint: c.mountpoint,
206228
path: path,
207229
}, nil
208230
}

cmd/cgctl/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var delCommand = cli.Command{
9999
Usage: "delete a cgroup",
100100
Action: func(clix *cli.Context) error {
101101
path := clix.Args().First()
102-
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
102+
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
103103
if err != nil {
104104
return err
105105
}
@@ -112,7 +112,7 @@ var listCommand = cli.Command{
112112
Usage: "list processes in a cgroup",
113113
Action: func(clix *cli.Context) error {
114114
path := clix.Args().First()
115-
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
115+
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
116116
if err != nil {
117117
return err
118118
}
@@ -132,7 +132,7 @@ var listControllersCommand = cli.Command{
132132
Usage: "list controllers in a cgroup",
133133
Action: func(clix *cli.Context) error {
134134
path := clix.Args().First()
135-
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
135+
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
136136
if err != nil {
137137
return err
138138
}
@@ -152,7 +152,7 @@ var statCommand = cli.Command{
152152
Usage: "stat a cgroup",
153153
Action: func(clix *cli.Context) error {
154154
path := clix.Args().First()
155-
c, err := cgroup2.LoadManager(clix.GlobalString("mountpoint"), path)
155+
c, err := cgroup2.Load(path, cgroup2.WithMountpoint(clix.GlobalString("mountpoint")))
156156
if err != nil {
157157
return err
158158
}

0 commit comments

Comments
 (0)