remote_daemon_options.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // +build !windows
  2. package libcontainerd
  3. import "fmt"
  4. // WithRemoteAddr sets the external containerd socket to connect to.
  5. func WithRemoteAddr(addr string) RemoteOption {
  6. return rpcAddr(addr)
  7. }
  8. type rpcAddr string
  9. func (a rpcAddr) Apply(r Remote) error {
  10. if remote, ok := r.(*remote); ok {
  11. remote.GRPC.Address = string(a)
  12. return nil
  13. }
  14. return fmt.Errorf("WithRemoteAddr option not supported for this remote")
  15. }
  16. // WithRemoteAddrUser sets the uid and gid to create the RPC address with
  17. func WithRemoteAddrUser(uid, gid int) RemoteOption {
  18. return rpcUser{uid, gid}
  19. }
  20. type rpcUser struct {
  21. uid int
  22. gid int
  23. }
  24. func (u rpcUser) Apply(r Remote) error {
  25. if remote, ok := r.(*remote); ok {
  26. remote.GRPC.UID = u.uid
  27. remote.GRPC.GID = u.gid
  28. return nil
  29. }
  30. return fmt.Errorf("WithRemoteAddr option not supported for this remote")
  31. }
  32. // WithStartDaemon defines if libcontainerd should also run containerd daemon.
  33. func WithStartDaemon(start bool) RemoteOption {
  34. return startDaemon(start)
  35. }
  36. type startDaemon bool
  37. func (s startDaemon) Apply(r Remote) error {
  38. if remote, ok := r.(*remote); ok {
  39. remote.startDaemon = bool(s)
  40. return nil
  41. }
  42. return fmt.Errorf("WithStartDaemon option not supported for this remote")
  43. }
  44. // WithLogLevel defines which log level to starts containerd with.
  45. // This only makes sense if WithStartDaemon() was set to true.
  46. func WithLogLevel(lvl string) RemoteOption {
  47. return logLevel(lvl)
  48. }
  49. type logLevel string
  50. func (l logLevel) Apply(r Remote) error {
  51. if remote, ok := r.(*remote); ok {
  52. remote.Debug.Level = string(l)
  53. return nil
  54. }
  55. return fmt.Errorf("WithDebugLog option not supported for this remote")
  56. }
  57. // WithDebugAddress defines at which location the debug GRPC connection
  58. // should be made
  59. func WithDebugAddress(addr string) RemoteOption {
  60. return debugAddress(addr)
  61. }
  62. type debugAddress string
  63. func (d debugAddress) Apply(r Remote) error {
  64. if remote, ok := r.(*remote); ok {
  65. remote.Debug.Address = string(d)
  66. return nil
  67. }
  68. return fmt.Errorf("WithDebugAddress option not supported for this remote")
  69. }
  70. // WithMetricsAddress defines at which location the debug GRPC connection
  71. // should be made
  72. func WithMetricsAddress(addr string) RemoteOption {
  73. return metricsAddress(addr)
  74. }
  75. type metricsAddress string
  76. func (m metricsAddress) Apply(r Remote) error {
  77. if remote, ok := r.(*remote); ok {
  78. remote.Metrics.Address = string(m)
  79. return nil
  80. }
  81. return fmt.Errorf("WithMetricsAddress option not supported for this remote")
  82. }
  83. // WithSnapshotter defines snapshotter driver should be used
  84. func WithSnapshotter(name string) RemoteOption {
  85. return snapshotter(name)
  86. }
  87. type snapshotter string
  88. func (s snapshotter) Apply(r Remote) error {
  89. if remote, ok := r.(*remote); ok {
  90. remote.snapshotter = string(s)
  91. return nil
  92. }
  93. return fmt.Errorf("WithSnapshotter option not supported for this remote")
  94. }
  95. // WithPlugin allow configuring a containerd plugin
  96. // configuration values passed needs to be quoted if quotes are needed in
  97. // the toml format.
  98. func WithPlugin(name string, conf interface{}) RemoteOption {
  99. return pluginConf{
  100. name: name,
  101. conf: conf,
  102. }
  103. }
  104. type pluginConf struct {
  105. // Name is the name of the plugin
  106. name string
  107. conf interface{}
  108. }
  109. func (p pluginConf) Apply(r Remote) error {
  110. if remote, ok := r.(*remote); ok {
  111. remote.pluginConfs.Plugins[p.name] = p.conf
  112. return nil
  113. }
  114. return fmt.Errorf("WithPlugin option not supported for this remote")
  115. }