Skip to content

Commit d54d55b

Browse files
author
Kazuyoshi Kato
committed
Make cgroup1.Load and cgroup2.Load closer
- cgroup1.Load's first parameter could be moved to functional options. - cgroup2.LoadManager's first parameter could be moved too and Manager suffix doesn't make much sense. Both changes make cgroup1.Load and cgroup2.Load much closer. Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
1 parent 129bf0a commit d54d55b

4 files changed

Lines changed: 42 additions & 11 deletions

File tree

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/opts.go

Lines changed: 9 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,10 @@ func RequireDevices(s Subsystem, _ Path, _ error) error {
5961
}
6062
return ErrIgnoreSubsystem
6163
}
64+
65+
func WithHiearchy(h Hierarchy) InitOpts {
66+
return func(c *InitConfig) error {
67+
c.hiearchy = h
68+
return nil
69+
}
70+
}

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 other than /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)