Skip to content

Commit 4a9f0f7

Browse files
committed
Add SkipOpts for handling how subsystems are registered
This adds a flexible way for subsystems to be skipped or required within the cgroups package. Signed-off-by: Michael Crosby <[email protected]>
1 parent 4dacf2b commit 4a9f0f7

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

cgroup.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ import (
3030
)
3131

3232
// New returns a new control via the cgroup cgroups interface
33-
func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources) (Cgroup, error) {
33+
func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources, opts ...InitOpts) (Cgroup, error) {
34+
config := newInitConfig()
35+
for _, o := range opts {
36+
if err := o(config); err != nil {
37+
return nil, err
38+
}
39+
}
3440
subsystems, err := hierarchy()
3541
if err != nil {
3642
return nil, err
@@ -40,6 +46,13 @@ func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources) (Cgrou
4046
// check if subsystem exists
4147
if err := initializeSubsystem(s, path, resources); err != nil {
4248
if err == ErrControllerNotActive {
49+
if config.InitCheck != nil {
50+
if skerr := config.InitCheck(s, path, err); skerr != nil {
51+
if skerr != ErrIgnoreSubsystem {
52+
return nil, skerr
53+
}
54+
}
55+
}
4356
continue
4457
}
4558
return nil, err
@@ -53,7 +66,13 @@ func New(hierarchy Hierarchy, path Path, resources *specs.LinuxResources) (Cgrou
5366
}
5467

5568
// Load will load an existing cgroup and allow it to be controlled
56-
func Load(hierarchy Hierarchy, path Path) (Cgroup, error) {
69+
func Load(hierarchy Hierarchy, path Path, opts ...InitOpts) (Cgroup, error) {
70+
config := newInitConfig()
71+
for _, o := range opts {
72+
if err := o(config); err != nil {
73+
return nil, err
74+
}
75+
}
5776
var activeSubsystems []Subsystem
5877
subsystems, err := hierarchy()
5978
if err != nil {
@@ -67,6 +86,13 @@ func Load(hierarchy Hierarchy, path Path) (Cgroup, error) {
6786
return nil, ErrCgroupDeleted
6887
}
6988
if err == ErrControllerNotActive {
89+
if config.InitCheck != nil {
90+
if skerr := config.InitCheck(s, path, err); skerr != nil {
91+
if skerr != ErrIgnoreSubsystem {
92+
return nil, skerr
93+
}
94+
}
95+
}
7096
continue
7197
}
7298
return nil, err

opts.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cgroups
18+
19+
import (
20+
"github.com/pkg/errors"
21+
)
22+
23+
var (
24+
// ErrIgnoreSubsystem allows the specific subsystem to be skipped
25+
ErrIgnoreSubsystem = errors.New("skip subsystem")
26+
// ErrDevicesRequired is returned when the devices subsystem is required but
27+
// does not exist or is not active
28+
ErrDevicesRequired = errors.New("devices subsystem is required")
29+
)
30+
31+
// InitOpts allows configuration for the creation or loading of a cgroup
32+
type InitOpts func(*InitConfig) error
33+
34+
// InitConfig provides configuration options for the creation
35+
// or loading of a cgroup and its subsystems
36+
type InitConfig struct {
37+
// InitCheck can be used to check initialization errors from the subsystem
38+
InitCheck InitCheck
39+
}
40+
41+
func newInitConfig() *InitConfig {
42+
return &InitConfig{
43+
InitCheck: RequireDevices,
44+
}
45+
}
46+
47+
// InitCheck allows subsystems errors to be checked when initialized or loaded
48+
type InitCheck func(Subsystem, Path, error) error
49+
50+
// AllowAny allows any subsystem errors to be skipped
51+
func AllowAny(s Subsystem, p Path, err error) error {
52+
return ErrIgnoreSubsystem
53+
}
54+
55+
// RequireDevices requires the device subsystem but no others
56+
func RequireDevices(s Subsystem, p Path, err error) error {
57+
if s.Name() == Devices {
58+
return ErrDevicesRequired
59+
}
60+
return ErrIgnoreSubsystem
61+
}

paths.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func PidPath(pid int) Path {
5757
return existingPath(paths, "")
5858
}
5959

60-
// ErrControllerNotActive is returned when a controller is not supported or enalbed
60+
// ErrControllerNotActive is returned when a controller is not supported or enabled
6161
var ErrControllerNotActive = errors.New("controller is not supported")
6262

6363
func existingPath(paths map[string]string, suffix string) Path {

0 commit comments

Comments
 (0)