opts.go 2.1 KB

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