config.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package server
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/BurntSushi/toml"
  6. "github.com/containerd/containerd/errdefs"
  7. "github.com/pkg/errors"
  8. )
  9. // Config provides containerd configuration data for the server
  10. type Config struct {
  11. // Root is the path to a directory where containerd will store persistent data
  12. Root string `toml:"root"`
  13. // State is the path to a directory where containerd will store transient data
  14. State string `toml:"state"`
  15. // GRPC configuration settings
  16. GRPC GRPCConfig `toml:"grpc"`
  17. // Debug and profiling settings
  18. Debug Debug `toml:"debug"`
  19. // Metrics and monitoring settings
  20. Metrics MetricsConfig `toml:"metrics"`
  21. // Plugins provides plugin specific configuration for the initialization of a plugin
  22. Plugins map[string]toml.Primitive `toml:"plugins"`
  23. // Enable containerd as a subreaper
  24. Subreaper bool `toml:"subreaper"`
  25. // OOMScore adjust the containerd's oom score
  26. OOMScore int `toml:"oom_score"`
  27. // Cgroup specifies cgroup information for the containerd daemon process
  28. Cgroup CgroupConfig `toml:"cgroup"`
  29. md toml.MetaData
  30. }
  31. // GRPCConfig provides GRPC configuration for the socket
  32. type GRPCConfig struct {
  33. Address string `toml:"address"`
  34. UID int `toml:"uid"`
  35. GID int `toml:"gid"`
  36. }
  37. // Debug provides debug configuration
  38. type Debug struct {
  39. Address string `toml:"address"`
  40. UID int `toml:"uid"`
  41. GID int `toml:"gid"`
  42. Level string `toml:"level"`
  43. }
  44. // MetricsConfig provides metrics configuration
  45. type MetricsConfig struct {
  46. Address string `toml:"address"`
  47. }
  48. // CgroupConfig provides cgroup configuration
  49. type CgroupConfig struct {
  50. Path string `toml:"path"`
  51. }
  52. // Decode unmarshals a plugin specific configuration by plugin id
  53. func (c *Config) Decode(id string, v interface{}) (interface{}, error) {
  54. data, ok := c.Plugins[id]
  55. if !ok {
  56. return v, nil
  57. }
  58. if err := c.md.PrimitiveDecode(data, v); err != nil {
  59. return nil, err
  60. }
  61. return v, nil
  62. }
  63. // WriteTo marshals the config to the provided writer
  64. func (c *Config) WriteTo(w io.Writer) (int64, error) {
  65. buf := bytes.NewBuffer(nil)
  66. e := toml.NewEncoder(buf)
  67. if err := e.Encode(c); err != nil {
  68. return 0, err
  69. }
  70. return io.Copy(w, buf)
  71. }
  72. // LoadConfig loads the containerd server config from the provided path
  73. func LoadConfig(path string, v *Config) error {
  74. if v == nil {
  75. return errors.Wrapf(errdefs.ErrInvalidArgument, "argument v must not be nil")
  76. }
  77. md, err := toml.DecodeFile(path, v)
  78. if err != nil {
  79. return err
  80. }
  81. v.md = md
  82. return nil
  83. }