opts.go (2113B)
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 cgroup1 18 19 import ( 20 "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 hiearchy Hierarchy 40 } 41 42 func newInitConfig() *InitConfig { 43 return &InitConfig{ 44 InitCheck: RequireDevices, 45 hiearchy: Default, 46 } 47 } 48 49 // InitCheck allows subsystems errors to be checked when initialized or loaded 50 type InitCheck func(Subsystem, Path, error) error 51 52 // AllowAny allows any subsystem errors to be skipped 53 func AllowAny(_ Subsystem, _ Path, _ error) error { 54 return ErrIgnoreSubsystem 55 } 56 57 // RequireDevices requires the device subsystem but no others 58 func RequireDevices(s Subsystem, _ Path, _ error) error { 59 if s.Name() == Devices { 60 return ErrDevicesRequired 61 } 62 return ErrIgnoreSubsystem 63 } 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 }